Skip to content

Commit efe1411

Browse files
committed
typing …
1 parent f9c93d1 commit efe1411

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

pydantic_extra_types/epoch.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import datetime
4-
from typing import Any, Callable, Optional
4+
from typing import Any, Callable
55

66
import pydantic_core.core_schema
77
from pydantic import GetJsonSchemaHandler
@@ -13,7 +13,6 @@
1313

1414
class _Base(datetime.datetime):
1515
TYPE: str = ''
16-
CLS: Optional[Callable[[Any], Any]]
1716
SCHEMA: pydantic_core.core_schema.CoreSchema
1817

1918
@classmethod
@@ -28,28 +27,36 @@ def __get_pydantic_json_schema__(
2827
def __get_pydantic_core_schema__(
2928
cls, source: type[Any], handler: Callable[[Any], CoreSchema]
3029
) -> core_schema.CoreSchema:
31-
def f(value: Any, serializer: Callable[[datetime.datetime], float]) -> float:
32-
ts = value.timestamp()
33-
return serializer(cls.CLS(ts) if cls.CLS is not None else ts)
34-
3530
return core_schema.with_info_after_validator_function(
3631
cls._validate,
3732
cls.SCHEMA,
38-
serialization=core_schema.wrap_serializer_function_ser_schema(f, return_schema=cls.SCHEMA),
33+
serialization=core_schema.wrap_serializer_function_ser_schema(cls._f, return_schema=cls.SCHEMA),
3934
)
4035

4136
@classmethod
4237
def _validate(cls, __input_value: Any, _: Any) -> datetime.datetime:
4338
return EPOCH + datetime.timedelta(seconds=__input_value)
4439

40+
@classmethod
41+
def _f(cls, value: Any, serializer: Callable[[Any], Any]) -> Any:
42+
pass
43+
4544

4645
class Number(_Base):
4746
TYPE = 'number'
48-
CLS = None
4947
SCHEMA = core_schema.float_schema()
5048

49+
@classmethod
50+
def _f(cls, value: Any, serializer: Callable[[float], float]) -> float:
51+
ts = value.timestamp()
52+
return serializer(ts)
53+
5154

5255
class Integer(_Base):
5356
TYPE = 'integer'
54-
CLS = int
5557
SCHEMA = core_schema.int_schema()
58+
59+
@classmethod
60+
def _f(cls, value: Any, serializer: Callable[[int], int]) -> int:
61+
ts = value.timestamp()
62+
return serializer(int(ts))

0 commit comments

Comments
 (0)