Skip to content

Commit

Permalink
Merge branch 'dmontagu/support-indirect-definition-refs' of https://g…
Browse files Browse the repository at this point in the history
…ithub.com/pydantic/pydantic into dmontagu/support-indirect-definition-refs
  • Loading branch information
sydney-runkle committed Jan 10, 2024
2 parents 9128269 + bdc3b76 commit 8575da0
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/test_type_alias_type.py
Expand Up @@ -4,7 +4,7 @@

import pytest
from annotated_types import MaxLen
from typing_extensions import Annotated, TypeAliasType
from typing_extensions import Annotated, Literal, TypeAliasType

from pydantic import BaseModel, Field, ValidationError
from pydantic.type_adapter import TypeAdapter
Expand Down Expand Up @@ -366,3 +366,25 @@ class MyOuterModel(BaseModel):

data = {'inner': {'x': 'hello'}, 'y': 1}
assert MyOuterModel.model_validate(data).model_dump() == data


def test_type_alias_to_type_with_ref():
class Div(BaseModel):
type: Literal['Div'] = 'Div'
components: List['AnyComponent']

AnyComponent = TypeAliasType('AnyComponent', Div)

adapter = TypeAdapter(AnyComponent)
adapter.validate_python({'type': 'Div', 'components': [{'type': 'Div', 'components': []}]})
with pytest.raises(ValidationError) as exc_info:
adapter.validate_python({'type': 'Div', 'components': [{'type': 'NotDiv', 'components': []}]})
assert exc_info.value.errors(include_url=False) == [
{
'ctx': {'expected': "'Div'"},
'input': 'NotDiv',
'loc': ('components', 0, 'type'),
'msg': "Input should be 'Div'",
'type': 'literal_error',
}
]

0 comments on commit 8575da0

Please sign in to comment.