Skip to content

Commit cb47b96

Browse files
authored
check not type in serialization (#962)
1 parent 1cf1c75 commit cb47b96

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/serializers/ob_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ fn is_dataclass(op_value: Option<&PyAny>) -> bool {
333333
value
334334
.hasattr(intern!(value.py(), "__dataclass_fields__"))
335335
.unwrap_or(false)
336+
&& !value.is_instance_of::<PyType>()
336337
} else {
337338
false
338339
}
@@ -343,6 +344,7 @@ fn is_pydantic_serializable(op_value: Option<&PyAny>) -> bool {
343344
value
344345
.hasattr(intern!(value.py(), "__pydantic_serializer__"))
345346
.unwrap_or(false)
347+
&& !value.is_instance_of::<PyType>()
346348
} else {
347349
false
348350
}

tests/serializers/test_any.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@ class Foo:
505505
assert s.to_python(Foo(a='hello', b=b'more'), exclude={'a'}) == IsStrictDict()
506506
assert s.to_json(Foo(a='hello', b=b'more'), exclude={'a'}) == b'{}'
507507

508+
assert s.to_python(Foo) == Foo
509+
with pytest.raises(PydanticSerializationError, match=r"Unable to serialize unknown type: <class 'type'>"):
510+
s.to_python(Foo, mode='json')
511+
with pytest.raises(PydanticSerializationError, match=r"Unable to serialize unknown type: <class 'type'>"):
512+
s.to_json(Foo)
513+
assert s.to_python(Foo, mode='json', fallback=lambda x: x.__name__) == 'Foo'
514+
assert s.to_json(Foo, fallback=lambda x: x.__name__) == b'"Foo"'
515+
508516

509517
def test_dataclass_classvar(any_serializer):
510518
@dataclasses.dataclass

0 commit comments

Comments
 (0)