-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
WrapValidator annotation changes smart union score #11752
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
Comments
I'd be happy to look into this on |
In smart mode, we count the number of fields that successfully validate for each union member. For instance, when validating: {
"type": "update",
"payload": {"name": "John Doe"},
} against class ObjectPayload(BaseModel):
name: str
class UpdateMutation(BaseModel):
type: Literal["update"] = Field("update")
payload: ObjectPayload = Field() that number equals 3: one for Looks like the wrap validator creates a new internal validation state (where that number of validated fields is tracked), so the counter is reset when validating |
It sounds like it's not intended? Should I take a stab at this? |
Indeed not intended. Thanks, go ahead! It should be pretty simple to fix. We already do the correct logic for In |
Initial Checks
Description
When Pydantic matches against a union type (in the default smart mode), adding a WrapValidator seems to change its preferred union member type.
Validating a field with a union of two similar models (see code below) without WrapValidator will use the leftmost union member. However, adding a "dummy" WrapValidator causes Pydantic to choose the right union member instead.
Using other validators like AfterValidator doesn't seem to show this issue.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: