Skip to content

Commit

Permalink
Make Latitude and Longitude evaluated (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Aug 25, 2023
1 parent 7712714 commit 874bd77
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions pydantic_extra_types/coordinate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, ClassVar, Mapping, Tuple, Union
from typing import Any, ClassVar, Tuple, Type, Union

from pydantic import GetCoreSchemaHandler
from pydantic._internal import _repr
Expand All @@ -15,7 +13,7 @@ class Latitude(float):
max: ClassVar[float] = 90.00

@classmethod
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
def __get_pydantic_core_schema__(cls, source: Type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
return core_schema.float_schema(ge=cls.min, le=cls.max)


Expand All @@ -24,19 +22,19 @@ class Longitude(float):
max: ClassVar[float] = 180.00

@classmethod
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
def __get_pydantic_core_schema__(cls, source: Type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
return core_schema.float_schema(ge=cls.min, le=cls.max)


@dataclass
class Coordinate(_repr.Representation):
_NULL_ISLAND: ClassVar[tuple[float, float]] = (0.0, 0.0)
_NULL_ISLAND: ClassVar[Tuple[float, float]] = (0.0, 0.0)

latitude: Latitude
longitude: Longitude

@classmethod
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
def __get_pydantic_core_schema__(cls, source: Type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
schema_chain = [
core_schema.no_info_wrap_validator_function(cls._parse_str, core_schema.str_schema()),
core_schema.no_info_wrap_validator_function(
Expand All @@ -47,11 +45,10 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
]

chain_length = len(schema_chain)
chain_schemas: list[Mapping[str, Any]] = [
core_schema.chain_schema(schema_chain[x:]) for x in range(chain_length - 1, -1, -1)
]

return core_schema.no_info_wrap_validator_function(cls._parse_args, core_schema.union_schema(chain_schemas))
chain_schemas = [core_schema.chain_schema(schema_chain[x:]) for x in range(chain_length - 1, -1, -1)]
return core_schema.no_info_wrap_validator_function(
cls._parse_args, core_schema.union_schema(chain_schemas) # type: ignore[arg-type]
)

@classmethod
def _parse_args(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> Any:
Expand Down

0 comments on commit 874bd77

Please sign in to comment.