Title: asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " · Issue #106113 · python/cpython · GitHub
Open Graph Title: asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " · Issue #106113 · python/cpython
X Title: asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " · Issue #106113 · python/cpython
Description: 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: De...
Open Graph Description: 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(l...
X Description: 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(l...
Opengraph URL: https://github.com/python/cpython/issues/106113
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asdict() on dataclass with defaultdict raises TypeError: \"first argument must be callable or None \"","articleBody":"\u003c!--\r\n If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not\r\n the right place to seek help. Consider the following options instead:\r\n\r\n - reading the Python tutorial: https://docs.python.org/3/tutorial/\r\n - posting in the \"Users\" category on discuss.python.org: https://discuss.python.org/c/users/7\r\n - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list\r\n - searching our issue tracker (https://github.com/python/cpython/issues) to see if\r\n your problem has already been reported\r\n--\u003e\r\n\r\n# Bug report\r\n\r\nMinimal working example:\r\n\r\n```python \r\nfrom dataclasses import dataclass, field, asdict\r\nfrom typing import DefaultDict\r\nfrom collections import defaultdict\r\n\r\ndef default_list_dict():\r\n return defaultdict(list)\r\n\r\n@dataclass\r\nclass Data:\r\n metadata: DefaultDict = field(default_factory=default_list_dict)\r\n \r\nD = Data() # =\u003e Data(metadata=defaultdict(\u003cclass 'list'\u003e, {}))\r\nasdict(D)\r\n```\r\n\r\n* Expected output: `{'metadata': defaultdict(list, {})}`\r\n* Got instead: `TypeError: first argument must be callable or None`\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eDisplay traceback\u003c/summary\u003e\r\n\r\n```python-traceback\r\n---------------------------------------------------------------------------\r\nTypeError Traceback (most recent call last)\r\nCell In[1], line 13\r\n 10 metadata: DefaultDict = field(default_factory=default_list_dict)\r\n 12 D = Data()\r\n---\u003e 13 asdict(D)\r\n\r\nFile ~/miniconda3/lib/python3.10/dataclasses.py:1238, in asdict(obj, dict_factory)\r\n 1236 if not _is_dataclass_instance(obj):\r\n 1237 raise TypeError(\"asdict() should be called on dataclass instances\")\r\n-\u003e 1238 return _asdict_inner(obj, dict_factory)\r\n\r\nFile ~/miniconda3/lib/python3.10/dataclasses.py:1245, in _asdict_inner(obj, dict_factory)\r\n 1243 result = []\r\n 1244 for f in fields(obj):\r\n-\u003e 1245 value = _asdict_inner(getattr(obj, f.name), dict_factory)\r\n 1246 result.append((f.name, value))\r\n 1247 return dict_factory(result)\r\n\r\nFile ~/miniconda3/lib/python3.10/dataclasses.py:1275, in _asdict_inner(obj, dict_factory)\r\n 1273 return type(obj)(_asdict_inner(v, dict_factory) for v in obj)\r\n 1274 elif isinstance(obj, dict):\r\n-\u003e 1275 return type(obj)((_asdict_inner(k, dict_factory),\r\n 1276 _asdict_inner(v, dict_factory))\r\n 1277 for k, v in obj.items())\r\n 1278 else:\r\n 1279 return copy.deepcopy(obj)\r\n\r\nTypeError: first argument must be callable or None\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n# Your environment\r\n\r\n\u003c!-- Include as many relevant details as possible about the environment you experienced the bug in --\u003e\r\n\r\n- CPython versions tested on: Python 3.10.11 [GCC 11.2.0] on linux\r\n- Operating system and architecture: Linux 6.1.31-2-MANJARO #1 SMP PREEMPT_DYNAMIC Sun Jun 4 12:31:46 UTC 2023 x86_64 GNU/Linux\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n","author":{"url":"https://github.com/johentsch","@type":"Person","name":"johentsch"},"datePublished":"2023-06-26T16:06:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/106113/cpython/issues/106113"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c8b65f01-01fb-59f8-47a0-1482854099a5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A55E:C07BE:1E4DDD:285D01:6969BEC1 |
| html-safe-nonce | 30cc0c818610cbcaf62714633b7545e199aa091d438f635fb477ead0649c71a9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTVFOkMwN0JFOjFFNERERDoyODVEMDE6Njk2OUJFQzEiLCJ2aXNpdG9yX2lkIjoiMTg1ODc4MjcyOTg4NDc3ODE3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 04596fe46acf3967a9df6a331a980a91599940dc8614fe23bb7ba96fc639459c |
| hovercard-subject-tag | issue:1775125362 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/106113/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d8ae8f2870f051b3ef2d28959a3e244c2e9147c488ba1277004016dfbdcf4a4f/python/cpython/issues/106113 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d8ae8f2870f051b3ef2d28959a3e244c2e9147c488ba1277004016dfbdcf4a4f/python/cpython/issues/106113 |
| og:image:alt | 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(l... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johentsch |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width