8000 Expose AnySerializer via core schema by BoxyUwU · Pull Request #1394 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Expose AnySerializer via core sc 8000 hema #1394

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

Merged
merged 1 commit into from
Aug 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def field_name(self) -> str | None:
'multi-host-url',
'json',
'uuid',
'any',
]


Expand Down
29 changes: 29 additions & 0 deletions tests/serializers/test_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,32 @@ def test_ser_json_inf_nan_with_list_of_any() -> None:
assert isnan(s.to_python([nan])[0])
assert s.to_python([nan], mode='json')[0] is None
assert s.to_json([nan]) == b'[null]'


def test_simple_any_ser_schema_repr():
assert (
plain_repr(SchemaSerializer(core_schema.simple_ser_schema('any')))
== 'SchemaSerializer(serializer=Any(AnySerializer),definitions=[])'
)


def test_simple_any_ser_schema():
import operator

class MyEnum(Enum):
A = (1,)
B = (2,)

v = SchemaSerializer(
core_schema.no_info_after_validator_function(
operator.attrgetter('value'),
core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())),
serialization=core_schema.simple_ser_schema('any'),
),
)

assert v.to_python({MyEnum.A: 'x'}) == {MyEnum.A: 'x'}
assert v.to_python({MyEnum.A: 'x'}, mode='json') == {'1': 'x'}
assert v.to_json({MyEnum.A: 'x'}) == b'{"1":"x"}'
assert v.to_python(1) == 1
assert v.to_json(1) == b'1'
Loading
0