From 65a14282724dac50e85b1385cd9663b329876837 Mon Sep 17 00:00:00 2001 From: jorenham Date: Thu, 3 Apr 2025 21:39:03 +0200 Subject: [PATCH 1/2] fix `TypeAliasType` union with `typing.TypeAliasType` --- src/test_typing_extensions.py | 4 ++++ src/typing_extensions.py | 29 +++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index da4e3e44..b8f5d4b7 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -7819,6 +7819,10 @@ def test_or(self): self.assertEqual(Alias | None, Union[Alias, None]) self.assertEqual(Alias | (int | str), Union[Alias, int | str]) self.assertEqual(Alias | list[float], Union[Alias, list[float]]) + + if sys.version_info >= (3, 12): + Alias2 = typing.TypeAliasType("Alias2", str) + self.assertEqual(Alias | Alias2, Union[Alias, Alias2]) else: with self.assertRaises(TypeError): Alias | int diff --git a/src/typing_extensions.py b/src/typing_extensions.py index 4b95dee7..c6c3b88e 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -3827,14 +3827,27 @@ def __ror__(self, other): TypeAliasType = typing.TypeAliasType # 3.8-3.13 else: - def _is_unionable(obj): - """Corresponds to is_unionable() in unionobject.c in CPython.""" - return obj is None or isinstance(obj, ( - type, - _types.GenericAlias, - _types.UnionType, - TypeAliasType, - )) + if sys.version_info >= (3, 12): + # 3.12-3.14 + def _is_unionable(obj): + """Corresponds to is_unionable() in unionobject.c in CPython.""" + return obj is None or isinstance(obj, ( + type, + _types.GenericAlias, + _types.UnionType, + typing.TypeAliasType, + TypeAliasType, + )) + else: + # 3.8-3.11 + def _is_unionable(obj): + """Corresponds to is_unionable() in unionobject.c in CPython.""" + return obj is None or isinstance(obj, ( + type, + _types.GenericAlias, + _types.UnionType, + TypeAliasType, + )) if sys.version_info < (3, 10): # Copied and pasted from https://github.com/python/cpython/blob/986a4e1b6fcae7fe7a1d0a26aea446107dd58dd2/Objects/genericaliasobject.c#L568-L582, From 2a278dcdaf4a945bcb83108e9e0b509045d2ab00 Mon Sep 17 00:00:00 2001 From: jorenham Date: Fri, 4 Apr 2025 14:03:55 +0200 Subject: [PATCH 2/2] changelog entry for the `TypeAliasType` union fix --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e0122cc..0d7f109c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Unreleased + +- Fix `TypeError` when taking the union of `typing_extensions.TypeAliasType` and a + `typing.TypeAliasType` on Python 3.12 and 3.13. + Patch by [Joren Hammudoglu](https://github.com/jorenham). + # Release 4.13.1 (April 3, 2025) Bugfixes: