[issue26391] Specialized sub-classes of Generic never call __init__

Guido van Rossum report at bugs.python.org
Wed Mar 23 16:41:58 EDT 2016


Guido van Rossum added the comment:

You've hit a type where PEP 484 and mypy disagree.

The PEP shows a few examples of this:
https://www.python.org/dev/peps/pep-0484/#instantiating-generic-classes-and-type-erasure

However when I feed the examples from the PEP to mypy I get an error on each of the last two lines:

-----
from typing import TypeVar, Generic
T = TypeVar('T')
class Node(Generic[T]):
    pass
x = Node[T]()
y = Node[int]()
-----
b.py:5: error: Invalid type "b.T"
b.py:5: error: Generic type not valid as an expression any more (use '# type:' comment instead)
b.py:6: error: Generic type not valid as an expression any more (use '# type:' comment instead)

I suspect that mypy is right and the PEP didn't catch up to the change of mind in the flurry of activity right before acceptance.

Since it's provisional I'd like to change the PEP to disallow this.

At the same time at runtime I think it should either fail loudly or work, not silently return an uninitialized instance, so typing.py also needs to be fixed.

Thanks for the report!

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26391>
_______________________________________


More information about the Python-bugs-list mailing list