1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
From: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Date: Sun, 11 May 2025 15:21:26 -0700
Subject: Support types.UnionType in get_parameters()
This fixes a test on Python 3.14 because there Union[] also creates instances
of types.UnionType.
Origin: upstream, https://github.com/ilevkivskyi/typing_inspect/pull/111
Bug-Debian: https://bugs.debian.org/1123326
Last-Update: 2026-02-16
---
test_typing_inspect.py | 5 ++++-
typing_inspect.py | 4 ++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/test_typing_inspect.py b/test_typing_inspect.py
index dcb3771..c5a3c9c 100644
--- a/test_typing_inspect.py
+++ b/test_typing_inspect.py
@@ -4,7 +4,7 @@ import typing
import pytest
from typing_inspect import (
- is_generic_type, is_callable_type, is_new_type, is_tuple_type, is_union_type,
+ WITH_PIPE_UNION, is_generic_type, is_callable_type, is_new_type, is_tuple_type, is_union_type,
is_optional_type, is_final_type, is_literal_type, is_typevar, is_classvar,
is_forward_ref, get_origin, get_parameters, get_last_args, get_args, get_bound,
get_constraints, get_generic_type, get_generic_bases, get_last_origin,
@@ -379,6 +379,9 @@ class GetUtilityTestCase(TestCase):
self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co))
if PY39:
self.assertEqual(get_parameters(dict[int, T]), (T,))
+ if WITH_PIPE_UNION:
+ self.assertEqual(get_parameters(int | str), ())
+ self.assertEqual(get_parameters(int | list[T]), (T,))
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
def test_last_args(self):
diff --git a/typing_inspect.py b/typing_inspect.py
index 0345066..7605026 100644
--- a/typing_inspect.py
+++ b/typing_inspect.py
@@ -33,6 +33,9 @@ else:
NEW_TYPING = sys.version_info[:3] >= (3, 7, 0) # PEP 560
if NEW_TYPING:
import collections.abc
+WITH_PIPE_UNION = sys.version_info[:3] >= (3, 10, 0) # PEP 604
+if WITH_PIPE_UNION:
+ from types import UnionType
WITH_FINAL = True
WITH_LITERAL = True
@@ -430,6 +433,7 @@ def get_parameters(tp):
isinstance(tp, typingGenericAlias) and
hasattr(tp, '__parameters__')
) or
+ (WITH_PIPE_UNION and isinstance(tp, UnionType)) or
isinstance(tp, type) and issubclass(tp, Generic) and
tp is not Generic):
return tp.__parameters__
|