Skip to content

BaseSetting Optional[conset(...)] not validated correctly #2320

@zoopp

Description

@zoopp

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

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

             pydantic version: 1.7.3
            pydantic compiled: True
                 install path: /home/mihai/.virtualenvs/pydantic/lib/python3.9/site-packages/pydantic
               python version: 3.9.1 (default, Dec 13 2020, 11:55:53)  [GCC 10.2.0]
                     platform: Linux-5.10.12-3-ck-x86_64-with-glibc2.32
     optional deps. installed: []
from typing import Optional

from pydantic import BaseModel, BaseSettings, IPvAnyNetwork, conset


class InfoModelSet(BaseModel):
    network_segments: Optional[set[IPvAnyNetwork]]


class InfoModelConset(BaseModel):
    network_segments: Optional[conset(IPvAnyNetwork, min_items=1)]


class InfoSettingsSet(BaseSettings):
    network_segments: Optional[set[IPvAnyNetwork]]


class InfoSettingsConset(BaseSettings):
    network_segments: Optional[conset(IPvAnyNetwork, min_items=1)]


print(f"Info model set: {InfoModelSet()}")
print(f"Info model conset: {InfoModelConset()}")
print(f"Info settings set: {InfoSettingsSet()}")
print()
print(f"Info settings conset: {InfoSettingsConset()}")

Results in:

Info model set: network_segments=None
Info model conset: network_segments=None
Info settings set: network_segments=None

Traceback (most recent call last):
  File "/home/mihai/bug.py", line 25, in <module>
    print(f"Info settings conset: {InfoSettingsConset()}")
  File "pydantic/env_settings.py", line 34, in pydantic.env_settings.BaseSettings.__init__
  File "pydantic/main.py", line 362, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for InfoSettingsConset
network_segments
  value is not a valid set (type=type_error.set)

I would have expected the sample to pass without raising errors and network_segments of the InfoSettingsConset instance to be None.

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