Skip to content

Commit

Permalink
Fix display_as_type for TypeAliasType in python 3.12 (#7929)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Oct 25, 2023
1 parent 026be6a commit aa73451
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
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

0 comments on commit aa73451

Please sign in to comment.