forked from pool/python-typing-inspect
Dirk Mueller
b53468ef28
- Add typing_inspect-pr69-py39-GenericAlias.patch to support Python 3.9 -- gh#ilevkivskyi/typing_inspect#69 OBS-URL: https://build.opensuse.org/request/show/880397 OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-typing-inspect?expand=0&rev=3
89 lines
3.6 KiB
Diff
89 lines
3.6 KiB
Diff
diff --git a/typing_inspect.py b/typing_inspect.py
|
|
index 2444027..896f1a9 100644
|
|
--- a/typing_inspect.py
|
|
+++ b/typing_inspect.py
|
|
@@ -23,6 +23,11 @@ if NEW_TYPING:
|
|
Generic, Callable, Union, TypeVar, ClassVar, Tuple, _GenericAlias, ForwardRef
|
|
)
|
|
from typing_extensions import Literal
|
|
+ if sys.version_info[:3] >= (3, 9, 0):
|
|
+ from typing import _SpecialGenericAlias
|
|
+ typingGenericAlias = (_GenericAlias, _SpecialGenericAlias)
|
|
+ else:
|
|
+ typingGenericAlias = (_GenericAlias, )
|
|
else:
|
|
from typing import (
|
|
Callable, CallableMeta, Union, Tuple, TupleMeta, TypeVar, GenericMeta, _ForwardRef
|
|
@@ -74,7 +79,7 @@ def is_generic_type(tp):
|
|
"""
|
|
if NEW_TYPING:
|
|
return (isinstance(tp, type) and issubclass(tp, Generic) or
|
|
- isinstance(tp, _GenericAlias) and
|
|
+ isinstance(tp, typingGenericAlias) and
|
|
tp.__origin__ not in (Union, tuple, ClassVar, collections.abc.Callable))
|
|
return (isinstance(tp, GenericMeta) and not
|
|
isinstance(tp, (CallableMeta, TupleMeta)))
|
|
@@ -100,7 +105,7 @@ def is_callable_type(tp):
|
|
get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7
|
|
"""
|
|
if NEW_TYPING:
|
|
- return (tp is Callable or isinstance(tp, _GenericAlias) and
|
|
+ return (tp is Callable or isinstance(tp, typingGenericAlias) and
|
|
tp.__origin__ is collections.abc.Callable or
|
|
isinstance(tp, type) and issubclass(tp, Generic) and
|
|
issubclass(tp, collections.abc.Callable))
|
|
@@ -126,7 +131,7 @@ def is_tuple_type(tp):
|
|
get_origin(tp) is tuple # Tuple prior to Python 3.7
|
|
"""
|
|
if NEW_TYPING:
|
|
- return (tp is Tuple or isinstance(tp, _GenericAlias) and
|
|
+ return (tp is Tuple or isinstance(tp, typingGenericAlias) and
|
|
tp.__origin__ is tuple or
|
|
isinstance(tp, type) and issubclass(tp, Generic) and
|
|
issubclass(tp, tuple))
|
|
@@ -164,14 +169,14 @@ def is_union_type(tp):
|
|
"""
|
|
if NEW_TYPING:
|
|
return (tp is Union or
|
|
- isinstance(tp, _GenericAlias) and tp.__origin__ is Union)
|
|
+ isinstance(tp, typingGenericAlias) and tp.__origin__ is Union)
|
|
return type(tp) is _Union
|
|
|
|
|
|
def is_literal_type(tp):
|
|
if NEW_TYPING:
|
|
return (tp is Literal or
|
|
- isinstance(tp, _GenericAlias) and tp.__origin__ is Literal)
|
|
+ isinstance(tp, typingGenericAlias) and tp.__origin__ is Literal)
|
|
return WITH_LITERAL and type(tp) is _Literal
|
|
|
|
|
|
@@ -196,7 +201,7 @@ def is_classvar(tp):
|
|
"""
|
|
if NEW_TYPING:
|
|
return (tp is ClassVar or
|
|
- isinstance(tp, _GenericAlias) and tp.__origin__ is ClassVar)
|
|
+ isinstance(tp, typingGenericAlias) and tp.__origin__ is ClassVar)
|
|
elif WITH_CLASSVAR:
|
|
return type(tp) is _ClassVar
|
|
else:
|
|
@@ -262,7 +267,7 @@ def get_origin(tp):
|
|
get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7
|
|
"""
|
|
if NEW_TYPING:
|
|
- if isinstance(tp, _GenericAlias):
|
|
+ if isinstance(tp, typingGenericAlias):
|
|
return tp.__origin__ if tp.__origin__ is not ClassVar else None
|
|
if tp is Generic:
|
|
return Generic
|
|
@@ -327,7 +332,7 @@ def get_parameters(tp):
|
|
else:
|
|
return ()
|
|
elif NEW_TYPING:
|
|
- if (isinstance(tp, _GenericAlias) or
|
|
+ if (isinstance(tp, typingGenericAlias) or
|
|
isinstance(tp, type) and issubclass(tp, Generic) and
|
|
tp is not Generic):
|
|
return tp.__parameters__
|
|
|