GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None " · Issue #106113 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
asdict() on dataclass with defaultdict raises TypeError: "first argument must be callable or None "#106113
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@johentsch
johentsch
opened on Jun 26, 2023
Issue body actions

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

Activity

  1. johentsch
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 26, 2023
  2. Eclips4 commented on Jun 26, 2023

    @Eclips4
    Eclips4
    on Jun 26, 2023
    Member
    More actions

    Hello!
    Duplicate of #79721.

  3. kwsp commented on Jun 26, 2023

    @kwsp
    kwsp
    on Jun 26, 2023
    Contributor
    More actions

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

  4. johentsch commented on Jun 26, 2023

    @johentsch
    johentsch
    on Jun 26, 2023
    Author
    More actions

    Ooops sorry for that and thanks!

  5. johentsch
    closed this as completedon Jun 26, 2023
  6. sunmy2019
    closed this as not plannedon Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.