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 FrozenSet members fail upon declaration. #745

Closed
djpetti opened this issue Aug 13, 2019 · 1 comment
Closed

Dataclasses with FrozenSet members fail upon declaration. #745

djpetti opened this issue Aug 13, 2019 · 1 comment

Comments

@djpetti
Copy link
Contributor

@djpetti djpetti commented Aug 13, 2019

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.

@djpetti djpetti added the bug label Aug 13, 2019
@samuelcolvin samuelcolvin added feature request and removed bug labels Aug 13, 2019
@samuelcolvin

This comment has been minimized.

Copy link
Owner

@samuelcolvin samuelcolvin commented Aug 13, 2019

This is not a bug, but a feature request to support the typing type FrozenSet which I'd be happy to do. It should accept or convert iterables to frozenset not set.

We should also improve the error handling to explain what's going on.

FrozenSet won't be a subtype of set since it's a typing object and as an object doesn't behave anything like a set. Same as List isn't a subtype of list.

PR welcome, but probably won't be merged until v1.1.

djpetti added a commit to djpetti/pydantic that referenced this issue Aug 17, 2019
Fixes samuelcolvin#745

Add a better error message for fields with types from the typing
module that are not directly supported by Pydantic. Previously,
it caused a cryptic assertion failure.
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 17, 2019
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 17, 2019
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 17, 2019
@djpetti djpetti mentioned this issue Aug 17, 2019
4 of 4 tasks complete
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 18, 2019
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 18, 2019
Fixes samuelcolvin#745

Add a better error message for fields with types from the typing
module that are not directly supported by Pydantic. Previously,
it caused a cryptic assertion failure.
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 18, 2019
Also provide an example of their usage.

Fixes samuelcolvin#745
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 18, 2019
djpetti added a commit to djpetti/pydantic that referenced this issue Aug 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.