Skip to content

Commit d309d32

Browse files
ilevkivskyigvanrossum
authored andcommitted
Soften restriction for runtime generics in PEP 484 (#120)
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.
1 parent ae7020c commit d309d32

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

pep-0484.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,10 @@ the type (class) of the objects created by instantiating them doesn't
600600
record the distinction. This behavior is called "type erasure"; it is
601601
common practice in languages with generics (e.g. Java, TypeScript).
602602

603-
You cannot use the subscripted class (e.g. ``Node[int]``) directly in
604-
an expression -- you must define a type alias. (This restriction
605-
exists because creating the subscripted class, e.g. ``Node[int]``, is
606-
an expensive operation -- usually many times as expensive as
607-
constructing an instance of it. Using a type alias is also more
608-
readable.)
603+
It is not recommended to use the subscripted class (e.g. ``Node[int]``)
604+
directly in an expression -- using a type alias instead is preferred.
605+
(First, creating the subscripted class, e.g. ``Node[int]``, has a runtime
606+
cost. Second, using a type alias is more readable.)
609607

610608

611609
Arbitrary generic types as base classes

0 commit comments

Comments
 (0)