Skip to content

Commit

Permalink
update ruff linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mahenzon committed Dec 19, 2023
1 parent 71c3294 commit 49f582a
Show file tree
Hide file tree
Showing 39 changed files with 1,228 additions and 1,212 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.269"
rev: "v0.1.8"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
args: [--fix, --exit-non-zero-on-fix, --unsafe-fixes]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Create a test.py file and copy the following code into it

```python
from pathlib import Path
from typing import Any, Dict
from typing import Any, ClassVar, Dict

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


class UserDetailView(DetailViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand All @@ -129,7 +129,7 @@ class UserDetailView(DetailViewBaseGeneric):


class UserListView(ListViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand Down
5 changes: 3 additions & 2 deletions docs/python_snippets/client_generated_id/schematic_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from pathlib import Path
from typing import ClassVar

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


class UserDetailView(DetailViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand All @@ -91,7 +92,7 @@ class UserDetailView(DetailViewBaseGeneric):


class UserListView(ListViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand Down
8 changes: 5 additions & 3 deletions docs/python_snippets/view_dependencies/main_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import ClassVar, Dict

from fastapi import Depends, Header
from pydantic import BaseModel
from sqlalchemy.engine import make_url
Expand Down Expand Up @@ -52,7 +54,7 @@ class Config:
arbitrary_types_allowed = True


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


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


class DetailView(DetailViewBaseGeneric):
method_dependencies: dict[HTTPMethod, HTTPMethodConfig] = {
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=common_handler,
Expand All @@ -75,7 +77,7 @@ class DetailView(DetailViewBaseGeneric):


class ListView(ListViewBaseGeneric):
method_dependencies: dict[HTTPMethod, HTTPMethodConfig] = {
method_dependencies: ClassVar[Dict[HTTPMethod, HTTPMethodConfig]] = {
HTTPMethod.GET: HTTPMethodConfig(dependencies=AdminOnlyPermission),
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import ClassVar

from fastapi import Depends
from pydantic import BaseModel

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


class DetailView(DetailViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=CommonDependency,
prepare_data_layer_kwargs=common_handler,
Expand Down
6 changes: 3 additions & 3 deletions examples/api_for_sqlalchemy/api/views_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict
from typing import ClassVar, Dict

from fastapi import Depends
from pydantic import BaseModel
Expand Down Expand Up @@ -29,7 +29,7 @@ class DetailViewBase(DetailViewBaseGeneric):

data_layer_cls = SqlalchemyDataLayer

method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=handler,
Expand All @@ -44,7 +44,7 @@ class ListViewBase(ListViewBaseGeneric):

data_layer_cls = SqlalchemyDataLayer

method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=handler,
Expand Down
6 changes: 3 additions & 3 deletions examples/api_limited_methods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from pathlib import Path
from typing import Any, Dict
from typing import Any, ClassVar, Dict

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


class UserDetailView(DetailViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand All @@ -91,7 +91,7 @@ class UserDetailView(DetailViewBaseGeneric):


class UserListView(ListViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand Down
6 changes: 3 additions & 3 deletions examples/api_minimal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from pathlib import Path
from typing import Any, Dict
from typing import Any, ClassVar, Dict

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


class UserDetailView(DetailViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand All @@ -91,7 +91,7 @@ class UserDetailView(DetailViewBaseGeneric):


class UserListView(ListViewBaseGeneric):
method_dependencies = {
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=session_dependency_handler,
Expand Down
8 changes: 7 additions & 1 deletion fastapi_jsonapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Dict,
Iterable,
List,
Expand Down Expand Up @@ -56,7 +57,7 @@ class RoutersJSONAPI:
"""

# xxx: store in app, not in routers!
all_jsonapi_routers: Dict[str, "RoutersJSONAPI"] = {}
all_jsonapi_routers: ClassVar[Dict[str, "RoutersJSONAPI"]] = {}
Methods = ViewMethods
DEFAULT_METHODS = tuple(str(method) for method in ViewMethods)

Expand Down Expand Up @@ -172,6 +173,8 @@ def get_endpoint_name(
kind: Literal["list", "detail"],
):
"""
Generate view name
:param action
:param kind: list / detail
:return:
Expand Down Expand Up @@ -458,6 +461,8 @@ async def handle_view_dependencies(
method: HTTPMethod,
) -> Dict[str, Any]:
"""
Combines all dependencies (prepared) and returns them as list
Consider method config is already prepared for generic views
Reuse the same config for atomic operations
Expand All @@ -483,6 +488,7 @@ def handle_dependencies(**dep_kwargs):
def _create_get_resource_list_view(self):
"""
Create wrapper for GET list (get objects list)
:return:
"""

Expand Down
2 changes: 1 addition & 1 deletion fastapi_jsonapi/atomic/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def view_atomic(
return result
return Response(status_code=status.HTTP_204_NO_CONTENT)

def _register_view(self):
def _register_view(self) -> None:
self.router.add_api_route(
path=self.url_path,
endpoint=self.view_atomic,
Expand Down
2 changes: 2 additions & 0 deletions fastapi_jsonapi/atomic/atomic_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(

async def prepare_one_operation(self, operation: AtomicOperation):
"""
Prepare one atomic operation
:param operation:
:return:
"""
Expand Down
2 changes: 2 additions & 0 deletions fastapi_jsonapi/atomic/prepared_atomic_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ async def handle(
dl: BaseDataLayer,
) -> None:
"""
Calls view to delete object
Todo: fix atomic delete
Deleting Resources
An operation that deletes a resource
Expand Down
8 changes: 6 additions & 2 deletions fastapi_jsonapi/atomic/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class OperationItemInSchema(BaseModel):

class AtomicOperationRef(BaseModel):
"""
This schema represents operation ref - reference to a resource
ref: an object that MUST contain one of the following combinations of members:
type and id: to target an individual resource.
Expand All @@ -57,8 +58,7 @@ class AtomicOperationRef(BaseModel):
@root_validator
def validate_atomic_operation_ref(cls, values: dict):
"""
type is required on schema
so id or lid has to be present
type is required on schema, so id or lid has to be present
:param values:
:return:
Expand Down Expand Up @@ -125,6 +125,8 @@ class AtomicOperation(BaseModel):
@classmethod
def _validate_one_of_ref_or_href(cls, values: dict):
"""
Make sure ref confirms spec
An operation object MAY contain either of the following members,
but not both, to specify the target of the operation: (ref, href)
Expand Down Expand Up @@ -152,6 +154,8 @@ def _validate_one_of_ref_or_href(cls, values: dict):
@root_validator
def validate_operation(cls, values: dict):
"""
Make sure atomic operation request conforms the spec
:param values:
:return:
"""
Expand Down
8 changes: 8 additions & 0 deletions fastapi_jsonapi/data_layers/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
The base class of a data layer.
If you want to create your own data layer
you must inherit from this base class
"""
Expand Down Expand Up @@ -30,6 +31,8 @@ def __init__(
**kwargs,
):
"""
Init
:param request:
:param schema:
:param model:
Expand Down Expand Up @@ -70,6 +73,8 @@ def _apply_client_generated_id(
model_kwargs: dict,
):
"""
Set custom id (if allowed)
:param data_create: the data validated by pydantic.
:param model_kwargs: the data validated by pydantic.
"""
Expand Down Expand Up @@ -101,6 +106,7 @@ async def create_object(self, data_create: BaseJSONAPIItemInSchema, view_kwargs:
def get_object_id_field_name(self):
"""
compound key may cause errors
:return:
"""
return self.id_name_field
Expand Down Expand Up @@ -249,6 +255,7 @@ def get_related_object_query(
):
"""
Prepare query to get related object
:param related_model:
:param related_id_field:
:param id_value:
Expand All @@ -264,6 +271,7 @@ def get_related_objects_list_query(
):
"""
Prepare query to get related objects list
:param related_model:
:param related_id_field:
:param ids:
Expand Down
12 changes: 9 additions & 3 deletions fastapi_jsonapi/data_layers/filtering/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
def create_filters(model: Type[TypeModel], filter_info: Union[list, dict], schema: Type[TypeSchema]):
"""
Apply filters from filters information to base query
:param model: the model of the node
:param filter_info: current node filter information
:param schema: the resource
Expand Down Expand Up @@ -91,6 +92,7 @@ def _cast_value_with_scheme(self, field_types: List[ModelField], value: Any) ->
def create_filter(self, schema_field: ModelField, model_column, operator, value):
"""
Create sqlalchemy filter
:param schema_field:
:param model_column: column sqlalchemy
:param operator:
Expand Down Expand Up @@ -155,9 +157,12 @@ def create_filter(self, schema_field: ModelField, model_column, operator, value)

def _separate_types(self, types: List[Type]) -> Tuple[List[Type], List[Type]]:
"""
Separates the types into two kinds. The first are those for which
Separates the types into two kinds.
The first are those for which
there are already validators defined by pydantic - str, int, datetime
and some other built-in types. The second are all other types for which
and some other built-in types.
The second are all other types for which
the `arbitrary_types_allowed` config is applied when defining the pydantic model
"""
pydantic_types = [
Expand Down Expand Up @@ -287,7 +292,8 @@ def _relationship_filtering(self, value):

def _create_filters(self, type_filter: str) -> FilterAndJoins:
"""
Создаём фильтр or или and
Create or / and filters
:param type_filter: 'or' или 'and'
:return:
"""
Expand Down
Loading

0 comments on commit 49f582a

Please sign in to comment.