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
Unexpected Config Error with discriminator #4773
Comments
The real error is there |
I wrote about error, which received by user. |
example Foo(foo={'target': 't1', 'a': 1}) # this is Model1 Foo(foo={'target': 't2', 'b': 1}) # this is Model2 Usage below is not right Foo(**{"foo": {"target": {}}}) |
this is not a bug |
You mean that I need to validate
Why do you think so? |
target should not be {}, target should be one of t1 / t2 |
sounds weird In this case how to explain that all works correctly with
|
The bug report seems to be about the error handling itself.
raises a
raises a Both documents should be invalid and thus should both raise |
* Demonstrate #4773 * Raise `ValidationError` for unhashable discriminator values Fixes #4773 * Update tests/test_discrimated_union.py * Update pydantic/fields.py --------- Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com> Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
This will be fixed when we release v2: from typing import Literal
import pydantic
class Model1(pydantic.BaseModel):
target: Literal['t1']
a: int
class Model2(pydantic.BaseModel):
target: Literal['t2']
b: int
class Foo(pydantic.BaseModel):
foo: Model1 | Model2 = pydantic.Field(discriminator='target')
print(Foo(**{"foo": {"target": 't2', 'b': 123}}))
#> foo=Model2(target='t2', b=123)
Foo(**{"foo": {"target": []}})
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
foo
Input should be a valid string [type=string_type, input_value=[], input_type=list]
"""
Foo(**{'foo': {'target': {}}})
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
foo
Input should be a valid string [type=string_type, input_value={}, input_type=dict]
"""
Foo(**{"foo": {"target": 123}})
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
foo
Input tag '123' found using 'target' does not match any of the expected tags: 't1', 't2' [type=union_tag_invalid, input_value={'target': 123}, input_type=dict]
""" |
Initial Checks
Description
Discriminator validation.
Expected: ValidationError when passing list or dict as discriminator.
Received: ConfigError.
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: