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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 21 additions & 8 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy