Skip to content

Commit

Permalink
Fix ByteSize error type change (#8681)
Browse files Browse the repository at this point in the history
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
  • Loading branch information
sydney-runkle and dmontagu committed Jan 30, 2024
1 parent 0404872 commit c3ec15c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pydantic/types.py
Expand Up @@ -1787,7 +1787,9 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
[
core_schema.str_schema(pattern=cls.byte_string_pattern),
core_schema.int_schema(ge=0),
]
],
custom_error_type='byte_size',
custom_error_message='could not parse value and unit from byte string',
),
serialization=core_schema.plain_serializer_function_ser_schema(
int, return_schema=core_schema.int_schema(ge=0)
Expand Down
21 changes: 19 additions & 2 deletions tests/test_types.py
Expand Up @@ -4481,11 +4481,28 @@ def test_bytesize_raises():
class Model(BaseModel):
size: ByteSize

with pytest.raises(ValidationError, match='should match'):
with pytest.raises(ValidationError, match='parse value') as exc_info:
Model(size='d1MB')
assert exc_info.value.errors(include_url=False) == [
{
'input': 'd1MB',
'loc': ('size',),
'msg': 'could not parse value and unit from byte string',
'type': 'byte_size',
}
]

with pytest.raises(ValidationError, match='byte unit'):
with pytest.raises(ValidationError, match='byte unit') as exc_info:
Model(size='1LiB')
assert exc_info.value.errors(include_url=False) == [
{
'ctx': {'unit': 'LiB'},
'input': '1LiB',
'loc': ('size',),
'msg': 'could not interpret byte unit: LiB',
'type': 'byte_size_unit',
}
]

# 1Gi is not a valid unit unlike 1G
with pytest.raises(ValidationError, match='byte unit'):
Expand Down

0 comments on commit c3ec15c

Please sign in to comment.