-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix serializing complex values in enums (#1524)
- Loading branch information
Showing
4 changed files
with
62 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from enum import Enum | ||
|
||
from pydantic_core import SchemaSerializer, core_schema | ||
|
||
|
||
# serializing enum calls methods in serializers::infer | ||
def test_infer_to_python(): | ||
class MyEnum(Enum): | ||
complex_ = complex(1, 2) | ||
|
||
v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()))) | ||
assert v.to_python(MyEnum.complex_, mode='json') == '1+2j' | ||
|
||
|
||
def test_infer_serialize(): | ||
class MyEnum(Enum): | ||
complex_ = complex(1, 2) | ||
|
||
v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()))) | ||
assert v.to_json(MyEnum.complex_) == b'"1+2j"' | ||
|
||
|
||
def test_infer_json_key(): | ||
class MyEnum(Enum): | ||
complex_ = {complex(1, 2): 1} | ||
|
||
v = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values()))) | ||
assert v.to_json(MyEnum.complex_) == b'{"1+2j":1}' |