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

Python 3.11 dataclasses.asdict AttributeError #21

Closed
timrid opened this issue May 9, 2023 · 0 comments · Fixed by #22
Closed

Python 3.11 dataclasses.asdict AttributeError #21

timrid opened this issue May 9, 2023 · 0 comments · Fixed by #22

Comments

@timrid
Copy link
Owner

timrid commented May 9, 2023

The following example is not working in python 3.11:

import construct_typed as cst
import dataclasses


class TestEnum(cst.EnumBase):
    one = 1
    two = 2
    four = 4
    eight = 8


@dataclasses.dataclass
class SomeDataclass:
    a: TestEnum


dc = SomeDataclass(TestEnum.one)
dc_dict = dataclasses.asdict(dc)  # <--- this is working

dc = SomeDataclass(TestEnum(5))
dc_dict = dataclasses.asdict(dc)  # <--- Error on Python 3.11

This runs with Python 3.8-3.10 but generates the following error on Python 3.11:

Traceback (most recent call last):
  File "...\construct-typing\example.py", line 21, in <module>
    dc_dict = dataclasses.asdict(dc)  # <--- Error on Python 3.11
              ^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\dataclasses.py", line 1274, in asdict
    return _asdict_inner(obj, dict_factory)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\dataclasses.py", line 1281, in _asdict_inner
    value = _asdict_inner(getattr(obj, f.name), dict_factory)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\dataclasses.py", line 1315, in _asdict_inner
    return copy.deepcopy(obj)
           ^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python311\Lib\copy.py", line 265, in _reconstruct
    y = func(*args)
        ^^^^^^^^^^^
  File "...\Python311\Lib\enum.py", line 789, in __getattr__
    raise AttributeError(name) from None
AttributeError: 5

It seems, that copy.deepcopy of the EnumBase is not working. Probably it has to do with the following change: python/cpython#26658

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant