Skip to content

Commit

Permalink
Fix a naming issue with JSON schema for generics parametrized by recu…
Browse files Browse the repository at this point in the history
…rsive type aliases (#8389)
  • Loading branch information
dmontagu committed Dec 18, 2023
1 parent a8f3f7a commit 395451f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pydantic/_internal/_repr.py
Expand Up @@ -93,6 +93,8 @@ def display_as_type(obj: Any) -> str:
return '...'
elif isinstance(obj, Representation):
return repr(obj)
elif isinstance(obj, typing_extensions.TypeAliasType):
return str(obj)

if not isinstance(obj, (_typing_extra.typing_base, _typing_extra.WithArgsTypes, type)):
obj = obj.__class__
Expand Down
15 changes: 14 additions & 1 deletion tests/test_type_alias_type.py
@@ -1,5 +1,6 @@
import datetime
from typing import Dict, List, Tuple, TypeVar, Union
from dataclasses import dataclass
from typing import Dict, Generic, List, Tuple, TypeVar, Union

import pytest
from annotated_types import MaxLen
Expand Down Expand Up @@ -133,6 +134,18 @@ def test_recursive_type_alias() -> None:
}


def test_recursive_type_alias_name():
T = TypeVar('T')

@dataclass
class MyGeneric(Generic[T]):
field: T

MyRecursiveType = TypeAliasType('MyRecursiveType', Union[MyGeneric['MyRecursiveType'], int])
json_schema = TypeAdapter(MyRecursiveType).json_schema()
assert sorted(json_schema['$defs'].keys()) == ['MyGeneric_MyRecursiveType_', 'MyRecursiveType']


def test_type_alias_annotated() -> None:
t = TypeAdapter(ShortMyList[int])

Expand Down

0 comments on commit 395451f

Please sign in to comment.