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

No key validation for typed __pydantic_extra__ #9400

Open
1 task done
RLKRo opened this issue May 6, 2024 · 2 comments
Open
1 task done

No key validation for typed __pydantic_extra__ #9400

RLKRo opened this issue May 6, 2024 · 2 comments

Comments

@RLKRo
Copy link

RLKRo commented May 6, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Adding type annotation for __pydantic_extra__ only validates values of the extra fields.
I expected that annotating __pydantic_extra__ with something like Dict[constr(pattern="..."), str] would lead to validation of extra field names.
In the example below __pydantic_extra__ is annotated as Dict[constr(pattern="extra"), str] which I expected to lead to validation errors for any extra field names that do not contain "extra".

If this behavior is intended, documentation

By default, no validation will be applied to these extra items, but you can set a type for the values by overriding the type annotation for __pydantic_extra__:

should mention that field names are not validated.

Example Code

from typing import Dict
from typing_extensions import Annotated

from pydantic import BaseModel, ValidationError, constr


class ExtraWithIncorrectValue(BaseModel, extra="allow"):
    __pydantic_extra__: Dict[str, int]


# values are validated for extra fields
try:
    ExtraWithIncorrectValue(a="a")
except ValidationError:
    print("ExtraWithIncorrectValue raised an error")


class DictWithIncorrectConstrKey(BaseModel, extra="allow"):
    my_dict: Dict[constr(pattern="extra"), str]


# dict keys are validated
try:
    DictWithIncorrectConstrKey(my_dict={"a": "a"})
except ValidationError:
    print("DictWithIncorrectConstrKey raised an error")


class ExtraWithIncorrectConstrKey(BaseModel, extra="allow"):
    __pydantic_extra__: Dict[constr(pattern="extra"), str]


# extra field names are not validated
ExtraWithIncorrectConstrKey(a="a")
print("ExtraWithIncorrectConstrKey did not raise an error")

Python, Pydantic & OS Version

pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: /home/[redacted]/.cache/pypoetry/virtualenvs/[redacted]/lib/python3.12/site-packages/pydantic
               python version: 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417]
                     platform: Linux-6.8.9-zen1-1-zen-x86_64-with-glibc2.39
             related packages: typing_extensions-4.11.0
                       commit: unknown
@RLKRo RLKRo added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 6, 2024
@sydney-runkle sydney-runkle added feature request and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 14, 2024
@sydney-runkle
Copy link
Member

Marking this as a feature request, because I believe we could add key validation support. I do believe that the current docs make it clear that only validation is applied to the values with the current design.

Thanks for the FR. PRs welcome!

@RLKRo
Copy link
Author

RLKRo commented May 22, 2024

Would also be great to be able to annotate __pydantic_extra__ as a whole:

    __pydantic_extra__: Annotated[dict[str, Any], MyValidator]

instead of calling MyValidator inside model_validator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants