Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bdf46bc

Browse filesBrowse files
[doc] Leverage the fact that the actual types can now be indexed for typing (GH-22340)
This shows users that they can use the actual types. Using deprecated types is confusing. This also prefers colections.abc.Sized instead of the alias typing.Sized. I guess the aliases were created to make it convenient to import all collections related types from the same place. This should be backported to 3.9. Automerge-Triggered-By: @gvanrossum (cherry picked from commit d9ab95f) Co-authored-by: Andre Delfino <adelfino@gmail.com>
1 parent 8b2595f commit bdf46bc
Copy full SHA for bdf46bc

File tree

2 files changed

+29
-30
lines changed
Filter options

2 files changed

+29
-30
lines changed

‎Doc/glossary.rst

Copy file name to clipboardExpand all lines: Doc/glossary.rst
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,19 +1084,15 @@ Glossary
10841084
Type aliases are useful for simplifying :term:`type hints <type hint>`.
10851085
For example::
10861086

1087-
from typing import List, Tuple
1088-
10891087
def remove_gray_shades(
1090-
colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
1088+
colors: list[tuple[int, int, int]]) -> list[tuple[int, int, int]]:
10911089
pass
10921090

10931091
could be made more readable like this::
10941092

1095-
from typing import List, Tuple
1096-
1097-
Color = Tuple[int, int, int]
1093+
Color = tuple[int, int, int]
10981094

1099-
def remove_gray_shades(colors: List[Color]) -> List[Color]:
1095+
def remove_gray_shades(colors: list[Color]) -> list[Color]:
11001096
pass
11011097

11021098
See :mod:`typing` and :pep:`484`, which describe this functionality.

‎Doc/library/typing.rst

Copy file name to clipboardExpand all lines: Doc/library/typing.rst
+26-23Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ Type aliases
3838
============
3939

4040
A type alias is defined by assigning the type to the alias. In this example,
41-
``Vector`` and ``List[float]`` will be treated as interchangeable synonyms::
41+
``Vector`` and ``list[float]`` will be treated as interchangeable synonyms::
4242

43-
from typing import List
44-
Vector = List[float]
43+
Vector = list[float]
4544

4645
def scale(scalar: float, vector: Vector) -> Vector:
4746
return [scalar * num for num in vector]
@@ -51,11 +50,11 @@ A type alias is defined by assigning the type to the alias. In this example,
5150

5251
Type aliases are useful for simplifying complex type signatures. For example::
5352

54-
from typing import Dict, Tuple, Sequence
53+
from collections.abc import Sequence
5554

56-
ConnectionOptions = Dict[str, str]
57-
Address = Tuple[str, int]
58-
Server = Tuple[Address, ConnectionOptions]
55+
ConnectionOptions = dict[str, str]
56+
Address = tuple[str, int]
57+
Server = tuple[Address, ConnectionOptions]
5958

6059
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
6160
...
@@ -64,7 +63,7 @@ Type aliases are useful for simplifying complex type signatures. For example::
6463
# being exactly equivalent to this one.
6564
def broadcast_message(
6665
message: str,
67-
servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
66+
servers: Sequence[tuple[tuple[str, int], dict[str, str]]]) -> None:
6867
...
6968

7069
Note that ``None`` as a type hint is a special case and is replaced by
@@ -157,7 +156,7 @@ type hinted using ``Callable[[Arg1Type, Arg2Type], ReturnType]``.
157156

158157
For example::
159158

160-
from typing import Callable
159+
from collections.abc import Callable
161160

162161
def feeder(get_next_item: Callable[[], str]) -> None:
163162
# Body
@@ -181,7 +180,7 @@ subscription to denote expected types for container elements.
181180

182181
::
183182

184-
from typing import Mapping, Sequence
183+
from collections.abc import Mapping, Sequence
185184

186185
def notify_by_email(employees: Sequence[Employee],
187186
overrides: Mapping[str, str]) -> None: ...
@@ -191,7 +190,8 @@ called :class:`TypeVar`.
191190

192191
::
193192

194-
from typing import Sequence, TypeVar
193+
from collections.abc import Sequence
194+
from typing import TypeVar
195195

196196
T = TypeVar('T') # Declare type variable
197197

@@ -235,7 +235,7 @@ class body.
235235
The :class:`Generic` base class defines :meth:`__class_getitem__` so that
236236
``LoggedVar[t]`` is valid as a type::
237237

238-
from typing import Iterable
238+
from collections.abc import Iterable
239239

240240
def zero_all_vars(vars: Iterable[LoggedVar[int]]) -> None:
241241
for var in vars:
@@ -266,7 +266,8 @@ This is thus invalid::
266266

267267
You can use multiple inheritance with :class:`Generic`::
268268

269-
from typing import TypeVar, Generic, Sized
269+
from collections.abc import Sized
270+
from typing import TypeVar, Generic
270271

271272
T = TypeVar('T')
272273

@@ -275,7 +276,8 @@ You can use multiple inheritance with :class:`Generic`::
275276

276277
When inheriting from generic classes, some type variables could be fixed::
277278

278-
from typing import TypeVar, Mapping
279+
from collections.abc import Mapping
280+
from typing import TypeVar
279281

280282
T = TypeVar('T')
281283

@@ -288,13 +290,14 @@ Using a generic class without specifying type parameters assumes
288290
:data:`Any` for each position. In the following example, ``MyIterable`` is
289291
not generic but implicitly inherits from ``Iterable[Any]``::
290292

291-
from typing import Iterable
293+
from collections.abc import Iterable
292294

293295
class MyIterable(Iterable): # Same as Iterable[Any]
294296

295297
User defined generic type aliases are also supported. Examples::
296298

297-
from typing import TypeVar, Iterable, Tuple, Union
299+
from collections.abc import Iterable
300+
from typing import TypeVar, Union
298301
S = TypeVar('S')
299302
Response = Union[Iterable[S], int]
300303

@@ -303,9 +306,9 @@ User defined generic type aliases are also supported. Examples::
303306
...
304307

305308
T = TypeVar('T', int, float, complex)
306-
Vec = Iterable[Tuple[T, T]]
309+
Vec = Iterable[tuple[T, T]]
307310

308-
def inproduct(v: Vec[T]) -> T: # Same as Iterable[Tuple[T, T]]
311+
def inproduct(v: Vec[T]) -> T: # Same as Iterable[tuple[T, T]]
309312
return sum(x*y for x, y in v)
310313

311314
.. versionchanged:: 3.7
@@ -408,7 +411,7 @@ to be explicitly marked to support them, which is unpythonic and unlike
408411
what one would normally do in idiomatic dynamically typed Python code.
409412
For example, this conforms to the :pep:`484`::
410413

411-
from typing import Sized, Iterable, Iterator
414+
from collections.abc import Sized, Iterable, Iterator
412415

413416
class Bucket(Sized, Iterable[int]):
414417
...
@@ -421,7 +424,7 @@ allowing ``Bucket`` to be implicitly considered a subtype of both ``Sized``
421424
and ``Iterable[int]`` by static type checkers. This is known as
422425
*structural subtyping* (or static duck-typing)::
423426

424-
from typing import Iterator, Iterable
427+
from collections.abc import Iterator, Iterable
425428

426429
class Bucket: # Note: no base classes
427430
...
@@ -1371,10 +1374,10 @@ Asynchronous programming
13711374
The variance and order of type variables
13721375
correspond to those of :class:`Generator`, for example::
13731376

1374-
from typing import List, Coroutine
1375-
c = None # type: Coroutine[List[str], str, int]
1377+
from collections.abc import Coroutine
1378+
c = None # type: Coroutine[list[str], str, int]
13761379
...
1377-
x = c.send('hi') # type: List[str]
1380+
x = c.send('hi') # type: list[str]
13781381
async def bar() -> None:
13791382
x = await c # type: int
13801383

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.