Skip to content

Commit 1405f01

Browse files
committed
Address reviewer comments.
1 parent 599d41a commit 1405f01

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

docs/spec/generics.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ This is equivalent to omitting the generic notation and just saying
9393
User-defined generic types
9494
--------------------------
9595

96-
You can define a user-defined class as generic by including a ``Generic``
97-
base class, either explicitly or implicitly via the ``Protocol[T]``
98-
shorthand for :ref:`generic protocols<generic-protocols>`. Example::
96+
You can define a user-defined class as generic in three ways: by including a
97+
``Generic`` base class, by using the new generic class syntax in Python 3.12
98+
and higher, or by including a ``Protocol`` base class parameterized with type
99+
variables. The third approach also marks the class as a protocol - see
100+
:ref:`generic protocols<generic-protocols>` for more information.
101+
102+
Example using ``Generic``::
99103

100104
from typing import TypeVar, Generic
101105
from logging import Logger
@@ -119,14 +123,14 @@ shorthand for :ref:`generic protocols<generic-protocols>`. Example::
119123
def log(self, message: str) -> None:
120124
self.logger.info('{}: {}'.format(self.name, message))
121125

122-
Or, in Python 3.12 and higher, by using the new syntax for generic
123-
classes::
126+
Or, using the new generic class syntax::
124127

125128
class LoggedVar[T]:
126129
# methods as in previous example
127130

128131
This implicitly adds ``Generic[T]`` as a base class and type checkers
129-
should treat the two largely equivalently (except for variance, see below).
132+
should treat the two definitions of ``LoggedVar`` largely equivalently (except
133+
for variance, see below).
130134

131135
``Generic[T]`` as a base class defines that the class ``LoggedVar``
132136
takes a single type parameter ``T``. This also makes ``T`` valid as

docs/spec/protocol.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,15 @@ non-protocol generic types::
272272
...
273273

274274
``Protocol[T, S, ...]`` is allowed as a shorthand for
275-
``Protocol, Generic[T, S, ...]``. It is an error to include both
276-
``Protocol[T, S, ...]`` and ``Generic[T, S, ...]`` in a class definition.
275+
``Protocol, Generic[T, S, ...]``. It is an error to combine
276+
``Protocol[T, S, ...]`` with ``Generic[T, S, ...]``, or with the new syntax for
277+
generic classes in Python 3.12 and above::
278+
279+
class Iterable(Protocol[T], Generic[T]): # INVALID
280+
...
281+
282+
class Iterable[T](Protocol[T]): # INVALID
283+
...
277284

278285
User-defined generic protocols support explicitly declared variance.
279286
Type checkers will warn if the inferred variance is different from

0 commit comments

Comments
 (0)