Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ByteSize error type change #8681

Merged
merged 4 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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