Skip to content

min_items not enforcable for FrozenSet #1897

@snazzyfox

Description

@snazzyfox

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions