Skip to content

Commit

Permalink
Demonstrate pydantic#4773
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Mar 2, 2023
1 parent 7f3b754 commit 0ae7b22
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_discrimated_union.py
Expand Up @@ -423,3 +423,23 @@ class Container(GenericModel, Generic[T]):

# coercion is done properly
assert Container[str].parse_obj({'result': {'type': 'Success', 'data': 1}}).result.data == '1'


def test_discriminator_with_unhashable_type():
"""Verify an unhashable discriminator value raises a ValidationError."""

class Model1(BaseModel):
target: Literal['t1']
a: int

class Model2(BaseModel):
target: Literal['t2']
b: int

class Foo(BaseModel):
foo: Union[Model1, Model2] = Field(discriminator='target')

with pytest.raises(
ValidationError, match=re.escape("No match for discriminator 'target' and value {}")
):
Foo(**{'foo': {'target': {}}})

0 comments on commit 0ae7b22

Please sign in to comment.