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

Dataclasses with Dataclass-Dict-Keys fail in asdict #2233

Closed
3 tasks done
dan-fritchman opened this issue Jan 3, 2021 · 2 comments
Closed
3 tasks done

Dataclasses with Dataclass-Dict-Keys fail in asdict #2233

dan-fritchman opened this issue Jan 3, 2021 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@dan-fritchman
Copy link

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.7.3
            pydantic compiled: True
                 install path: /Users/dan/miniconda3/envs/py38/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Aug  5 2020, 03:39:04)  [Clang 10.0.0 ]
                     platform: macOS-10.16-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

Thanks for all of your work on pydantic - it has been immensely valuable.

Bug report for dataclasses including Dict with other dataclasses as keys, failing to run dataclasses.asdict.

from pydantic.dataclasses import dataclass
from dataclasses import asdict
from typing import Dict

@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"}))

Yields:

Traceback (most recent call last):
  File "pydantic_asdict_demo.py", line 14, in <module>
    asdict(B({A("x"):"y"}))
  File "/Users/dan/miniconda3/envs/py38/lib/python3.8/dataclasses.py", line 1073, in asdict
    return _asdict_inner(obj, dict_factory)
  File "/Users/dan/miniconda3/envs/py38/lib/python3.8/dataclasses.py", line 1080, in _asdict_inner
    value = _asdict_inner(getattr(obj, f.name), dict_factory)
  File "/Users/dan/miniconda3/envs/py38/lib/python3.8/dataclasses.py", line 1110, in _asdict_inner
    return type(obj)((_asdict_inner(k, dict_factory),
TypeError: unhashable type: 'dict'
@dan-fritchman dan-fritchman added the bug V1 Bug related to Pydantic V1.X label Jan 3, 2021
@dan-fritchman
Copy link
Author

Looking some more, standard-library dataclasses fail the same way.

I can close if the expectation is that pydantic.dataclasses have parity with those.

@PrettyWood
Copy link
Member

Hi @dan-fritchman
Indeed the behaviour is the same as stdlib dataclass and the error is actually "expected"
Happy new year!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

2 participants