-
Notifications
You must be signed in to change notification settings - Fork 238
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
Typing inspect #377
Conversation
typing_inspect/typing_inspect.py
Outdated
the internal representation of types. If `evaluate` is True, then all | ||
type parameters are applied (this could be expensive). Examples:: | ||
|
||
get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int)) |
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.
Why not
get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
Your transformation is a bit suprising if I’m concerned.
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.
This is not "transformation", this is - quoting the docstring - "internal representation". The reason is also mentioned there: creating new type objects is space and time consuming (even with cashing). If you want everything be actually substituted, just use evaluate=True
.
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.
OK, maybe adding an example would be useful here
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.
There is an example with evaluate=True
4 lines below, or do you mean one more example?
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.
must have missed it, sorry! now i look like a doofus 😉
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.
No problem, anyway, I decided to add one more example to the docstring.
typing_inspect/typing_inspect.py
Outdated
from typing_inspect import is_generic | ||
""" | ||
|
||
# NOTE: This module must support Python 2.7 in addition to Python 3.x |
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 typing
modules?
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.
typing
on 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
typing_inspect/typing_inspect.py
Outdated
in the standard "typing" module. | ||
|
||
Example usage:: | ||
from typing_inspect import is_generic |
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_generic_type
get_last_args(Union) == () | ||
get_last_args(ClassVar[int]) == (int,) | ||
get_last_args(Union[T, int]) == (T, int) | ||
get_last_args(Iterable[Tuple[T, S]][int, T]) == (int, T) |
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.
Could you add an example here for Callable? It's not obvious to me what this will return for something like Callable[[int], str]
.
|
||
get_args(int) == () | ||
get_args(Union[int, Union[T, int], str][int]) == (int, str) | ||
get_args(Union[int, Tuple[T, int]][str]) == (int, (Tuple, str, int)) |
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.
Callable examples would be helpful here too.
typing_inspect/typing_inspect.py
Outdated
|
||
For more general tests use callable(), for more precise test use:: | ||
|
||
get_origin(tp) is Callable |
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.
What is the difference exactly between this construct and is_callable_type
? I think this could be explained more.
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.
the latter excludes subclasses, I added this to the docstring.
|
||
def test_tuple(self): | ||
samples = [Tuple, Tuple[str, int], Tuple[Iterable, ...]] | ||
nonsamples = [int, tuple, 42, List[int]] |
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.
@JelleZijlstra Thank you for review! I addressed your comments (and a bug that I found) in a new commit. |
What is blocking this PR? Having |
@JelleZijlstra
@gvanrossum |
I'm sorry, I did not realize you were waiting for my review. I still don't have time to do this justice, so I recommend that you make this a separate distro and iterate that way. Sorry! |
@gvanrossum @glyph @xgid @wsanchez Everyone is welcome to try it and comment on the API. Also please tell everyone else who might be interested, so that we can iterate fast. After the API is settled, some introspection functions may be added directly to |
As discussed in http://bugs.python.org/issue29262 here is a PR to add runtime inspection tools for typing types.
EDIT: It is no more WIP