Skip to content

Commit 8092c39

Browse files
authored
fix TypeAliasType union with typing.TypeAliasType (#575)
1 parent 45a8847 commit 8092c39

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Unreleased
2+
3+
- Fix `TypeError` when taking the union of `typing_extensions.TypeAliasType` and a
4+
`typing.TypeAliasType` on Python 3.12 and 3.13.
5+
Patch by [Joren Hammudoglu](https://github.com/jorenham).
6+
17
# Release 4.13.1 (April 3, 2025)
28

39
Bugfixes:

src/test_typing_extensions.py

+4
Original file line numberDiff line numberDiff line change
@@ -7819,6 +7819,10 @@ def test_or(self):
78197819
self.assertEqual(Alias | None, Union[Alias, None])
78207820
self.assertEqual(Alias | (int | str), Union[Alias, int | str])
78217821
self.assertEqual(Alias | list[float], Union[Alias, list[float]])
7822+
7823+
if sys.version_info >= (3, 12):
7824+
Alias2 = typing.TypeAliasType("Alias2", str)
7825+
self.assertEqual(Alias | Alias2, Union[Alias, Alias2])
78227826
else:
78237827
with self.assertRaises(TypeError):
78247828
Alias | int

src/typing_extensions.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -3827,14 +3827,27 @@ def __ror__(self, other):
38273827
TypeAliasType = typing.TypeAliasType
38283828
# 3.8-3.13
38293829
else:
3830-
def _is_unionable(obj):
3831-
"""Corresponds to is_unionable() in unionobject.c in CPython."""
3832-
return obj is None or isinstance(obj, (
3833-
type,
3834-
_types.GenericAlias,
3835-
_types.UnionType,
3836-
TypeAliasType,
3837-
))
3830+
if sys.version_info >= (3, 12):
3831+
# 3.12-3.14
3832+
def _is_unionable(obj):
3833+
"""Corresponds to is_unionable() in unionobject.c in CPython."""
3834+
return obj is None or isinstance(obj, (
3835+
type,
3836+
_types.GenericAlias,
3837+
_types.UnionType,
3838+
typing.TypeAliasType,
3839+
TypeAliasType,
3840+
))
3841+
else:
3842+
# 3.8-3.11
3843+
def _is_unionable(obj):
3844+
"""Corresponds to is_unionable() in unionobject.c in CPython."""
3845+
return obj is None or isinstance(obj, (
3846+
type,
3847+
_types.GenericAlias,
3848+
_types.UnionType,
3849+
TypeAliasType,
3850+
))
38383851

38393852
if sys.version_info < (3, 10):
38403853
# Copied and pasted from https://github.com/python/cpython/blob/986a4e1b6fcae7fe7a1d0a26aea446107dd58dd2/Objects/genericaliasobject.c#L568-L582,

0 commit comments

Comments
 (0)