I tried the following code:
from dataclasses import asdict, dataclass
@dataclass
class DC:
a: int
b: int
c = DC(a=10, b=5)
dc = asdict(c)
print(dc)
On Python 3.10 it works as expected, printing a dictionary as expected. When I try the same with Python 3.11 I get the following error:
File "C:\Sources\IAA\python-utils\tests\test_dict.py", line 11, in <module>
dc = asdict(c)
^^^^^^^^^
File "C:\Python\311\Lib\dataclasses.py", line 1272, in asdict
if not _is_dataclass_instance(obj):
^^^^^^^^^^^^^^^^^^^^^^
NameError: name '_is_dataclass_instance' is not defined. Did you mean:
'config_is_dataclass_instance'?
I checked the Python 3.11 documentation, dataclasses.asdict
is there and should work. Am I doing something wrong, or is this a Python 3.11.1 bug?
The solution for Python 3.11.1 is to add the following lines to my module:
import dataclasses
dataclasses._is_dataclass_instance = dataclasses.config_is_dataclass_instance
But it's really not a good solution.