Skip to content
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

Closed
wants to merge 12 commits into from
Closed

Typing inspect #377

wants to merge 12 commits into from

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Jan 28, 2017

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

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))

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.

Copy link
Member Author

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.

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

Copy link
Member Author

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?

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 😉

Copy link
Member Author

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.

from typing_inspect import is_generic
"""

# NOTE: This module must support Python 2.7 in addition to Python 3.x

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?

Copy link
Member Author

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).

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

@ilevkivskyi ilevkivskyi changed the title [WIP] Typing inspect Typing inspect Jan 30, 2017
in the standard "typing" module.

Example usage::
from typing_inspect import is_generic
Copy link
Member

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)
Copy link
Member

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))
Copy link
Member

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.


For more general tests use callable(), for more precise test use::

get_origin(tp) is Callable
Copy link
Member

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.

Copy link
Member Author

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]]
Copy link
Member

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.

@ilevkivskyi
Copy link
Member Author

@JelleZijlstra Thank you for review! I addressed your comments (and a bug that I found) in a new commit.

@JelleZijlstra
Copy link
Member

What is blocking this PR? Having typing_inspect available would be useful to me.

@ilevkivskyi
Copy link
Member Author

@JelleZijlstra
Actually nothing blocks this. I was just waiting for an approval from @gvanrossum. We discussed this in http://bugs.python.org/issue29262 beginning of this year and the plan was following:

  • Make few releases of typing_inspect as a separate package on PyPI
  • Listen to feedback and tune API
  • Eventually include this in typing

@gvanrossum
Is this still the plan? Would you like to make a review?

@gvanrossum
Copy link
Member

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!

@ilevkivskyi
Copy link
Member Author

@gvanrossum @glyph @xgid @wsanchez
OK, as Guido proposed, I am closing this PR. The typing_inspect is now published as a separate package:
https://pypi.python.org/pypi/typing-inspect
https://github.com/ilevkivskyi/typing_inspect

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 typing.

@ilevkivskyi ilevkivskyi closed this May 9, 2017
@ilevkivskyi ilevkivskyi deleted the typing-inspect branch May 9, 2017 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants