Skip to content

Commit 2702b01

Browse files
erictrauthauntsaninjaAlexWaygood
authored
Updated the typing spec to use modern (PEP 695) generics syntax (#2089)
* Updated the typing spec to use modern (PEP 695) generics syntax * Update docs/spec/generics.rst Co-authored-by: Shantanu <[email protected]> * Update docs/spec/annotations.rst Co-authored-by: Alex Waygood <[email protected]> * Update docs/spec/annotations.rst Co-authored-by: Alex Waygood <[email protected]> * Incorporated code review feedback * Incorporated Jelle's review feedback * Addressed code review feedback from Jelle --------- Co-authored-by: Shantanu <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent edba79d commit 2702b01

File tree

12 files changed

+179
-240
lines changed

12 files changed

+179
-240
lines changed

docs/spec/aliases.rst

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,24 @@ Type aliases may be as complex as type hints in annotations --
3434
anything that is acceptable as a type hint is acceptable in a type
3535
alias::
3636

37-
from typing import TypeVar
3837
from collections.abc import Iterable
3938

40-
T = TypeVar('T', bound=float)
41-
Vector = Iterable[tuple[T, T]]
39+
type Vector[T: float] = Iterable[tuple[T, T]]
4240

43-
def inproduct(v: Vector[T]) -> T:
41+
def inproduct[T: float](v: Vector[T]) -> T:
4442
return sum(x*y for x, y in v)
45-
def dilate(v: Vector[T], scale: T) -> Vector[T]:
43+
def dilate[T: float](v: Vector[T], scale: T) -> Vector[T]:
4644
return ((x * scale, y * scale) for x, y in v)
4745
vec: Vector[float] = []
4846

4947

5048
This is equivalent to::
5149

52-
from typing import TypeVar
5350
from collections.abc import Iterable
5451

55-
T = TypeVar('T', bound=float)
56-
57-
def inproduct(v: Iterable[tuple[T, T]]) -> T:
52+
def inproduct[T: float](v: Iterable[tuple[T, T]]) -> T:
5853
return sum(x*y for x, y in v)
59-
def dilate(v: Iterable[tuple[T, T]], scale: T) -> Iterable[tuple[T, T]]:
54+
def dilate[T: float](v: Iterable[tuple[T, T]], scale: T) -> Iterable[tuple[T, T]]:
6055
return ((x * scale, y * scale) for x, y in v)
6156
vec: Iterable[tuple[float, float]] = []
6257

docs/spec/annotations.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,8 @@ In addition, the first argument in an instance method can be annotated
366366
with a type variable. In this case the return type may use the same
367367
type variable, thus making that method a generic function. For example::
368368

369-
T = TypeVar('T', bound='Copyable')
370369
class Copyable:
371-
def copy(self: T) -> T:
370+
def copy[T: Copyable](self: T) -> T:
372371
# return a copy of self
373372

374373
class C(Copyable): ...
@@ -378,10 +377,9 @@ type variable, thus making that method a generic function. For example::
378377
The same applies to class methods using ``type[]`` in an annotation
379378
of the first argument::
380379

381-
T = TypeVar('T', bound='C')
382380
class C:
383381
@classmethod
384-
def factory(cls: type[T]) -> T:
382+
def factory[T: C](cls: type[T]) -> T:
385383
# make a new instance of cls
386384

387385
class D(C): ...

docs/spec/class-compat.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ For example, annotating the discussed class::
101101
As a matter of convenience (and convention), instance variables can be
102102
annotated in ``__init__`` or other methods, rather than in the class::
103103

104-
from typing import Generic, TypeVar
105-
T = TypeVar('T')
106-
107-
class Box(Generic[T]):
108-
def __init__(self, content):
104+
class Box[T]:
105+
def __init__(self, content: T):
109106
self.content: T = content
110107

111108
``ClassVar`` cannot be used as a qualifier for a :ref:`TypedDict <typeddict>`

docs/spec/constructors.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,14 @@ these method evaluations, they should take on their default values.
318318

319319
::
320320

321-
T1 = TypeVar("T1")
322-
T2 = TypeVar("T2")
323-
T3 = TypeVar("T3", default=str)
321+
from typing import Any, Self, assert_type
324322

325-
class MyClass1(Generic[T1, T2]):
323+
class MyClass1[T1, T2]:
326324
def __new__(cls, x: T1) -> Self: ...
327325

328326
assert_type(MyClass1(1), MyClass1[int, Any])
329327

330-
class MyClass2(Generic[T1, T3]):
328+
class MyClass2[T1, T3 = str]:
331329
def __new__(cls, x: T1) -> Self: ...
332330

333331
assert_type(MyClass2(1), MyClass2[int, str])

docs/spec/dataclasses.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ Decorator function example
7272

7373
.. code-block:: python
7474
75-
_T = TypeVar("_T")
76-
7775
# The ``create_model`` decorator is defined by a library.
7876
# This could be in a type stub or inline.
7977
@typing.dataclass_transform()
80-
def create_model(cls: Type[_T]) -> Type[_T]:
78+
def create_model[T](cls: type[T]) -> type[T]:
8179
cls.__init__ = ...
8280
cls.__eq__ = ...
8381
cls.__ne__ = ...
@@ -148,7 +146,9 @@ customization of default behaviors:
148146

149147
.. code-block:: python
150148
151-
_T = TypeVar("_T")
149+
class _IdentityCallable(Protocol):
150+
def __call__[T](self, arg: T, /) -> T:
151+
...
152152
153153
def dataclass_transform(
154154
*,
@@ -158,7 +158,7 @@ customization of default behaviors:
158158
frozen_default: bool = False,
159159
field_specifiers: tuple[type | Callable[..., Any], ...] = (),
160160
**kwargs: Any,
161-
) -> Callable[[_T], _T]: ...
161+
) -> _IdentityCallable: ...
162162
163163
* ``eq_default`` indicates whether the ``eq`` parameter is assumed to
164164
be True or False if it is omitted by the caller. If not specified,

0 commit comments

Comments
 (0)