Skip to content

Commit

Permalink
check not type in serialization (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Nov 6, 2023
1 parent 1cf1c75 commit cb47b96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/serializers/ob_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ fn is_dataclass(op_value: Option<&PyAny>) -> bool {
value
.hasattr(intern!(value.py(), "__dataclass_fields__"))
.unwrap_or(false)
&& !value.is_instance_of::<PyType>()
} else {
false
}
Expand All @@ -343,6 +344,7 @@ fn is_pydantic_serializable(op_value: Option<&PyAny>) -> bool {
value
.hasattr(intern!(value.py(), "__pydantic_serializer__"))
.unwrap_or(false)
&& !value.is_instance_of::<PyType>()
} else {
false
}
Expand Down
8 changes: 8 additions & 0 deletions tests/serializers/test_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ class Foo:
assert s.to_python(Foo(a='hello', b=b'more'), exclude={'a'}) == IsStrictDict()
assert s.to_json(Foo(a='hello', b=b'more'), exclude={'a'}) == b'{}'

assert s.to_python(Foo) == Foo
with pytest.raises(PydanticSerializationError, match=r"Unable to serialize unknown type: <class 'type'>"):
s.to_python(Foo, mode='json')
with pytest.raises(PydanticSerializationError, match=r"Unable to serialize unknown type: <class 'type'>"):
s.to_json(Foo)
assert s.to_python(Foo, mode='json', fallback=lambda x: x.__name__) == 'Foo'
assert s.to_json(Foo, fallback=lambda x: x.__name__) == b'"Foo"'


def test_dataclass_classvar(any_serializer):
@dataclasses.dataclass
Expand Down

0 comments on commit cb47b96

Please sign in to comment.