From cf6962bc2060e5e6e716d09167578d1d809ac0c9 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 22 Mar 2016 08:56:19 -0700 Subject: [PATCH] Clarify what's allowed in the base classes when making a new generic class. --- pep-0484.txt | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pep-0484.txt b/pep-0484.txt index 5d6f87c9a..ac0a04de8 100644 --- a/pep-0484.txt +++ b/pep-0484.txt @@ -403,15 +403,39 @@ thus invalid:: class Pair(Generic[T, T]): # INVALID ... +The ``Generic[T]`` base class is redundant in simple cases where you +subclass some other generic class and specify type variables for its +parameters:: + + from typing import TypeVar, Iterator + + T = TypeVar('T') + + class MyIter(Iterator[T]): + ... + +That class definition is equivalent to:: + + class MyIter(Iterator[T], Generic[T]): + ... + You can use multiple inheritance with ``Generic``:: - from typing import TypeVar, Generic, Sized + from typing import TypeVar, Generic, Sized, Iterable, Container, Tuple T = TypeVar('T') class LinkedList(Sized, Generic[T]): ... + K = TypeVar('K') + V = TypeVar('V') + + class MyMapping(Iterable[Tuple[K, V]], + Container[Tuple[K, V]], + Generic[K, V]): + ... + Subclassing a generic class without specifying type parameters assumes ``Any`` for each position. In the following example, ``MyIterable`` is not generic but implicitly inherits from ``Iterable[Any]``::