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

fix readonly __args__ #275

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
2 changes: 1 addition & 1 deletion dacite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dacite.frozen_dict import FrozenDict

if sys.version_info.minor >= 8:
if sys.version_info >= (3, 8):
from functools import cached_property # type: ignore # pylint: disable=no-name-in-module
else:
# Remove when we drop support for Python<3.8
Expand Down
15 changes: 9 additions & 6 deletions dacite/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ def __concretize(
hint_origin = get_origin(hint)
hint_args = get_args(hint)
if hint_origin and hint_args and hint_origin is not Literal:
concretized = tuple(__concretize(a, generics, data_class) for a in hint_args)
if concretized != hint_args:
try:
hint.__args__ = concretized
except AttributeError as err:
raise DaciteError(f"Could not set __args__ on {hint} [original error: {err}]") from None
concrete_hint_args = tuple(__concretize(a, generics, data_class) for a in hint_args)
if concrete_hint_args != hint_args:
if sys.version_info >= (3, 9):
return hint_origin[concrete_hint_args]
# It's generally not a good practice to overwrite __args__,
# and it even has become impossible starting from python 3.13 (read-only),
# but changing the output of get_type_hints is harmless (see unit test)
# and at least this way, we get it working for python 3.8.
hint.__args__ = concrete_hint_args

return hint

Expand Down