Skip to content

Commit

Permalink
Fix misbehavior for models referencing redefined type aliases (#8050)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Nov 7, 2023
1 parent a9cebd4 commit ecc18bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydantic/_internal/_core_utils.py
Expand Up @@ -100,7 +100,7 @@ def get_type_ref(type_: type[Any], args_override: tuple[type[Any], ...] | None =

module_name = getattr(origin, '__module__', '<No __module__>')
if isinstance(origin, TypeAliasType):
type_ref = f'{module_name}.{origin.__name__}'
type_ref = f'{module_name}.{origin.__name__}:{id(origin)}'
else:
try:
qualname = getattr(origin, '__qualname__', f'<No __qualname__: {origin}>')
Expand Down
16 changes: 16 additions & 0 deletions tests/test_type_alias_type.py
Expand Up @@ -337,3 +337,19 @@ class MyModel(BaseModel):

def test_non_specified_generic_type_alias_type() -> None:
assert TypeAdapter(MyList).json_schema() == {'items': {}, 'type': 'array'}


def test_redefined_type_alias():
MyType = TypeAliasType('MyType', str)

class MyInnerModel(BaseModel):
x: MyType

MyType = TypeAliasType('MyType', int)

class MyOuterModel(BaseModel):
inner: MyInnerModel
y: MyType

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

0 comments on commit ecc18bd

Please sign in to comment.