Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect caching that caused throwing exception #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions dacite/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
T = TypeVar("T", bound=Any)


@cache
def extract_origin_collection(collection: Type) -> Type:
try:
return collection.__extra__
Expand All @@ -38,7 +37,6 @@ def extract_optional(optional: Type[Optional[T]]) -> T:
raise ValueError("can not find not-none value")


@cache
def is_generic(type_: Type) -> bool:
return hasattr(type_, "__origin__")

Expand Down Expand Up @@ -140,7 +138,6 @@ def is_instance(value: Any, type_: Type) -> bool:
return False


@cache
def is_generic_collection(type_: Type) -> bool:
if not is_generic(type_):
return False
Expand Down
21 changes: 21 additions & 0 deletions tests/core/test_subsequent_calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dataclasses import dataclass
from typing import Optional

from dacite import from_dict, Config


def test_from_dict_subsequent_calls_on_optional_type_and_union_none_does_not_raise():
@dataclass
class OptionalDataclass:
text: Optional[str]

@dataclass
class UnionDataclass:
text: str | None

d1 = {"text": None}
from_dict(OptionalDataclass, d1)

d2 = {"text": "abc"}
c = Config(cast=[int])
from_dict(UnionDataclass, d2, config=c)
5 changes: 5 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,8 @@ class FakeType:
_special = True

assert extract_generic(FakeType, defaults) == defaults


def test_optional_and_union_none_does_not_pollute_scope_via_caching():
is_generic(Optional[str])
is_generic_collection(str | None)