Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoV committed Apr 27, 2024
1 parent 013739a commit d2ea1af
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions fastapi_jsonapi/schema_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""JSON API schemas builder class."""
from dataclasses import dataclass
from functools import lru_cache, partial
from functools import lru_cache
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -126,22 +126,9 @@ def __init__(self, resource_type: str, max_cache_size: int = 0):

def _init_cache(self, max_cache_size: int):
# TODO: remove crutch
@lru_cache(maxsize=max_cache_size)
def _get_info_from_schema_for_building_cached(
self: SchemaBuilder,
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,
)

self._get_info_from_schema_for_building_cached = partial(_get_info_from_schema_for_building_cached, self)
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 @@ -283,7 +270,12 @@ def _get_info_from_schema_for_building_cached(
includes: str,
non_optional_relationships: bool,
):
"""Just for annotation purpose. See _init_cache"""
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,
Expand Down

0 comments on commit d2ea1af

Please sign in to comment.