Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a naming issue with JSON schema for generics parametrized by recursive type aliases #8389

Merged
merged 2 commits into from Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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