Skip to content

Commit

Permalink
Merge pull request #487 from yukinarit/fix-type-check-strict
Browse files Browse the repository at this point in the history
Don't implement beartype for dataclass without serde
  • Loading branch information
yukinarit committed Mar 10, 2024
2 parents f4ab380 + 11a052f commit 7a30f56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion serde/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ def deserializable_to_obj(cls: Type[T]) -> T:
try:
thisfunc = functools.partial(from_obj, named=named, reuse_instances=reuse_instances)
if is_dataclass_without_de(c):
deserialize(c)
# Do not automatically implement beartype if dataclass without serde decorator
# is passed, because it is surprising for users
# See https://github.com/yukinarit/pyserde/issues/480
deserialize(c, type_check=disabled)
res = deserializable_to_obj(c)
elif is_deserializable(c):
res = deserializable_to_obj(c)
Expand Down
5 changes: 4 additions & 1 deletion serde/se.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,10 @@ def serializable_to_obj(object: Any) -> Any:
if o is None:
return None
if is_dataclass_without_se(o):
serialize(type(o))
# Do not automatically implement beartype if dataclass without serde decorator
# is passed, because it is surprising for users
# See https://github.com/yukinarit/pyserde/issues/480
serialize(type(o), type_check=disabled)
return serializable_to_obj(o)
elif is_serializable(o):
return serializable_to_obj(o)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_type_check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
import datetime
import pathlib
from beartype.roar import BeartypeCallHintViolation
Expand All @@ -15,6 +16,7 @@
import pytest

import serde
import serde.json

from . import data

Expand Down Expand Up @@ -100,6 +102,18 @@ class C:
serde.from_dict(C, d)


def test_type_check_disabled_for_dataclass_without_serde() -> None:
@dataclass
class Foo:
value: int

f = Foo("100") # type: ignore
data = serde.json.to_json(f)
assert f == serde.json.from_json(Foo, data)

f = Foo("100") # type: ignore


def test_uncoercible() -> None:
@serde.serde(type_check=serde.coerce)
class Foo:
Expand Down

0 comments on commit 7a30f56

Please sign in to comment.