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

asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " #106113

Closed
johentsch opened this issue Jun 26, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@johentsch
Copy link

Bug report

Minimal working example:

from dataclasses import dataclass, field, asdict
from typing import DefaultDict
from collections import defaultdict

def default_list_dict():
    return defaultdict(list)

@dataclass
class Data:
    metadata: DefaultDict = field(default_factory=default_list_dict)
    
D = Data() # => Data(metadata=defaultdict(<class 'list'>, {}))
asdict(D)
  • Expected output: {'metadata': defaultdict(list, {})}
  • Got instead: TypeError: first argument must be callable or None
Display traceback
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 13
     10     metadata: DefaultDict = field(default_factory=default_list_dict)
     12 D = Data()
---> 13 asdict(D)

File ~/miniconda3/lib/python3.10/dataclasses.py:1238, in asdict(obj, dict_factory)
   1236 if not _is_dataclass_instance(obj):
   1237     raise TypeError("asdict() should be called on dataclass instances")
-> 1238 return _asdict_inner(obj, dict_factory)

File ~/miniconda3/lib/python3.10/dataclasses.py:1245, in _asdict_inner(obj, dict_factory)
   1243 result = []
   1244 for f in fields(obj):
-> 1245     value = _asdict_inner(getattr(obj, f.name), dict_factory)
   1246     result.append((f.name, value))
   1247 return dict_factory(result)

File ~/miniconda3/lib/python3.10/dataclasses.py:1275, in _asdict_inner(obj, dict_factory)
   1273     return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
   1274 elif isinstance(obj, dict):
-> 1275     return type(obj)((_asdict_inner(k, dict_factory),
   1276                       _asdict_inner(v, dict_factory))
   1277                      for k, v in obj.items())
   1278 else:
   1279     return copy.deepcopy(obj)

TypeError: first argument must be callable or None

Your environment

  • CPython versions tested on: Python 3.10.11 [GCC 11.2.0] on linux
  • Operating system and architecture: Linux 6.1.31-2-MANJARO Support "bpo-" in Misc/NEWS #1 SMP PREEMPT_DYNAMIC Sun Jun 4 12:31:46 UTC 2023 x86_64 GNU/Linux
@johentsch johentsch added the type-bug An unexpected behavior, bug, or error label Jun 26, 2023
@Eclips4
Copy link
Member

Eclips4 commented Jun 26, 2023

Hello!
Duplicate of #79721.

@kwsp
Copy link
Contributor

kwsp commented Jun 26, 2023

Fixed in #32056 and first released in 3.12 https://docs.python.org/3.12/whatsnew/changelog.html

@johentsch
Copy link
Author

Ooops sorry for that and thanks!

@sunmy2019 sunmy2019 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants