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

min_items not enforcable for FrozenSet #1897

Closed
snazzyfox opened this issue Sep 3, 2020 · 2 comments · Fixed by #2672
Closed

min_items not enforcable for FrozenSet #1897

snazzyfox opened this issue Sep 3, 2020 · 2 comments · Fixed by #2672
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@snazzyfox
Copy link

snazzyfox commented Sep 3, 2020

Bug

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

             pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /home/snazzy/code/test-project/venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.2 (default, Jul 16 2020, 14:00:26)  [GCC 9.3.0]
                     platform: Linux-5.4.0-42-generic-x86_64-with-glibc2.29
     optional deps. installed: ['typing-extensions']

Code Example

This works fine:

from pydantic import BaseModel, Field
from typing import Set

class ModelOne(BaseModel):
    my_set: Set = Field(..., min_items=1)

this_should_fail = ModelOne(my_set=set())

Using FrozenSet instead of Set doesn't work:

from pydantic import BaseModel, Field
from typing import FrozenSet

class ModelTwo(BaseModel):
    my_frozen_set: FrozenSet = Field(..., min_items=1)

This causes an error at definition time:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "pydantic/main.py", line 252, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/fields.py", line 308, in pydantic.fields.ModelField.infer
  File "pydantic/schema.py", line 864, in pydantic.schema.get_annotation_from_field_info
ValueError: On field "my_frozen_set" the following field constraints are set but not enforced: min_items. 
For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints

Since FrozenSet is simply the immutable version of Set, it should be possible to use min_items to validate the size of the set.


Edit:

The reason I expected FrozenSet to work the same way as mutable Set is because the docs describe them in the exact same manner. Even though frozen sets are not mentioned in the constrained types section, it's a little natural to (wrongly) assume parallel functionalities are available for it.

@snazzyfox snazzyfox added the bug V1 Bug related to Pydantic V1.X label Sep 3, 2020
@PrettyWood
Copy link
Member

Hello @snazzyfox
Unfortunately we currently have some custom logic, which doesn't support Frozenset as the origin is frozenset.
We could probably just add a if issubclass(origin, frozenset) ... and add the immutability in conset or duplicate some logic to have a confrozenset

@snazzyfox
Copy link
Author

Thanks for taking a look! This totally makes sense.

I think whether we create confrozenset or conset(frozen=True) depends on if we want to also duplicate the ConstrainedSet class. The code for frozen sets will most probably be almost identical, just with a different base class. If there's a way we can update/reuse the existing class, adding a immutable option to conset would make more sense.

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

Successfully merging a pull request may close this issue.

2 participants