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 display_as_type for TypeAliasType in python 3.12 #7929

Merged
merged 1 commit into from Oct 25, 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
5 changes: 4 additions & 1 deletion pydantic/_internal/_repr.py
Expand Up @@ -105,7 +105,10 @@ def display_as_type(obj: Any) -> str:
args = ', '.join(map(repr, typing_extensions.get_args(obj)))
else:
args = ', '.join(map(display_as_type, typing_extensions.get_args(obj)))
return f'{obj.__qualname__}[{args}]'
try:
return f'{obj.__qualname__}[{args}]'
except AttributeError:
return str(obj) # handles TypeAliasType in 3.12
elif isinstance(obj, type):
return obj.__qualname__
else:
Expand Down
5 changes: 0 additions & 5 deletions tests/test_type_alias_type.py
@@ -1,5 +1,4 @@
import datetime
import sys
from typing import Dict, List, Tuple, TypeVar, Union

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


@pytest.mark.xfail(sys.version_info >= (3, 12), reason="TypeAliasType doesn't have __qualname__ yet")
def test_type_alias_annotated() -> None:
t = TypeAdapter(ShortMyList[int])

Expand All @@ -155,7 +153,6 @@ def test_type_alias_annotated() -> None:
assert t.json_schema() == {'type': 'array', 'items': {'type': 'integer'}, 'maxItems': 1}


@pytest.mark.xfail(sys.version_info >= (3, 12), reason="TypeAliasType doesn't have __qualname__ yet")
def test_type_alias_annotated_defs() -> None:
# force use of refs by referencing the schema in multiple places
t = TypeAdapter(Tuple[ShortMyList[int], ShortMyList[int]])
Expand Down Expand Up @@ -233,7 +230,6 @@ def test_recursive_generic_type_alias() -> None:
}


@pytest.mark.xfail(sys.version_info >= (3, 12), reason="TypeAliasType doesn't have __qualname__ yet")
def test_recursive_generic_type_alias_annotated() -> None:
t = TypeAdapter(ShortRecursiveGenericAlias[int])

Expand Down Expand Up @@ -265,7 +261,6 @@ def test_recursive_generic_type_alias_annotated() -> None:
}


@pytest.mark.xfail(sys.version_info >= (3, 12), reason="TypeAliasType doesn't have __qualname__ yet")
def test_recursive_generic_type_alias_annotated_defs() -> None:
# force use of refs by referencing the schema in multiple places
t = TypeAdapter(Tuple[ShortRecursiveGenericAlias[int], ShortRecursiveGenericAlias[int]])
Expand Down