Skip to content

constrained set type "conset" #1623

@patrickkwang

Description

@patrickkwang

Feature Request

Is there a way to benefit from the best of both worlds: [uniqueness] from set and length constraints from conlist()? - @neimad1985 in #1472 (comment)

Also important to me is that sets are unordered.


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

             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: .../venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.1 (default, Feb 19 2020, 09:57:06)  [Clang 11.0.0 (clang-1100.0.33.17)]
                     platform: macOS-10.15.5-x86_64-i386-64bit
     optional deps. installed: []

Example behavior:

from pydantic import conset

class ConSetModel(BaseModel):
    v: conset(int, max_items=3)

print(ConSetModel(v=[1, 2, 2]).v)     # {1, 2}
print(ConSetModel(v=[1, 2, 2, 3]).v)  # {1, 2, 3}
print(ConSetModel(v=[1, 2, 3, 4]).v)  # raises ValidationError

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