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

UUID type Field doesn't support strict specification #7838

Closed
1 task done
EvanED opened this issue Oct 16, 2023 · 5 comments · Fixed by #7865
Closed
1 task done

UUID type Field doesn't support strict specification #7838

EvanED opened this issue Oct 16, 2023 · 5 comments · Fixed by #7865
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@EvanED
Copy link

EvanED commented Oct 16, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

This seems to be closely related to #7759 which is marked fixed, but if I installed the current main correctly it doesn't seem to be fixed there.

I would like to make a class that is, in general, strict, but where that strictness is relaxed for UUID fields so I can get conversions from strings rather than have to pre-convert those:

class UnstrictExample(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(strict=True)
    id: uuid.UUID = pydantic.Field(strict=False)
    val: int

With Pydantic 2.1, this works:

>>> import pydantic
>>> import uuid
>>> class UnstrictExample(pydantic.BaseModel):
...     model_config = pydantic.ConfigDict(strict=True)
...     id: uuid.UUID = pydantic.Field(strict=False)
...     val: int
... 
>>> UnstrictExample(id="e9dbca76-6adb-4e1c-af87-ac7b9e24fded", val=123)
UnstrictExample(id=UUID('e9dbca76-6adb-4e1c-af87-ac7b9e24fded'), val=123)
>>> pydantic.version.VERSION
'2.1.1'

However, upgrade to 2.2, and it does not:

>>> class UnstrictExample(pydantic.BaseModel):
...     model_config = pydantic.ConfigDict(strict=True)
...     id: uuid.UUID = pydantic.Field(strict=False)
...     val: int
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 178, in __new__
    complete_model_class(
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 468, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1644, in _apply_single_annotation
    maybe_updated_schema = _known_annotated_metadata.apply_known_metadata(metadata, schema.copy())
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_known_annotated_metadata.py", line 239, in apply_known_metadata
    raise RuntimeError(f'Unable to apply constraint {constraint} to schema {schema_type}')
RuntimeError: Unable to apply constraint strict to schema uuid
>>> pydantic.version.VERSION
'2.2.1'

and this remains true if I pip install 'git+https://github.com/pydantic/pydantic@main#egg=pydantic' (though it may be installing pydantic-core from somewhere else...? I'm not sure I'm checking current trunk correctly; sorry if this is fixed after all.)

Example Code

import pydantic
import uuid
class UnstrictExample(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(strict=True)
    id: uuid.UUID = pydantic.Field(strict=False)
    val: int

Python, Pydantic & OS Version

Running from python:3.11-slim-bookworm Docker container, but I get similar with 3.8 in a local venv. Here's the version information for 2.2:

             pydantic version: 2.2.1
        pydantic-core version: 2.6.1
          pydantic-core build: profile=release pgo=true
                 install path: /usr/local/lib/python3.11/site-packages/pydantic
               python version: 3.11.4 (main, Jul 28 2023, 05:12:06) [GCC 12.2.0]
                     platform: Linux-5.4.0-150-generic-x86_64-with-glibc2.36
     optional deps. installed: ['typing-extensions']
@EvanED EvanED added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 16, 2023
@EvanED
Copy link
Author

EvanED commented Oct 16, 2023

Actually... this is a somewhat different problem than I thought, and got a slightly more simple example. Turns out the class-level config doesn't matter, and even this is sufficient:

>>> class UnstrictExample(pydantic.BaseModel):
...     id: uuid.UUID = pydantic.Field(strict=False)
...     val: int
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 178, in __new__
    complete_model_class(
...
  File "/usr/local/lib/python3.11/site-packages/pydantic/_internal/_known_annotated_metadata.py", line 239, in apply_known_metadata
    raise RuntimeError(f'Unable to apply constraint {constraint} to schema {schema_type}')
RuntimeError: Unable to apply constraint strict to schema uuid

@sydney-runkle sydney-runkle self-assigned this Oct 16, 2023
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Oct 16, 2023
@sydney-runkle
Copy link
Member

@EvanED,

Thanks for reporting this. This indeed looks like a bug 🐛. I'll work to get a fix out for this soon 👍.

@EvanED
Copy link
Author

EvanED commented Oct 16, 2023

Appreciated; thanks a bundle!

@sydney-runkle sydney-runkle changed the title "Unable to apply constraint strict to schema..." when class strictness is set to True but field is set to False UUID type Field doesn't support strict specification Oct 16, 2023
@sydney-runkle
Copy link
Member

Just merged a fix. This should be fixed in the next release 😄.

@EvanED
Copy link
Author

EvanED commented Oct 20, 2023

A couple hours to confirm and less than two days for a fix... absolutely brilliant. Thank you very much, I look forward it making a release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants