Skip to content

Is it still necessary to prohibit generics at runtime? #303

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
ilevkivskyi opened this issue Oct 18, 2016 · 1 comment
Closed

Is it still necessary to prohibit generics at runtime? #303

ilevkivskyi opened this issue Oct 18, 2016 · 1 comment

Comments

@ilevkivskyi
Copy link
Member

There is a paragraph in PEP 484:

You cannot use the subscripted class (e.g. ``Node[int]``) directly in
an expression -- you must define a type alias.  (This restriction
exists because creating the subscripted class, e.g. ``Node[int]``, is
an expensive operation -- usually many times as expensive as
constructing an instance of it.  Using a type alias is also more
readable.)

However, __getitem__ is cached for generics now and this leads to significant speedup (Guido reported up to 300x speedup in extreme cases). I think the above restriction is not necessary anymore and could be removed from the PEP.

@gvanrossum
Copy link
Member

However mypy still prohibits it. I'd much rather remove this from mypy and leave it in the PEP than the other way around. ;-)

gvanrossum pushed a commit to python/mypy that referenced this issue Oct 24, 2016
As discussed in python/typing#303 generics in ``typing.py`` are now cached and are therefore much faster at runtime than before. Here I propose (and it looks like Guido is not against this) to allow generic types as runtime expressions.
gvanrossum pushed a commit to python/peps that referenced this issue Oct 24, 2016
Fixes python/typing#303.

See also python/mypy#2302 (which removes the restriction from mypy).

As a motivation, in Python one always can substitute expressions, so that if ``IntNode = Node[int]; IntNode()`` works, then it is reasonable to also allow ``Node[int]``, but say that the first way is preferred.
rowillia pushed a commit to rowillia/mypy that referenced this issue Oct 24, 2016
As discussed in python/typing#303 generics in ``typing.py`` are now cached and are therefore much faster at runtime than before. Here I propose (and it looks like Guido is not against this) to allow generic types as runtime expressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants