-
Notifications
You must be signed in to change notification settings - Fork 280
Make ValidationInfo
generic for context
#1686
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
base: main
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #1686 will not alter performanceComparing Summary
|
@@ -34,7 +34,9 @@ classifiers = [ | |||
'Operating System :: MacOS', | |||
'Typing :: Typed', | |||
] | |||
dependencies = ['typing-extensions>=4.6.0,!=4.7.0'] | |||
dependencies = [ | |||
'typing-extensions>=4.12.0', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to call this out in the changelog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pydantic already requires 4.12, so I don't think so.
ContextT = TypeVar('ContextT', covariant=True, default='Any | None') | ||
|
||
|
||
class ValidationInfo(Protocol[ContextT]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add some mypy tests for this / maybe also the covariance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do so in Pydantic after updating the core version. The covariance is required because it is a Protocol
, but I'm wondering if it really makes sense. At runtime, it is defined as a pyo3 class: shouldn't we move it's definition to the stub file instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess maybe it is defined this way so that it cannot be instantiated (at least for static type checkers, at runtime it would still be possible)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean move this ValidationInfo
definition to _pydantic_core.pyi
? That seems probably correct to me?
EDIT see below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah actually it's not exported by the library, so having it in _pydantic_core.pyi
seems slightly incorrect, it's not actually importable from there at runtime.
I guess this is why it's defined as a protocol here, the Rust type then satisfies the protocol without needing to actually be related to it in the hierarchy.
Change Summary
Fixes pydantic/pydantic#11485.
Example usage:
One one hand, it isn't strictly correct to give the validator implementation a way to "enforce" (at least from a static type checking perspective) a context type, as ultimately this can be unsafe if provide a different context value in the validate functions. On the other hand, it is convenient if you are guaranteed that the correct context will be used during validation.
If users don't control how validation is performed, they can still leave
ValidationInfo
unparameterized (and have the context type fallback toAny | None
) and have safety guards on theinfo.context
value.Related issue number
Checklist
pydantic-core
(except for expected changes)