This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `asdict` fails with frozen dataclass keys, using raw dict form
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: GalaxySnail, enaumov, eric.smith, terry.reedy
Priority: normal Keywords:

Created on 2021-02-06 05:46 by enaumov, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg386545 - (view) Author: Evgeny Naumov (enaumov) Date: 2021-02-06 05:46
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.
msg386880 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-12 21:40
On freshly compiled master on Win10, the example code gives "TypeError: unhashable type: 'dict'".
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87307
2022-03-21 23:17:26GalaxySnailsetnosy: + GalaxySnail
2021-02-12 21:40:32terry.reedysetnosy: + terry.reedy
title: `asdict` fails with frozen dataclass keys; tries to use raw dict form as key -> `asdict` fails with frozen dataclass keys, using raw dict form
messages: + msg386880

versions: + Python 3.10
2021-02-06 06:07:09xtreaksetnosy: + eric.smith
2021-02-06 05:46:54enaumovcreate