Repository navigation
Typing inspect #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing inspect #377
Changes from 1 commit
ec72f71
a0fbbe8
1cb1b6f
d8270bb
7a52845
d53acc6
258b79f
3e3532a
d46558b
df44514
b34b263
329902f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,10 @@ | |
|
|
||
| # NOTE: This module must support Python 2.7 in addition to Python 3.x | ||
|
|
||
| from typing import Callable, Union, Tuple, TypeVar, _gorg, ClassVar, Generic | ||
| from typing import ( | ||
| Callable, CallableMeta, Union, _Union, TupleMeta, TypeVar, | ||
| _gorg, _ClassVar, GenericMeta, | ||
| ) | ||
|
|
||
|
|
||
| def is_generic_type(tp): | ||
|
|
@@ -29,8 +32,8 @@ def is_generic_type(tp): | |
| is_generic_type(Sequence[Union[str, bytes]]) == True | ||
| """ | ||
|
|
||
| return (isinstance(tp, type(Generic)) and not | ||
| isinstance(tp, (type(Callable), type(Tuple)))) | ||
| return (isinstance(tp, GenericMeta) and not | ||
| isinstance(tp, (CallableMeta, TupleMeta))) | ||
|
|
||
|
|
||
| def is_callable_type(tp): | ||
|
|
@@ -53,7 +56,7 @@ class MyClass(Callable[[int], int]): | |
| get_origin(tp) is Callable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference exactly between this construct and
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the latter excludes subclasses, I added this to the docstring. |
||
| """ | ||
|
|
||
| return type(tp) is type(Callable) | ||
| return type(tp) is CallableMeta | ||
|
|
||
|
|
||
| def is_tuple_type(tp): | ||
|
|
@@ -75,7 +78,7 @@ class MyClass(Tuple[str, int]): | |
| get_origin(tp) is Tuple | ||
| """ | ||
|
|
||
| return type(tp) is type(Tuple) | ||
| return type(tp) is TupleMeta | ||
|
|
||
|
|
||
| def is_union_type(tp): | ||
|
|
@@ -87,7 +90,7 @@ def is_union_type(tp): | |
| is_union_type(Union[T, int]) == True | ||
| """ | ||
|
|
||
| return type(tp) is type(Union) | ||
| return type(tp) is _Union | ||
|
|
||
|
|
||
| def is_typevar(tp): | ||
|
|
@@ -110,7 +113,22 @@ def is_classvar(tp): | |
| is_classvar(ClassVar[List[T]]) == True | ||
| """ | ||
|
|
||
| return type(tp) is type(ClassVar) | ||
| return type(tp) is _ClassVar | ||
|
|
||
|
|
||
| def get_last_origin(tp): | ||
| """Get the last base of (multiply) subscripted type. Supports generic types, | ||
| Union, Callable, and Tuple. Returns None for unsupported types. | ||
| Examples:: | ||
|
|
||
| get_last_origin(int) == None | ||
| get_last_origin(ClassVar[int]) == None | ||
| get_last_origin(Generic[T]) == Generic | ||
| get_last_origin(Union[T, int][str]) == Union[T, int] | ||
| get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]] | ||
| """ | ||
|
|
||
| return getattr(tp, '__origin__', None) | ||
|
|
||
|
|
||
| def get_origin(tp): | ||
|
|
@@ -125,7 +143,7 @@ def get_origin(tp): | |
| get_origin(List[Tuple[T, T]][int]) == List | ||
| """ | ||
|
|
||
| if isinstance(tp, type(Generic)): | ||
| if isinstance(tp, GenericMeta): | ||
| return _gorg(tp) | ||
| if is_union_type(tp): | ||
| return Union | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a requirement for all
typingmodules?And regardless: Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typingon PyPI supports 2.7 (which is supported until 2020).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah! didn’t know it was on PyPI