Skip to content

Commit

Permalink
🐛 Fix validation warning for unions of Literal and other type
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Jul 12, 2023
1 parent 176714d commit 3b80f52
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pytest
from dirty_equals import HasRepr, IsStr
from pydantic_core import ErrorDetails, InitErrorDetails, PydanticSerializationError, core_schema
from typing_extensions import Annotated, TypedDict, get_args
from typing_extensions import Annotated, Literal, TypedDict, get_args

from pydantic import (
BaseModel,
Expand Down Expand Up @@ -2563,3 +2563,29 @@ class MyModel(TypedDict):
b: Optional[MyEnum]

TypeAdapter(MyModel)


@pytest.mark.parametrize(
('literal_type', 'other_type', 'data'),
[
(Literal[False], str, False),
(Literal[True], str, True),
(Literal[False], str, 'abc'),
(Literal[False], int, False),
(Literal[True], int, True),
(Literal[False], int, 42),
],
)
def test_union_literal_with_other_type(literal_type, other_type, data):
class Model(BaseModel):
value: Union[literal_type, other_type]
value_types_reversed: Union[other_type, literal_type]

from pprint import pprint

pprint(Model.__pydantic_core_schema__)
pprint(Model.model_json_schema())

m = Model(value=data, value_types_reversed=data)
assert m.model_dump() == {'value': data, 'value_types_reversed': data}
assert m.model_dump_json() == {'value': data, 'value_types_reversed': data}

0 comments on commit 3b80f52

Please sign in to comment.