8000 Dataclasses with FrozenSet members fail upon declaration. · Issue #745 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Dataclasses with FrozenSet members fail upon declaration. #745

@djpetti

Description

@djpetti

Feature Request

Please complete:

  • OS: Ubuntu 16.04
  • Python version import sys; print(sys.version): 3.7.3
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.32.1

When creating a pydantic dataclass that has a FrozenSet member, initialization fails in the @dataclass decorator.

For example,

from typing import FrozenSet

from pydantic.dataclasses import dataclass

@dataclass
class MyDataClass:
    foo: FrozenSet[str]
...

will fail with an assertion error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "pydantic/dataclasses.py", line 125, in pydantic.dataclasses.dataclass.wrap
  File "pydantic/dataclasses.py", line 87, in pydantic.dataclasses._process_class
  File "pydantic/main.py", line 710, in pydantic.main.create_model
  File "pydantic/main.py", line 228, in pydantic.main.MetaModel.__new__
  File "pydantic/fields.py", line 145, in pydantic.fields.Field.infer
  File "pydantic/fields.py", line 122, in pydantic.fields.Field.__init__
  File "pydantic/fields.py", line 187, in pydantic.fields.Field.prepare
  File "pydantic/fields.py", line 252, in pydantic.fields.Field._populate_sub_fields
AssertionError

Looking in the pydantic source code, it looks like this has to do with how it determines the type of the field. Specifically, the issue is that FrozenSet, somewhat counter-intuitively, does not appear to be a subtype of Set:

>>> from typing import FrozenSet, Set
>>> issubclass(FrozenSet, Set)
False 

It looks like this if-else block has to be amended with a specific case for FrozenSet.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0