Skip to content

Commit

Permalink
refactor: serialization function
Browse files Browse the repository at this point in the history
  • Loading branch information
geospackle committed Jan 12, 2024
1 parent 803766a commit 94b4666
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions pydantic/types.py
Expand Up @@ -1768,7 +1768,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
core_schema.int_schema(ge=0),
]
),
serialization=core_schema.plain_serializer_function_ser_schema(function=cls._serialize),
serialization=core_schema.plain_serializer_function_ser_schema(lambda v: int(v)),
)

@classmethod
Expand All @@ -1793,28 +1793,6 @@ def _validate(cls, __input_value: Any, _: core_schema.ValidationInfo) -> ByteSiz

return cls(int(float(scalar) * unit_mult))

@staticmethod
def _serialize(__input_value: Any) -> int:
try:
return int(__input_value)
except ValueError:
pass

str_match = byte_string_re.match(str(__input_value))
if str_match is None:
raise PydanticCustomError('byte_size', 'could not parse value and unit from byte string')

scalar, unit = str_match.groups()
if unit is None:
unit = 'b'

try:
unit_mult = BYTE_SIZES[unit.lower()]
except KeyError:
raise PydanticCustomError('byte_size_unit', 'could not interpret byte unit: {unit}', {'unit': unit})

return int(float(scalar) * unit_mult)

def human_readable(self, decimal: bool = False) -> str:
"""Converts a byte size to a human readable string.
Expand Down

0 comments on commit 94b4666

Please sign in to comment.