Skip to content

Commit 49f582a

Browse files
committed
update ruff linter
1 parent 71c3294 commit 49f582a

39 files changed

+1228
-1212
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repos:
1515
- id: black
1616

1717
- repo: https://github.com/charliermarsh/ruff-pre-commit
18-
rev: "v0.0.269"
18+
rev: "v0.1.8"
1919
hooks:
2020
- id: ruff
21-
args: [--fix, --exit-non-zero-on-fix]
21+
args: [--fix, --exit-non-zero-on-fix, --unsafe-fixes]

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Create a test.py file and copy the following code into it
3030

3131
```python
3232
from pathlib import Path
33-
from typing import Any, Dict
33+
from typing import Any, ClassVar, Dict
3434

3535
import uvicorn
3636
from fastapi import APIRouter, Depends, FastAPI
@@ -120,7 +120,7 @@ def session_dependency_handler(view: ViewBase, dto: SessionDependency) -> Dict[s
120120

121121

122122
class UserDetailView(DetailViewBaseGeneric):
123-
method_dependencies = {
123+
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
124124
HTTPMethod.ALL: HTTPMethodConfig(
125125
dependencies=SessionDependency,
126126
prepare_data_layer_kwargs=session_dependency_handler,
@@ -129,7 +129,7 @@ class UserDetailView(DetailViewBaseGeneric):
129129

130130

131131
class UserListView(ListViewBaseGeneric):
132-
method_dependencies = {
132+
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
133133
HTTPMethod.ALL: HTTPMethodConfig(
134134
dependencies=SessionDependency,
135135
prepare_data_layer_kwargs=session_dependency_handler,

docs/python_snippets/client_generated_id/schematic_example.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from pathlib import Path
3+
from typing import ClassVar
34

45
import uvicorn
56
from fastapi import APIRouter, Depends, FastAPI
@@ -82,7 +83,7 @@ def session_dependency_handler(view: ViewBase, dto: SessionDependency) -> dict:
8283

8384

8485
class UserDetailView(DetailViewBaseGeneric):
85-
method_dependencies = {
86+
method_dependencies: ClassVar = {
8687
HTTPMethod.ALL: HTTPMethodConfig(
8788
dependencies=SessionDependency,
8889
prepare_data_layer_kwargs=session_dependency_handler,
@@ -91,7 +92,7 @@ class UserDetailView(DetailViewBaseGeneric):
9192

9293

9394
class UserListView(ListViewBaseGeneric):
94-
method_dependencies = {
95+
method_dependencies: ClassVar = {
9596
HTTPMethod.ALL: HTTPMethodConfig(
9697
dependencies=SessionDependency,
9798
prepare_data_layer_kwargs=session_dependency_handler,

docs/python_snippets/view_dependencies/main_example.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import ClassVar, Dict
4+
35
from fastapi import Depends, Header
46
from pydantic import BaseModel
57
from sqlalchemy.engine import make_url
@@ -52,7 +54,7 @@ class Config:
5254
arbitrary_types_allowed = True
5355

5456

55-
async def common_handler(view: ViewBase, dto: BaseModel) -> dict:
57+
async def common_handler(view: ViewBase, dto: SessionDependency) -> dict:
5658
return {"session": dto.session}
5759

5860

@@ -66,7 +68,7 @@ class AdminOnlyPermission(BaseModel):
6668

6769

6870
class DetailView(DetailViewBaseGeneric):
69-
method_dependencies: dict[HTTPMethod, HTTPMethodConfig] = {
71+
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
7072
HTTPMethod.ALL: HTTPMethodConfig(
7173
dependencies=SessionDependency,
7274
prepare_data_layer_kwargs=common_handler,
@@ -75,7 +77,7 @@ class DetailView(DetailViewBaseGeneric):
7577

7678

7779
class ListView(ListViewBaseGeneric):
78-
method_dependencies: dict[HTTPMethod, HTTPMethodConfig] = {
80+
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
7981
HTTPMethod.GET: HTTPMethodConfig(dependencies=AdminOnlyPermission),
8082
HTTPMethod.ALL: HTTPMethodConfig(
8183
dependencies=SessionDependency,

docs/python_snippets/view_dependencies/several_dependencies.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import ClassVar
2+
13
from fastapi import Depends
24
from pydantic import BaseModel
35

@@ -35,7 +37,7 @@ def get_handler(view: ViewBase, dto: DependencyMix):
3537

3638

3739
class DetailView(DetailViewBaseGeneric):
38-
method_dependencies = {
40+
method_dependencies: ClassVar = {
3941
HTTPMethod.ALL: HTTPMethodConfig(
4042
dependencies=CommonDependency,
4143
prepare_data_layer_kwargs=common_handler,

examples/api_for_sqlalchemy/api/views_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict
1+
from typing import ClassVar, Dict
22

33
from fastapi import Depends
44
from pydantic import BaseModel
@@ -29,7 +29,7 @@ class DetailViewBase(DetailViewBaseGeneric):
2929

3030
data_layer_cls = SqlalchemyDataLayer
3131

32-
method_dependencies = {
32+
method_dependencies: ClassVar = {
3333
HTTPMethod.ALL: HTTPMethodConfig(
3434
dependencies=SessionDependency,
3535
prepare_data_layer_kwargs=handler,
@@ -44,7 +44,7 @@ class ListViewBase(ListViewBaseGeneric):
4444

4545
data_layer_cls = SqlalchemyDataLayer
4646

47-
method_dependencies = {
47+
method_dependencies: ClassVar = {
4848
HTTPMethod.ALL: HTTPMethodConfig(
4949
dependencies=SessionDependency,
5050
prepare_data_layer_kwargs=handler,

examples/api_limited_methods.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from pathlib import Path
3-
from typing import Any, Dict
3+
from typing import Any, ClassVar, Dict
44

55
import uvicorn
66
from fastapi import APIRouter, Depends, FastAPI
@@ -82,7 +82,7 @@ def session_dependency_handler(view: ViewBase, dto: SessionDependency) -> Dict[s
8282

8383

8484
class UserDetailView(DetailViewBaseGeneric):
85-
method_dependencies = {
85+
method_dependencies: ClassVar = {
8686
HTTPMethod.ALL: HTTPMethodConfig(
8787
dependencies=SessionDependency,
8888
prepare_data_layer_kwargs=session_dependency_handler,
@@ -91,7 +91,7 @@ class UserDetailView(DetailViewBaseGeneric):
9191

9292

9393
class UserListView(ListViewBaseGeneric):
94-
method_dependencies = {
94+
method_dependencies: ClassVar = {
9595
HTTPMethod.ALL: HTTPMethodConfig(
9696
dependencies=SessionDependency,
9797
prepare_data_layer_kwargs=session_dependency_handler,

examples/api_minimal.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from pathlib import Path
3-
from typing import Any, Dict
3+
from typing import Any, ClassVar, Dict
44

55
import uvicorn
66
from fastapi import APIRouter, Depends, FastAPI
@@ -82,7 +82,7 @@ def session_dependency_handler(view: ViewBase, dto: SessionDependency) -> Dict[s
8282

8383

8484
class UserDetailView(DetailViewBaseGeneric):
85-
method_dependencies = {
85+
method_dependencies: ClassVar = {
8686
HTTPMethod.ALL: HTTPMethodConfig(
8787
dependencies=SessionDependency,
8888
prepare_data_layer_kwargs=session_dependency_handler,
@@ -91,7 +91,7 @@ class UserDetailView(DetailViewBaseGeneric):
9191

9292

9393
class UserListView(ListViewBaseGeneric):
94-
method_dependencies = {
94+
method_dependencies: ClassVar = {
9595
HTTPMethod.ALL: HTTPMethodConfig(
9696
dependencies=SessionDependency,
9797
prepare_data_layer_kwargs=session_dependency_handler,

fastapi_jsonapi/api.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
TYPE_CHECKING,
66
Any,
77
Callable,
8+
ClassVar,
89
Dict,
910
Iterable,
1011
List,
@@ -56,7 +57,7 @@ class RoutersJSONAPI:
5657
"""
5758

5859
# xxx: store in app, not in routers!
59-
all_jsonapi_routers: Dict[str, "RoutersJSONAPI"] = {}
60+
all_jsonapi_routers: ClassVar[Dict[str, "RoutersJSONAPI"]] = {}
6061
Methods = ViewMethods
6162
DEFAULT_METHODS = tuple(str(method) for method in ViewMethods)
6263

@@ -172,6 +173,8 @@ def get_endpoint_name(
172173
kind: Literal["list", "detail"],
173174
):
174175
"""
176+
Generate view name
177+
175178
:param action
176179
:param kind: list / detail
177180
:return:
@@ -458,6 +461,8 @@ async def handle_view_dependencies(
458461
method: HTTPMethod,
459462
) -> Dict[str, Any]:
460463
"""
464+
Combines all dependencies (prepared) and returns them as list
465+
461466
Consider method config is already prepared for generic views
462467
Reuse the same config for atomic operations
463468
@@ -483,6 +488,7 @@ def handle_dependencies(**dep_kwargs):
483488
def _create_get_resource_list_view(self):
484489
"""
485490
Create wrapper for GET list (get objects list)
491+
486492
:return:
487493
"""
488494

fastapi_jsonapi/atomic/atomic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def view_atomic(
3838
return result
3939
return Response(status_code=status.HTTP_204_NO_CONTENT)
4040

41-
def _register_view(self):
41+
def _register_view(self) -> None:
4242
self.router.add_api_route(
4343
path=self.url_path,
4444
endpoint=self.view_atomic,

fastapi_jsonapi/atomic/atomic_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __init__(
7575

7676
async def prepare_one_operation(self, operation: AtomicOperation):
7777
"""
78+
Prepare one atomic operation
79+
7880
:param operation:
7981
:return:
8082
"""

fastapi_jsonapi/atomic/prepared_atomic_operation.py

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ async def handle(
178178
dl: BaseDataLayer,
179179
) -> None:
180180
"""
181+
Calls view to delete object
182+
181183
Todo: fix atomic delete
182184
Deleting Resources
183185
An operation that deletes a resource

fastapi_jsonapi/atomic/schemas.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class OperationItemInSchema(BaseModel):
3939

4040
class AtomicOperationRef(BaseModel):
4141
"""
42+
This schema represents operation ref - reference to a resource
4243
4344
ref: an object that MUST contain one of the following combinations of members:
4445
type and id: to target an individual resource.
@@ -57,8 +58,7 @@ class AtomicOperationRef(BaseModel):
5758
@root_validator
5859
def validate_atomic_operation_ref(cls, values: dict):
5960
"""
60-
type is required on schema
61-
so id or lid has to be present
61+
type is required on schema, so id or lid has to be present
6262
6363
:param values:
6464
:return:
@@ -125,6 +125,8 @@ class AtomicOperation(BaseModel):
125125
@classmethod
126126
def _validate_one_of_ref_or_href(cls, values: dict):
127127
"""
128+
Make sure ref confirms spec
129+
128130
An operation object MAY contain either of the following members,
129131
but not both, to specify the target of the operation: (ref, href)
130132
@@ -152,6 +154,8 @@ def _validate_one_of_ref_or_href(cls, values: dict):
152154
@root_validator
153155
def validate_operation(cls, values: dict):
154156
"""
157+
Make sure atomic operation request conforms the spec
158+
155159
:param values:
156160
:return:
157161
"""

fastapi_jsonapi/data_layers/base.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
22
The base class of a data layer.
3+
34
If you want to create your own data layer
45
you must inherit from this base class
56
"""
@@ -30,6 +31,8 @@ def __init__(
3031
**kwargs,
3132
):
3233
"""
34+
Init
35+
3336
:param request:
3437
:param schema:
3538
:param model:
@@ -70,6 +73,8 @@ def _apply_client_generated_id(
7073
model_kwargs: dict,
7174
):
7275
"""
76+
Set custom id (if allowed)
77+
7378
:param data_create: the data validated by pydantic.
7479
:param model_kwargs: the data validated by pydantic.
7580
"""
@@ -101,6 +106,7 @@ async def create_object(self, data_create: BaseJSONAPIItemInSchema, view_kwargs:
101106
def get_object_id_field_name(self):
102107
"""
103108
compound key may cause errors
109+
104110
:return:
105111
"""
106112
return self.id_name_field
@@ -249,6 +255,7 @@ def get_related_object_query(
249255
):
250256
"""
251257
Prepare query to get related object
258+
252259
:param related_model:
253260
:param related_id_field:
254261
:param id_value:
@@ -264,6 +271,7 @@ def get_related_objects_list_query(
264271
):
265272
"""
266273
Prepare query to get related objects list
274+
267275
:param related_model:
268276
:param related_id_field:
269277
:param ids:

fastapi_jsonapi/data_layers/filtering/sqlalchemy.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
def create_filters(model: Type[TypeModel], filter_info: Union[list, dict], schema: Type[TypeSchema]):
4646
"""
4747
Apply filters from filters information to base query
48+
4849
:param model: the model of the node
4950
:param filter_info: current node filter information
5051
:param schema: the resource
@@ -91,6 +92,7 @@ def _cast_value_with_scheme(self, field_types: List[ModelField], value: Any) ->
9192
def create_filter(self, schema_field: ModelField, model_column, operator, value):
9293
"""
9394
Create sqlalchemy filter
95+
9496
:param schema_field:
9597
:param model_column: column sqlalchemy
9698
:param operator:
@@ -155,9 +157,12 @@ def create_filter(self, schema_field: ModelField, model_column, operator, value)
155157

156158
def _separate_types(self, types: List[Type]) -> Tuple[List[Type], List[Type]]:
157159
"""
158-
Separates the types into two kinds. The first are those for which
160+
Separates the types into two kinds.
161+
162+
The first are those for which
159163
there are already validators defined by pydantic - str, int, datetime
160-
and some other built-in types. The second are all other types for which
164+
and some other built-in types.
165+
The second are all other types for which
161166
the `arbitrary_types_allowed` config is applied when defining the pydantic model
162167
"""
163168
pydantic_types = [
@@ -287,7 +292,8 @@ def _relationship_filtering(self, value):
287292

288293
def _create_filters(self, type_filter: str) -> FilterAndJoins:
289294
"""
290-
Создаём фильтр or или and
295+
Create or / and filters
296+
291297
:param type_filter: 'or' или 'and'
292298
:return:
293299
"""

0 commit comments

Comments
 (0)