[New-bugs-announce] [issue43141] `asdict` fails with frozen dataclass keys; tries to use raw dict form as key

Evgeny Naumov report at bugs.python.org
Sat Feb 6 00:46:54 EST 2021


New submission from Evgeny Naumov <evgeny.naumov at pm.me>:

A simple example is documented here: https://github.com/samuelcolvin/pydantic/issues/2233 but it doesn't look like it was actually filed as a bug against CPython... Copy paste of the minimal reproducing example:

from dataclasses import asdict, dataclass

@dataclass(eq=True, frozen=True)
class A:
  a: str

@dataclass(eq=True, frozen=True)
class B:
  b: dict[A, str]

asdict(B({A("x"):"y"}))


The offending code are these lines in dataclasses.py:

    elif isinstance(obj, dict):
        return type(obj)((_asdict_inner(k, dict_factory),
                          _asdict_inner(v, dict_factory))

The _asdict_inner(k, dict_factory) [correctly] turns dataclasses into dicts... which are not hashable. I assume there is a good reason the _asdict_inner function is called on the key, but its failure in this case looks like a (rather annoying!) error.

----------
components: Library (Lib)
messages: 386545
nosy: enaumov
priority: normal
severity: normal
status: open
title: `asdict` fails with frozen dataclass keys; tries to use raw dict form as key
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43141>
_______________________________________


More information about the New-bugs-announce mailing list