Skip to content

Commit 72668e5

Browse files
authored
Merge pull request #98 from pallets-eco/replace-black-by-ruff
Replace Black by Ruff
2 parents ccf5fa2 + a0842bb commit 72668e5

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

.flake8

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ jobs:
3939
python -m pip install --upgrade pip
4040
pip install -e .
4141
pip install -r requirements/test.txt
42-
- name: Test with pytest
42+
- name: Run tests and check code format
4343
run: |
44-
python3 -m pytest
45-
- name: Lint with flake8
46-
run: |
47-
flake8 .
44+
python3 -m pytest -ruff --ruff-format

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![PyPI](https://img.shields.io/pypi/v/Flask-Pydantic?color=g)](https://pypi.org/project/Flask-Pydantic/)
44
[![License](https://img.shields.io/badge/license-MIT-purple)](https://github.com/bauerji/flask_pydantic/blob/master/LICENSE)
5-
[![Code style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black)
65

76
Flask extension for integration of the awesome [pydantic package](https://github.com/samuelcolvin/pydantic) with [Flask](https://palletsprojects.com/p/flask/).
87

@@ -343,11 +342,11 @@ Feature requests and pull requests are welcome. For major changes, please open a
343342
```bash
344343
git checkout -b <your_branch_name>
345344
```
346-
- run tests
345+
- make sure your code style is compliant with [Ruff](https://github.com/astral-sh/ruff). Your can check these errors and automatically correct some of them with `ruff check --select I --fix . `
346+
- run tests and check code format
347347
```bash
348-
python3 -m pytest
348+
python3 -m pytest --ruff --ruff-format
349349
```
350-
- if tests fails on Black tests, make sure You have your code compliant with style of [Black formatter](https://github.com/psf/black)
351350
- push your changes and create a pull request to master branch
352351

353352
## TODOs:

flask_pydantic/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, Callable, Iterable, List, Optional, Tuple, Type, Union
33

44
from flask import Response, current_app, jsonify, make_response, request
5-
from pydantic import BaseModel, ValidationError, TypeAdapter, RootModel
5+
from pydantic import BaseModel, RootModel, TypeAdapter, ValidationError
66

77
from .converters import convert_query_params
88
from .exceptions import (
@@ -53,7 +53,7 @@ def is_iterable_of_models(content: Any) -> bool:
5353
def validate_many_models(model: Type[BaseModel], content: Any) -> List[BaseModel]:
5454
try:
5555
return [model(**fields) for fields in content]
56-
except TypeError:
56+
except TypeError as te:
5757
# iteration through `content` fails
5858
err = [
5959
{
@@ -62,9 +62,9 @@ def validate_many_models(model: Type[BaseModel], content: Any) -> List[BaseModel
6262
"type": "type_error.array",
6363
}
6464
]
65-
raise ManyModelValidationError(err)
65+
raise ManyModelValidationError(err) from te
6666
except ValidationError as ve:
67-
raise ManyModelValidationError(ve.errors())
67+
raise ManyModelValidationError(ve.errors()) from ve
6868

6969

7070
def validate_path_params(func: Callable, kwargs: dict) -> Tuple[dict, list]:
@@ -194,13 +194,13 @@ def wrapper(*args, **kwargs):
194194
else:
195195
try:
196196
b = body_model(**body_params)
197-
except TypeError:
197+
except TypeError as te:
198198
content_type = request.headers.get("Content-Type", "").lower()
199199
media_type = content_type.split(";")[0]
200200
if media_type != "application/json":
201201
return unsupported_media_type_response(content_type)
202202
else:
203-
raise JsonBodyParsingError()
203+
raise JsonBodyParsingError() from te
204204
except ValidationError as ve:
205205
err["body_params"] = ve.errors()
206206
form_in_kwargs = func.__annotations__.get("form")
@@ -215,13 +215,13 @@ def wrapper(*args, **kwargs):
215215
else:
216216
try:
217217
f = form_model(**form_params)
218-
except TypeError:
218+
except TypeError as te:
219219
content_type = request.headers.get("Content-Type", "").lower()
220220
media_type = content_type.split(";")[0]
221221
if media_type != "multipart/form-data":
222222
return unsupported_media_type_response(content_type)
223223
else:
224-
raise JsonBodyParsingError
224+
raise JsonBodyParsingError from te
225225
except ValidationError as ve:
226226
err["form_params"] = ve.errors()
227227
request.query_params = q

pyproject.toml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,19 @@ name = "flask_pydantic"
3737

3838
[tool.pytest]
3939
testpaths = "tests"
40-
addopts = "-vv --black --cov --cov-config=pyproject.toml -s"
41-
flake8-ignore = "E121 E122 E123 E124 E125 E126 E127 E128 E711 E712 F811 F841 H803 E501 E265 E741 W391 W503 E203"
40+
addopts = "-vv --ruff --ruff-format --cov --cov-config=pyproject.toml -s"
41+
42+
[tool.ruff]
43+
src = ["flask_pydantic"]
44+
lint.select = [
45+
"B", # flake8-bugbear
46+
"E", # pycodestyle error
47+
"F", # pyflakes
48+
"I", # isort
49+
"UP", # pyupgrade
50+
"W", # pycodestyle warning
51+
]
52+
lint.ignore = ["E501"]
4253

4354
[tool.coverage.run]
4455
branch = true

requirements/test.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pytest
22
pytest-flask
3-
pytest-flake8
43
pytest-coverage
5-
pytest-black
64
pytest-mock
5+
pytest-ruff

tests/func/test_app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from ..util import assert_matches
21
import re
32
from typing import List, Optional
43

54
import pytest
65
from flask import jsonify, request
7-
from flask_pydantic import validate, ValidationError
8-
from pydantic import BaseModel, RootModel, ConfigDict
6+
from flask_pydantic import ValidationError, validate
7+
from pydantic import BaseModel, ConfigDict, RootModel
8+
9+
from ..util import assert_matches
910

1011

1112
class ArrayModel(BaseModel):
@@ -308,7 +309,7 @@ def test_custom_headers(client):
308309

309310

310311
@pytest.mark.usefixtures("app_with_custom_headers_status")
311-
def test_custom_headers(client):
312+
def test_custom_headers_status(client):
312313
response = client.get("/custom_headers_status")
313314
assert response.json == {"test": 1}
314315
assert response.status_code == 201

tests/unit/test_core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import re
22
from typing import Any, List, NamedTuple, Optional, Type, Union
3-
from ..util import assert_matches
43

54
import pytest
65
from flask import jsonify
7-
from flask_pydantic import validate, ValidationError
6+
from flask_pydantic import ValidationError, validate
87
from flask_pydantic.core import convert_query_params, is_iterable_of_models
98
from flask_pydantic.exceptions import (
109
InvalidIterableOfModelsException,
@@ -13,6 +12,8 @@
1312
from pydantic import BaseModel, RootModel
1413
from werkzeug.datastructures import ImmutableMultiDict
1514

15+
from ..util import assert_matches
16+
1617

1718
class ValidateParams(NamedTuple):
1819
body_model: Optional[Type[BaseModel]] = None

0 commit comments

Comments
 (0)