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
Closed
Closed
Typing inspect #377
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ec72f71
Initial commit for typing_inspect
ilevkivskyi a0fbbe8
Fix some lint
ilevkivskyi 1cb1b6f
More fixes
ilevkivskyi d8270bb
Add docstrings plus few tests
ilevkivskyi 7a52845
A reorg
ilevkivskyi d53acc6
More precise rules for Generic
ilevkivskyi 258b79f
Add two more functions, add tests, clarify docstrings
3e3532a
Add one more example to docs
ilevkivskyi d46558b
Update setup.py
ilevkivskyi df44514
Response to review comments
ilevkivskyi b34b263
Add one more utility, make some things faster
329902f
Take care of cases where None is valid __origin__
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,4 @@ exclude = | |
| setup.py | ||
| python2/typing.py, | ||
| src/typing.py | ||
| typing_inspect/typing_inspect.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Typing Inspect | ||
| ============== | ||
|
|
||
| The "typing_inspect" module defines experimental API for runtime | ||
| inspection of types defined in the standard "typing" module. | ||
|
|
||
| Example usage:: | ||
| from typing_inspect import is_generic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # NOTE: This package must support Python 2.7 in addition to Python 3.x | ||
|
|
||
| from distutils.core import setup | ||
|
|
||
| version = '0.1.0-dev' | ||
| description = 'Runtime inspection utilities for typing module.' | ||
| long_description = ''' | ||
| Typing Inspect | ||
| ============== | ||
|
|
||
| The "typing_inspect" module defines experimental API for runtime | ||
| inspection of types defined in the standard "typing" module. | ||
| '''.lstrip() | ||
|
|
||
| classifiers = [ | ||
| 'Development Status :: 3 - Alpha', | ||
| 'Environment :: Console', | ||
| 'Intended Audience :: Developers', | ||
| 'License :: OSI Approved :: Python Software Foundation License', | ||
| 'Operating System :: OS Independent', | ||
| 'Programming Language :: Python :: 2', | ||
| 'Programming Language :: Python :: 2.7', | ||
| 'Programming Language :: Python :: 3', | ||
| 'Programming Language :: Python :: 3.3', | ||
| 'Programming Language :: Python :: 3.4', | ||
| 'Programming Language :: Python :: 3.5', | ||
| 'Programming Language :: Python :: 3.6', | ||
| 'Topic :: Software Development', | ||
| ] | ||
|
|
||
| setup( | ||
| name='typing_inspect', | ||
| version=version, | ||
| description=description, | ||
| long_description=long_description, | ||
| author='Ivan Levkivskyi', | ||
| author_email='levkivskyi@gmail.com', | ||
| url='https://github.com/python/typing', | ||
| license='PSF', | ||
| keywords='typing function annotations type hints hinting checking ' | ||
| 'checker typehints typehinting typechecking inspect reflection', | ||
| py_modules=['typing_inspect'], | ||
| classifiers=classifiers, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| from typing_inspect import ( | ||
| is_generic_type, is_callable_type, is_tuple_type, is_union_type, | ||
| is_typevar, is_classvar, get_origin, get_parameters, get_last_args, get_args, | ||
| get_generic_type, get_generic_bases, | ||
| ) | ||
| from unittest import TestCase, main | ||
| from typing import ( | ||
| Union, ClassVar, Callable, Optional, TypeVar, Sequence, Mapping, | ||
| MutableMapping, Iterable, Generic, List, Any, Dict, Tuple, | ||
| ) | ||
|
|
||
|
|
||
| class IsUtilityTestCase(TestCase): | ||
| def sample_test(self, fun, samples, nonsamples): | ||
| for s in samples: | ||
| self.assertTrue(fun(s)) | ||
| for s in nonsamples: | ||
| self.assertFalse(fun(s)) | ||
|
|
||
| def test_generic(self): | ||
| T = TypeVar('T') | ||
| samples = [Generic, Generic[T], Iterable[int], Mapping, | ||
| MutableMapping[T, List[int]], Sequence[Union[str, bytes]]] | ||
| nonsamples = [int, Union[int, str], Union[int, T], ClassVar[List[int]], | ||
| Callable[..., T], ClassVar, Optional, bytes, list] | ||
| self.sample_test(is_generic_type, samples, nonsamples) | ||
|
|
||
| def test_callable(self): | ||
| samples = [Callable, Callable[..., int], | ||
| Callable[[int, int], Iterable[str]]] | ||
| nonsamples = [int, type, 42, [], List[int], | ||
| Union[callable, Callable[..., int]]] | ||
| self.sample_test(is_callable_type, samples, nonsamples) | ||
| class MyClass(Callable[[int], int]): | ||
| pass | ||
| self.assertTrue(is_callable_type(MyClass)) | ||
|
|
||
| def test_tuple(self): | ||
| samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]] | ||
| nonsamples = [int, tuple, 42, List[int]] | ||
| self.sample_test(is_tuple_type, samples, nonsamples) | ||
| class MyClass(Tuple[str, int]): | ||
| pass | ||
| self.assertTrue(is_tuple_type(MyClass)) | ||
|
|
||
| def test_union(self): | ||
| T = TypeVar('T') | ||
| S = TypeVar('S') | ||
| samples = [Union, Union[T, int], Union[int, Union[T, S]]] | ||
| nonsamples = [int, Union[int, int], [], Iterable[Any]] | ||
| self.sample_test(is_union_type, samples, nonsamples) | ||
|
|
||
| def test_typevar(self): | ||
| T = TypeVar('T') | ||
| S_co = TypeVar('S_co', covariant=True) | ||
| samples = [T, S_co] | ||
| nonsamples = [int, Union[T, int], Union[T, S_co], type, ClassVar[int]] | ||
| self.sample_test(is_typevar, samples, nonsamples) | ||
|
|
||
| def test_classvar(self): | ||
| T = TypeVar('T') | ||
| samples = [ClassVar, ClassVar[int], ClassVar[List[T]]] | ||
| nonsamples = [int, 42, Iterable, List[int], type, T] | ||
| self.sample_test(is_classvar, samples, nonsamples) | ||
|
|
||
|
|
||
| class GetUtilityTestCase(TestCase): | ||
| def test_origin(self): | ||
| T = TypeVar('T') | ||
| self.assertEqual(get_origin(int), None) | ||
| self.assertEqual(get_origin(ClassVar[int]), None) | ||
| self.assertEqual(get_origin(Generic), Generic) | ||
| self.assertEqual(get_origin(Generic[T]), Generic) | ||
| self.assertEqual(get_origin(List[Tuple[T, T]][int]), List) | ||
|
|
||
| def test_parameters(self): | ||
| T = TypeVar('T') | ||
| S_co = TypeVar('S_co', covariant=True) | ||
| U = TypeVar('U') | ||
| self.assertEqual(get_parameters(int), ()) | ||
| self.assertEqual(get_parameters(Generic), ()) | ||
| self.assertEqual(get_parameters(Union), ()) | ||
| self.assertEqual(get_parameters(List[int]), ()) | ||
| self.assertEqual(get_parameters(Generic[T]), (T,)) | ||
| self.assertEqual(get_parameters(Tuple[List[T], List[S_co]]), (T, S_co)) | ||
| self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,)) | ||
| self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co)) | ||
|
|
||
| def test_last_args(self): | ||
| T = TypeVar('T') | ||
| S = TypeVar('S') | ||
| self.assertEqual(get_last_args(int), ()) | ||
| self.assertEqual(get_last_args(Union), ()) | ||
| self.assertEqual(get_last_args(ClassVar[int]), (int,)) | ||
| self.assertEqual(get_last_args(Union[T, int]), (T, int)) | ||
| self.assertEqual(get_last_args(Iterable[Tuple[T, S]][int, T]), (int, T)) | ||
|
|
||
| def test_args(self): | ||
| T = TypeVar('T') | ||
| self.assertEqual(get_args(Union[int, Tuple[T, int]][str]), | ||
| (int, (Tuple, str, int))) | ||
| self.assertEqual(get_args(Union[int, Union[T, int], str][int]), | ||
| (int, str)) | ||
| self.assertEqual(get_args(int), ()) | ||
| self.assertEqual(get_args(Dict[int, Tuple[T, T]][Optional[int]], evaluate=True), | ||
| (int, Tuple[Optional[int], Optional[int]])) | ||
|
|
||
| def test_generic_type(self): | ||
| T = TypeVar('T') | ||
| class Node(Generic[T]): pass | ||
| self.assertIs(get_generic_type(Node()), Node) | ||
| self.assertIs(get_generic_type(Node[int]()), Node[int]) | ||
| self.assertIs(get_generic_type(Node[T]()), Node[T],) | ||
| self.assertIs(get_generic_type(1), int) | ||
|
|
||
| def test_generic_bases(self): | ||
| class MyClass(List[int], Mapping[str, List[int]]): pass | ||
| self.assertEqual(get_generic_bases(MyClass), | ||
| (List[int], Mapping[str, List[int]])) | ||
| self.assertEqual(get_generic_bases(int), ()) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe add some NamedTuples as test cases; I assume they'll be nonsamples because typing.Tuple and typing.NamedTuple are completely distinct, but it would be good to clarify anyway.