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

Type of validation context #9411

Open
1 task done
OhioDschungel6 opened this issue May 8, 2024 · 1 comment
Open
1 task done

Type of validation context #9411

OhioDschungel6 opened this issue May 8, 2024 · 1 comment
Labels
bug V2 Bug related to Pydantic V2

Comments

@OhioDschungel6
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using BaseModel.model_validate_json() or BaseModel.model_validate() the optional parameter "context" is typed to be a dict[str, Any].
This context is made available to validators via the ValidationInfo object.
The type of the info.context object is "Any | None" instead of "dict[str,Any] | None".

Example Code

from typing import Any

from pydantic import BaseModel, model_validator
from pydantic_core.core_schema import ValidationInfo


class Model(BaseModel):
    @model_validator(mode="after")
    def validator(self, info: ValidationInfo) -> Any:
        info.context  # this returns a object of type "Any | None"


Model.model_validate_json("{}", context={})  # type of context has to be "dict[str, Any] | None"

Python, Pydantic & OS Version

pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
               python version: 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: mypy-1.10.0 typing_extensions-4.11.0
@OhioDschungel6 OhioDschungel6 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 8, 2024
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label May 16, 2024
@sydney-runkle
Copy link
Member

@OhioDschungel6,

Indeed, looks like our type hints could be improved. Would you be interested in opening a PR to fix this issue? Thanks for bringing it to our attention!

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

No branches or pull requests

2 participants