@@ -38,10 +38,9 @@ Type aliases
3838============
3939
4040A 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
5251Type 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
7069Note 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
158157For 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.
235235The :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
267267You 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
276277When 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
289291not 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
295297User 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
408411what one would normally do in idiomatic dynamically typed Python code.
409412For 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``
421424and ``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