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 fails with frozen dataclass keys, using raw dict form #87307

Open
qdbp mannequin opened this issue Feb 6, 2021 · 4 comments
Open

asdict fails with frozen dataclass keys, using raw dict form #87307

qdbp mannequin opened this issue Feb 6, 2021 · 4 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir topic-dataclasses type-bug An unexpected behavior, bug, or error

Comments

@qdbp
Copy link
Mannequin

qdbp mannequin commented Feb 6, 2021

BPO 43141
Nosy @terryjreedy, @ericvsmith, @qdbp, @GalaxySnail

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-02-06.05:46:54.621>
labels = ['type-bug', 'library', '3.9', '3.10']
title = '`asdict` fails with frozen dataclass keys, using raw dict form'
updated_at = <Date 2022-03-21.23:17:26.040>
user = 'https://github.com/qdbp'

bugs.python.org fields:

activity = <Date 2022-03-21.23:17:26.040>
actor = 'GalaxySnail'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-02-06.05:46:54.621>
creator = 'enaumov'
dependencies = []
files = []
hgrepos = []
issue_num = 43141
keywords = []
message_count = 2.0
messages = ['386545', '386880']
nosy_count = 4.0
nosy_names = ['terry.reedy', 'eric.smith', 'enaumov', 'GalaxySnail']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43141'
versions = ['Python 3.9', 'Python 3.10']

@qdbp
Copy link
Mannequin Author

qdbp mannequin commented Feb 6, 2021

A simple example is documented here: pydantic/pydantic#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.

@qdbp qdbp mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 6, 2021
@terryjreedy
Copy link
Member

On freshly compiled master on Win10, the example code gives "TypeError: unhashable type: 'dict'".

@terryjreedy terryjreedy added the 3.10 only security fixes label Feb 12, 2021
@terryjreedy terryjreedy changed the 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 Feb 12, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@nj-vs-vh
Copy link

nj-vs-vh commented Nov 4, 2023

Is there a plan to fix or somehow mitigate this? I stumbled upon this error in the wild and spent some time figuring out the cause.

From my point of view, it doesn't really make sense to call _asdict_inner on keys, because dicts are not hashable by design. So, maybe just remove it?

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

@terryjreedy
Copy link
Member

@ericvsmith dataclass issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir topic-dataclasses type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants