Soften restriction for runtime generics in PEP 484 (#120)
Fixes https://github.com/python/typing/issues/303. See also https://github.com/python/mypy/pull/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.
This commit is contained in:
parent
ae7020c262
commit
d309d32728
10
pep-0484.txt
10
pep-0484.txt
|
@ -600,12 +600,10 @@ the type (class) of the objects created by instantiating them doesn't
|
||||||
record the distinction. This behavior is called "type erasure"; it is
|
record the distinction. This behavior is called "type erasure"; it is
|
||||||
common practice in languages with generics (e.g. Java, TypeScript).
|
common practice in languages with generics (e.g. Java, TypeScript).
|
||||||
|
|
||||||
You cannot use the subscripted class (e.g. ``Node[int]``) directly in
|
It is not recommended to use the subscripted class (e.g. ``Node[int]``)
|
||||||
an expression -- you must define a type alias. (This restriction
|
directly in an expression -- using a type alias instead is preferred.
|
||||||
exists because creating the subscripted class, e.g. ``Node[int]``, is
|
(First, creating the subscripted class, e.g. ``Node[int]``, has a runtime
|
||||||
an expensive operation -- usually many times as expensive as
|
cost. Second, using a type alias is more readable.)
|
||||||
constructing an instance of it. Using a type alias is also more
|
|
||||||
readable.)
|
|
||||||
|
|
||||||
|
|
||||||
Arbitrary generic types as base classes
|
Arbitrary generic types as base classes
|
||||||
|
|
Loading…
Reference in New Issue