Skip to content

Python 3.11 dataclasses.asdict AttributeError #21

Closed
@timrid

Description

@timrid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions