Skip to content

Commit 978b20d

Browse files
committed
Incorporated code review feedback
1 parent fb21e7a commit 978b20d

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

docs/spec/dataclasses.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ customization of default behaviors:
146146

147147
.. code-block:: python
148148
149-
def dataclass_transform[T](
149+
class _IdentityCallable(Protocol):
150+
def __call__[T](self, arg: T, /) -> T:
151+
...
152+
153+
def dataclass_transform(
150154
*,
151155
eq_default: bool = True,
152156
order_default: bool = False,
153157
kw_only_default: bool = False,
154158
frozen_default: bool = False,
155159
field_specifiers: tuple[type | Callable[..., Any], ...] = (),
156160
**kwargs: Any,
157-
) -> Callable[[T], T]: ...
161+
) -> _IdentityCallable: ...
158162
159163
* ``eq_default`` indicates whether the ``eq`` parameter is assumed to
160164
be True or False if it is omitted by the caller. If not specified,

docs/spec/generics.rst

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,22 @@ defined class is generic if you subclass one or more other generic classes and
210210
specify type variables for their parameters. See :ref:`generic-base-classes`
211211
for details.
212212

213-
You can use multiple inheritance with generic classes::
213+
You can use multiple inheritance with ``Generic``::
214214

215-
from collections.abc import Container, Iterable, Sized
215+
from typing import TypeVar, Generic
216+
from collections.abc import Sized, Iterable, Container
217+
218+
T = TypeVar('T')
216219

217-
class LinkedList[T](Sized):
220+
class LinkedList(Sized, Generic[T]):
218221
...
219222

220-
class MyMapping[K, V](Iterable[tuple[K, V]],
221-
Container[tuple[K, V]]):
223+
K = TypeVar('K')
224+
V = TypeVar('V')
225+
226+
class MyMapping(Iterable[tuple[K, V]],
227+
Container[tuple[K, V]],
228+
Generic[K, V]):
222229
...
223230

224231
Subclassing a generic class without specifying type parameters assumes
@@ -240,7 +247,7 @@ Scoping rules for type variables
240247

241248
When using the generic class syntax introduced in Python 3.12, the location of
242249
its declaration defines its scope. When using the older syntax, the scoping
243-
rules are more subtle and complex.
250+
rules are more subtle and complex:
244251

245252
* A type variable used in a generic function could be inferred to represent
246253
different types in the same code block. Example::
@@ -662,7 +669,7 @@ inline by prefixing its name with ``**`` inside a generic parameter list
662669
class CallbackWrapper[T, **P]:
663670
callback: Callable[P, T]
664671
665-
Prior to 3.12, the ``ParamSpec`` constructor can be used::
672+
Prior to 3.12, the ``ParamSpec`` constructor can be used.
666673

667674
.. code-block::
668675
@@ -699,8 +706,8 @@ parameter specification variable (``Callable[Concatenate[int, P], int]``\ ).
699706
"]"
700707
701708
where ``parameter_specification_variable`` is introduced either inline (using
702-
``**``) or via ``typing.ParamSpec`` as shown above, and ``concatenate`` is
703-
``typing.Concatenate``.
709+
``**`` in a type parameter list) or via ``typing.ParamSpec`` as shown above,
710+
and ``concatenate`` is ``typing.Concatenate``.
704711

705712
As before, ``parameters_expression``\ s by themselves are not acceptable in
706713
places where a type is expected
@@ -1086,7 +1093,7 @@ type such as ``int``, a type variable *tuple* is a stand-in for a *tuple* type s
10861093
``tuple[int, str]``.
10871094

10881095
In Python 3.12 and newer, type variable tuples can be introduced inline by prefixing
1089-
their name with ``*`` inside a generic parameter list.
1096+
their name with ``*`` inside a type parameter list.
10901097

10911098
::
10921099

docs/spec/overload.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ arguments depending on the type of the callable::
4848
@overload
4949
def map[T1, T2, S](
5050
func: Callable[[T1, T2], S],
51-
iter1: Iterable[T1], iter2: Iterable[T2],
51+
iter1: Iterable[T1],
52+
iter2: Iterable[T2],
5253
) -> Iterator[S]: ...
5354
# ... and we could add more items to support more than two iterables
5455

@@ -91,7 +92,7 @@ A constrained ``TypeVar`` type can sometimes be used instead of
9192
using the ``@overload`` decorator. For example, the definitions
9293
of ``concat1`` and ``concat2`` in this stub file are equivalent::
9394

94-
def concat1[AnyStr: (str, bytes)](x: AnyStr, y: AnyStr) -> AnyStr: ...
95+
def concat1[S: (str, bytes)](x: S, y: S) -> S: ...
9596

9697
@overload
9798
def concat2(x: str, y: str) -> str: ...
@@ -103,13 +104,13 @@ be represented precisely using type variables. We
103104
recommend that ``@overload`` is only used in cases where a type
104105
variable is not sufficient.
105106

106-
Another important difference between type variables such as ``AnyStr``
107-
and using ``@overload`` is that the prior can also be used to define
108-
constraints for generic class type parameters. For example, the type
109-
parameter of the generic class ``typing.IO`` is constrained (only
110-
``IO[str]``, ``IO[bytes]`` and ``IO[Any]`` are valid)::
107+
Another important difference between type variables and an ``@overload``
108+
is that the former can also be used to define constraints for generic
109+
class type parameters. For example, the type parameter of the generic
110+
class ``typing.IO`` is constrained (only ``IO[str]``, ``IO[bytes]``
111+
and ``IO[Any]`` are valid)::
111112

112-
class IO[AnyStr: (str, bytes)]: ...
113+
class IO[S: (str, bytes)]: ...
113114

114115

115116
Invalid overload definitions

docs/spec/protocol.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,12 @@ non-protocol generic types::
271271
def __iter__(self) -> Iterator[T]:
272272
...
273273

274-
The older syntax ``Protocol[T, S, ...]`` remains available as a shorthand for
275-
``Protocol, Generic[T, S, ...]``. It is an error to combine the shorthand with
276-
``Generic[T, S, ...]`` or to mix it with the new ``class Iterable[T]`` form::
274+
The older (pre-3.12) syntax ``Protocol[T, S, ...]`` remains available as a
275+
shorthand for ``Protocol, Generic[T, S, ...]``. It is an error to combine the
276+
shorthand with ``Generic[T, S, ...]`` or to mix it with the new
277+
``class Iterable[T]`` form::
277278

278-
class Iterable[T](Protocol, Generic[T]): # INVALID
279+
class Iterable(Protocol[T], Generic[T]): # INVALID
279280
...
280281

281282
class Iterable[T](Protocol[T]): # INVALID

0 commit comments

Comments
 (0)