Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ruff black linters #90

Open
wants to merge 7 commits into
base: feature/massive-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ repos:
- id: requirements-txt-fixer

- repo: https://github.com/psf/black
rev: "23.3.0"
rev: "24.4.2"
hooks:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.1.8"
rev: "v0.4.10"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --unsafe-fixes]
14 changes: 14 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
Changelog
#########

**2.8.0**
*********

Performance improvements
========================

* added simple cache to SchemaBuilder by `@CosmoV`_ in `#87 <https://github.com/mts-ai/FastAPI-JSONAPI/pull/87>`_

Authors
"""""""

* `@CosmoV`_


**2.7.0**
*********

Expand Down
1 change: 1 addition & 0 deletions examples/api_for_sqlalchemy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

In module placed db initialization functions, app factory.
"""

import sys
from pathlib import Path

Expand Down
1 change: 0 additions & 1 deletion examples/api_for_sqlalchemy/models/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""schemas package."""


from .child import (
ChildInSchema,
ChildPatchSchema,
Expand Down
1 change: 1 addition & 0 deletions examples/api_for_sqlalchemy/models/schemas/post_comment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Post Comment schemas module."""

from datetime import datetime
from typing import TYPE_CHECKING

Expand Down
1 change: 1 addition & 0 deletions examples/api_for_sqlalchemy/models/schemas/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""User schemas module."""

from __future__ import annotations

from datetime import datetime
Expand Down
1 change: 1 addition & 0 deletions examples/api_for_sqlalchemy/models/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""User model."""

from __future__ import annotations

from sqlalchemy import Column, Integer, String
Expand Down
1 change: 1 addition & 0 deletions examples/api_for_sqlalchemy/models/user_bio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""User Bio model."""

from typing import Dict, List

from sqlalchemy import JSON, Column, ForeignKey, Integer, String
Expand Down
1 change: 1 addition & 0 deletions examples/api_for_tortoise_orm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

In module placed db initialization functions, app factory.
"""

import sys
from pathlib import Path

Expand Down
1 change: 0 additions & 1 deletion examples/api_for_tortoise_orm/models/pydantic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""W-mount schemas package."""


from .user import (
UserPatchSchema,
UserSchema,
Expand Down
1 change: 0 additions & 1 deletion examples/api_for_tortoise_orm/models/tortoise/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""User model."""


from tortoise import (
fields,
models,
Expand Down
2 changes: 1 addition & 1 deletion fastapi_jsonapi/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0
2.8.0
1 change: 1 addition & 0 deletions fastapi_jsonapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""JSON API utils package."""

from pathlib import Path

from fastapi import FastAPI
Expand Down
4 changes: 3 additions & 1 deletion fastapi_jsonapi/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""JSON API router class."""

from enum import Enum, auto
from inspect import Parameter, Signature, signature
from typing import (
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(
pagination_default_offset: Optional[int] = None,
pagination_default_limit: Optional[int] = None,
methods: Iterable[str] = (),
max_cache_size: int = 0,
) -> None:
"""
Initialize router items.
Expand Down Expand Up @@ -127,7 +129,7 @@ def __init__(
self.pagination_default_number: Optional[int] = pagination_default_number
self.pagination_default_offset: Optional[int] = pagination_default_offset
self.pagination_default_limit: Optional[int] = pagination_default_limit
self.schema_builder = SchemaBuilder(resource_type=resource_type)
self.schema_builder = SchemaBuilder(resource_type=resource_type, max_cache_size=max_cache_size)

dto = self.schema_builder.create_schemas(
schema=schema,
Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/data_layers/filtering/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper to create sqlalchemy filters according to filter querystring parameter"""

import inspect
import logging
from collections.abc import Sequence
Expand Down
4 changes: 2 additions & 2 deletions fastapi_jsonapi/data_layers/filtering/tortoise_operation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Previously used: '__'
"""

from typing import Protocol


Expand Down Expand Up @@ -292,8 +293,7 @@ def type_op_ilike(field_name: str, type_op: str) -> str:


class ProcessTypeOperationFieldName(Protocol):
def __call__(self, field_name: str, type_op: str) -> str:
...
def __call__(self, field_name: str, type_op: str) -> str: ...


filters_dict: dict[str, ProcessTypeOperationFieldName] = {
Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/data_layers/sorting/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper to create sqlalchemy sortings according to filter querystring parameter"""

from typing import Any, List, Tuple, Type, Union

from pydantic.fields import ModelField
Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/data_layers/sqla_orm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module is a CRUD interface between resource managers and the sqlalchemy ORM"""

import logging
from typing import TYPE_CHECKING, Any, Iterable, List, Literal, Optional, Tuple, Type, Union

Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/querystring.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper to deal with querystring parameters according to jsonapi specification."""

from collections import defaultdict
from functools import cached_property
from typing import (
Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Pydantic (for FastAPI).
"""

from typing import (
TYPE_CHECKING,
Dict,
Expand Down
48 changes: 46 additions & 2 deletions fastapi_jsonapi/schema_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""JSON API schemas builder class."""

from dataclasses import dataclass
from functools import lru_cache
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -122,8 +124,16 @@ class SchemaBuilder:
def __init__(
self,
resource_type: str,
max_cache_size: int = 0,
):
self._resource_type = resource_type
self._init_cache(max_cache_size)

def _init_cache(self, max_cache_size: int):
# TODO: remove crutch
self._get_info_from_schema_for_building_cached = lru_cache(maxsize=max_cache_size)(
self._get_info_from_schema_for_building_cached,
)

def _create_schemas_objects_list(self, schema: Type[BaseModel]) -> Type[JSONAPIResultListSchema]:
object_jsonapi_list_schema, list_jsonapi_schema = self.build_list_schemas(schema)
Expand Down Expand Up @@ -187,7 +197,7 @@ def build_schema_in(
) -> Tuple[Type[BaseJSONAPIDataInSchema], Type[BaseJSONAPIItemInSchema]]:
base_schema_name = schema_in.__name__.removesuffix("Schema") + schema_name_suffix

dto = self._get_info_from_schema_for_building(
dto = self._get_info_from_schema_for_building_wrapper(
base_name=base_schema_name,
schema=schema_in,
non_optional_relationships=non_optional_relationships,
Expand Down Expand Up @@ -258,6 +268,40 @@ def build_list_schemas(
includes=includes,
)

def _get_info_from_schema_for_building_cached(
self,
base_name: str,
schema: Type[BaseModel],
includes: Iterable[str],
non_optional_relationships: bool,
):
return self._get_info_from_schema_for_building(
base_name=base_name,
schema=schema,
includes=includes,
non_optional_relationships=non_optional_relationships,
)

def _get_info_from_schema_for_building_wrapper(
self,
base_name: str,
schema: Type[BaseModel],
includes: Iterable[str] = not_passed,
non_optional_relationships: bool = False,
):
"""
Wrapper function for return cached schema result
"""
if includes is not not_passed:
includes = tuple(includes)

return self._get_info_from_schema_for_building_cached(
base_name=base_name,
schema=schema,
includes=includes,
non_optional_relationships=non_optional_relationships,
)

def _get_info_from_schema_for_building(
self,
base_name: str,
Expand Down Expand Up @@ -494,7 +538,7 @@ def create_jsonapi_object_schemas(
if includes is not not_passed:
includes = set(includes)

dto = self._get_info_from_schema_for_building(
dto = self._get_info_from_schema_for_building_wrapper(
base_name=base_name,
schema=schema,
includes=includes,
Expand Down
1 change: 1 addition & 0 deletions fastapi_jsonapi/signature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Functions for extracting and updating signatures."""

import inspect
import logging
from enum import Enum
Expand Down
Loading