Skip to content

Commit

Permalink
Use Python 3.8 compatible types
Browse files Browse the repository at this point in the history
  • Loading branch information
ELC committed Jan 28, 2024
1 parent 938c086 commit 3757773
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
24 changes: 13 additions & 11 deletions fastapi_hypermodel/siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
Any,
Callable,
Dict,
List,
Mapping,
Sequence,
Type,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -55,7 +57,7 @@ class SirenLinkType(SirenBase):

@field_validator("rel", "href")
@classmethod
def mandatory(cls: type[Self], value: Union[str, None]) -> str:
def mandatory(cls: Type[Self], value: Union[str, None]) -> str:
if not value:
error_message = "Field rel and href are mandatory"
raise ValueError(error_message)
Expand Down Expand Up @@ -138,15 +140,15 @@ class SirenFieldType(SirenBase):
value: Union[Any, None] = None

@classmethod
def from_field_info(cls: type[Self], name: str, field_info: FieldInfo) -> Self:
def from_field_info(cls: Type[Self], name: str, field_info: FieldInfo) -> Self:
return cls.model_validate({
"name": name,
"type": cls.parse_type(field_info.annotation),
"value": field_info.default,
})

@staticmethod
def parse_type(python_type: Union[type[Any], None]) -> str:
def parse_type(python_type: Union[Type[Any], None]) -> str:
type_repr = repr(python_type)

text_types = ("str",)
Expand All @@ -170,7 +172,7 @@ class SirenActionType(SirenBase):

@field_validator("name", "href")
@classmethod
def mandatory(cls: type[Self], value: Union[str, None]) -> str:
def mandatory(cls: Type[Self], value: Union[str, None]) -> str:
if not value:
error_message = f"Field name and href are mandatory, {value}"
raise ValueError(error_message)
Expand Down Expand Up @@ -233,7 +235,7 @@ def _get_uri_path(

def _prepopulate_fields(
self: Self, fields: Sequence[SirenFieldType], values: Mapping[str, Any]
) -> list[SirenFieldType]:
) -> List[SirenFieldType]:
if not self._populate_fields:
return list(fields)

Expand All @@ -244,7 +246,7 @@ def _prepopulate_fields(

def _compute_fields(
self: Self, route: Route, values: Mapping[str, Any]
) -> list[SirenFieldType]:
) -> List[SirenFieldType]:
if not isinstance(route, APIRoute): # pragma: no cover
route.body_field = "" # type: ignore
route = cast(APIRoute, route)
Expand Down Expand Up @@ -317,7 +319,7 @@ class SirenEmbeddedType(SirenEntityType):


class SirenHyperModel(HyperModel):
properties: dict[str, Any] = Field(default_factory=dict)
properties: Dict[str, Any] = Field(default_factory=dict)
entities: Sequence[Union[SirenEmbeddedType, SirenLinkType]] = Field(
default_factory=list
)
Expand All @@ -329,7 +331,7 @@ class SirenHyperModel(HyperModel):

@model_validator(mode="after")
def add_hypermodels_to_entities(self: Self) -> Self:
entities: list[Union[SirenEmbeddedType, SirenLinkType]] = []
entities: List[Union[SirenEmbeddedType, SirenLinkType]] = []
for name, field in self:
alias = self.model_fields[name].alias or name

Expand Down Expand Up @@ -396,7 +398,7 @@ def add_properties(self: Self) -> Self:
@model_validator(mode="after")
def add_links(self: Self) -> Self:
links_key = "links"
validated_links: list[SirenLinkFor] = []
validated_links: List[SirenLinkFor] = []
for name, value in self:
alias = self.model_fields[name].alias or name

Expand Down Expand Up @@ -440,8 +442,8 @@ def add_actions(self: Self) -> Self:

def _validate_factory(
self: Self, elements: Sequence[T], properties: Mapping[str, str]
) -> list[T]:
validated_elements: list[T] = []
) -> List[T]:
validated_elements: List[T] = []
for element_factory in elements:
element = element_factory(self._app, properties)
if not element:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ignore = [
"ANN401", # Ignore Anys
"ISC001", # Disable for compatibility with ruff-format
"UP007", # Not compatible with Python 3.8
"UP006", # Not compatible with Python 3.8
]
extend-exclude = ["tests", "*_schema.py"]
target-version = "py38"
Expand Down

0 comments on commit 3757773

Please sign in to comment.