Skip to content

Commit

Permalink
Don't implement beartype for dataclass without serde
Browse files Browse the repository at this point in the history
Closes #480
  • Loading branch information
yukinarit committed Mar 9, 2024
1 parent f4ab380 commit 11a052f
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 11a052f

Please sign in to comment.