-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Handle function validators in a discriminated union #6570
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
Conversation
Deploying with
|
| Latest commit: |
35e9586
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://0629540d.pydantic-docs2.pages.dev |
| Branch Preview URL: | https://discriminator-function-valid.pydantic-docs2.pages.dev |
|
Sorry for never linking it to the original issue: #6541 |
| AllowedAnimal = Annotated[Union[CatModel, DogModel], Field(discriminator='name')] | ||
|
|
||
| class Model(BaseModel): | ||
| x: AllowedAnimal | ||
|
|
||
| m = Model(x={'name': 'kitty'}) | ||
| assert m.x.name == 'cat' |
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.
Might be worth confirming a discriminated union was used, either via an error case or a json schema (or both)
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 was relying on the docs tests for that but I'll add it here too
| elif schema['type'] == 'function-after': | ||
| # After validators don't affect the discriminator values | ||
| return self._infer_discriminator_values_for_inner_schema(schema['schema'], source) | ||
|
|
||
| elif schema['type'] in {'function-before', 'function-wrap', 'function-plain'}: |
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.
Not sure how you got around typing here but might be worth using _core_utils.is_function_with_inner_schema(
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.
Note that it's not actually equivalent to that function call. We could do something like that, but this works because I'm not actually using any fields from inside schema to generate this error, so I don't need to do any type narrowing of schema.
The reason I did this was because each of those types of validator could affect what goes in, whereas function-after can't, so it's safe to rely on the input discriminator to select the union case to use for type-checking.
| Model(x={'name': 'invalid'}) | ||
| assert exc_info.value.errors(include_url=False) == [ | ||
| { | ||
| 'ctx': {'discriminator': "'name'", 'expected_tags': "'kitty', 'cat', 'puppy', 'dog'", 'tag': 'invalid'}, |
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.
These should be a list, it’s a 🐛
Closes #6525
skip change file check