Skip to content

Commit 0dfb718

Browse files
Update various references to deprecated type aliases in docs (#17829)
Use `collections.abc.Iterable`, for example, instead of `typing.Iterable`, unless we are discussing support for older Python versions. The latter has been deprecated since Python 3.9. Also update a few other out-of-date things that I happened to notice. Part of the motivation is that Python 3.8 will reach end of life in October, so we can soon start mostly assuming that users are on 3.9 or newer (even if we'll continue supporting 3.8 for a while still). --------- Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 6336ce1 commit 0dfb718

17 files changed

+161
-124
lines changed

docs/source/additional_features.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,20 @@ Extended Callable types
363363
This feature is deprecated. You can use
364364
:ref:`callback protocols <callback_protocols>` as a replacement.
365365

366-
As an experimental mypy extension, you can specify :py:data:`~typing.Callable` types
366+
As an experimental mypy extension, you can specify :py:class:`~collections.abc.Callable` types
367367
that support keyword arguments, optional arguments, and more. When
368-
you specify the arguments of a :py:data:`~typing.Callable`, you can choose to supply just
368+
you specify the arguments of a :py:class:`~collections.abc.Callable`, you can choose to supply just
369369
the type of a nameless positional argument, or an "argument specifier"
370370
representing a more complicated form of argument. This allows one to
371371
more closely emulate the full range of possibilities given by the
372372
``def`` statement in Python.
373373

374374
As an example, here's a complicated function definition and the
375-
corresponding :py:data:`~typing.Callable`:
375+
corresponding :py:class:`~collections.abc.Callable`:
376376

377377
.. code-block:: python
378378
379-
from typing import Callable
379+
from collections.abc import Callable
380380
from mypy_extensions import (Arg, DefaultArg, NamedArg,
381381
DefaultNamedArg, VarArg, KwArg)
382382
@@ -449,7 +449,7 @@ purpose:
449449
In all cases, the ``type`` argument defaults to ``Any``, and if the
450450
``name`` argument is omitted the argument has no name (the name is
451451
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
452-
:py:data:`~typing.Callable` such as
452+
:py:class:`~collections.abc.Callable` such as
453453

454454
.. code-block:: python
455455
@@ -461,7 +461,7 @@ is equivalent to the following:
461461
462462
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float]
463463
464-
A :py:data:`~typing.Callable` with unspecified argument types, such as
464+
A :py:class:`~collections.abc.Callable` with unspecified argument types, such as
465465

466466
.. code-block:: python
467467

docs/source/cheat_sheet_py3.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ Functions
8888

8989
.. code-block:: python
9090
91-
from typing import Callable, Iterator, Union, Optional
91+
from collections.abc import Iterator, Callable
92+
from typing import Union, Optional
9293
9394
# This is how you annotate a function definition
9495
def stringify(num: int) -> str:
@@ -274,7 +275,8 @@ that are common in idiomatic Python are standardized.
274275

275276
.. code-block:: python
276277
277-
from typing import Mapping, MutableMapping, Sequence, Iterable
278+
from collections.abc import Mapping, MutableMapping, Sequence, Iterable
279+
# or 'from typing import ...' (required in Python 3.8)
278280
279281
# Use Iterable for generic iterables (anything usable in "for"),
280282
# and Sequence where a sequence (supporting "len" and "__getitem__") is
@@ -354,7 +356,8 @@ syntax:
354356

355357
.. code-block:: python
356358
357-
from typing import Any, Callable
359+
from collections.abc import Callable
360+
from typing import Any
358361
359362
def bare_decorator[F: Callable[..., Any]](func: F) -> F:
360363
...
@@ -366,7 +369,8 @@ The same example using pre-3.12 syntax:
366369

367370
.. code-block:: python
368371
369-
from typing import Any, Callable, TypeVar
372+
from collections.abc import Callable
373+
from typing import Any, TypeVar
370374
371375
F = TypeVar('F', bound=Callable[..., Any])
372376

docs/source/class_basics.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ between class and instance variables with callable types. For example:
152152

153153
.. code-block:: python
154154
155-
from typing import Callable, ClassVar
155+
from collections.abc import Callable
156+
from typing import ClassVar
156157
157158
class A:
158159
foo: Callable[[int], None]

docs/source/command_line.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,11 @@ of the above sections.
630630

631631
.. code-block:: python
632632
633-
from typing import Text
634-
635633
items: list[int]
636634
if 'some string' in items: # Error: non-overlapping container check!
637635
...
638636
639-
text: Text
637+
text: str
640638
if text != b'other bytes': # Error: non-overlapping equality check!
641639
...
642640

docs/source/common_issues.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ explicit type cast:
363363

364364
.. code-block:: python
365365
366-
from typing import Sequence, cast
366+
from collections.abc import Sequence
367+
from typing import cast
367368
368369
def find_first_str(a: Sequence[object]) -> str:
369370
index = next((i for i, s in enumerate(a) if isinstance(s, str)), -1)
@@ -700,7 +701,7 @@ This example demonstrates both safe and unsafe overrides:
700701

701702
.. code-block:: python
702703
703-
from typing import Sequence, List, Iterable
704+
from collections.abc import Sequence, Iterable
704705
705706
class A:
706707
def test(self, t: Sequence[int]) -> Sequence[str]:
@@ -713,7 +714,7 @@ This example demonstrates both safe and unsafe overrides:
713714
714715
class NarrowerArgument(A):
715716
# A more specific argument type isn't accepted
716-
def test(self, t: List[int]) -> Sequence[str]: # Error
717+
def test(self, t: list[int]) -> Sequence[str]: # Error
717718
...
718719
719720
class NarrowerReturn(A):

docs/source/dynamic_typing.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ treated as ``Any``:
8181

8282
.. code-block:: python
8383
84-
from typing import List
85-
86-
def f(x: List) -> None:
84+
def f(x: list) -> None:
8785
reveal_type(x) # Revealed type is "builtins.list[Any]"
8886
reveal_type(x[0]) # Revealed type is "Any"
8987
x[0].anything_goes() # OK

docs/source/error_code_list.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ Example:
124124

125125
.. code-block:: python
126126
127-
from typing import Sequence
128-
129127
def greet(name: str) -> None:
130128
print('hello', name)
131129
@@ -210,11 +208,11 @@ This example incorrectly uses the function ``log`` as a type:
210208
for x in objs:
211209
f(x)
212210
213-
You can use :py:data:`~typing.Callable` as the type for callable objects:
211+
You can use :py:class:`~collections.abc.Callable` as the type for callable objects:
214212

215213
.. code-block:: python
216214
217-
from typing import Callable
215+
from collections.abc import Callable
218216
219217
# OK
220218
def log_all(objs: list[object], f: Callable[[object], None]) -> None:

docs/source/error_code_list2.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ example:
270270
271271
# mypy: enable-error-code="possibly-undefined"
272272
273-
from typing import Iterable
273+
from collections.abc import Iterable
274274
275275
def test(values: Iterable[int], flag: bool) -> None:
276276
if flag:
@@ -318,7 +318,7 @@ Example:
318318

319319
.. code-block:: python
320320
321-
from typing import Iterable
321+
from collections.abc import Iterable
322322
323323
def transform(items: Iterable[int]) -> list[int]:
324324
# Error: "items" has type "Iterable[int]" which can always be true in boolean context. Consider using "Collection[int]" instead. [truthy-iterable]

docs/source/faq.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ Structural subtyping can be thought of as "static duck typing".
102102
Some argue that structural subtyping is better suited for languages with duck
103103
typing such as Python. Mypy however primarily uses nominal subtyping,
104104
leaving structural subtyping mostly opt-in (except for built-in protocols
105-
such as :py:class:`~typing.Iterable` that always support structural subtyping). Here are some
106-
reasons why:
105+
such as :py:class:`~collections.abc.Iterable` that always support structural
106+
subtyping). Here are some reasons why:
107107

108108
1. It is easy to generate short and informative error messages when
109109
using a nominal type system. This is especially important when
@@ -140,13 +140,14 @@ How are mypy programs different from normal Python?
140140
Since you use a vanilla Python implementation to run mypy programs,
141141
mypy programs are also Python programs. The type checker may give
142142
warnings for some valid Python code, but the code is still always
143-
runnable. Also, some Python features and syntax are still not
143+
runnable. Also, a few Python features are still not
144144
supported by mypy, but this is gradually improving.
145145

146146
The obvious difference is the availability of static type
147147
checking. The section :ref:`common_issues` mentions some
148148
modifications to Python code that may be required to make code type
149-
check without errors. Also, your code must make attributes explicit.
149+
check without errors. Also, your code must make defined
150+
attributes explicit.
150151

151152
Mypy supports modular, efficient type checking, and this seems to
152153
rule out type checking some language features, such as arbitrary

docs/source/generics.rst

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ Let us illustrate this by few simple examples:
591591
Covariance should feel relatively intuitive, but contravariance and invariance
592592
can be harder to reason about.
593593

594-
* :py:data:`~typing.Callable` is an example of type that behaves contravariant
594+
* :py:class:`~collections.abc.Callable` is an example of type that behaves contravariant
595595
in types of arguments. That is, ``Callable[[Shape], int]`` is a subtype of
596596
``Callable[[Triangle], int]``, despite ``Shape`` being a supertype of
597597
``Triangle``. To understand this, consider:
@@ -833,7 +833,8 @@ Here's how one could annotate the decorator (Python 3.12 syntax):
833833

834834
.. code-block:: python
835835
836-
from typing import Any, Callable, cast
836+
from collections.abc import Callable
837+
from typing import Any, cast
837838
838839
# A decorator that preserves the signature.
839840
def printing_decorator[F: Callable[..., Any]](func: F) -> F:
@@ -854,7 +855,8 @@ Here is the example using the legacy syntax (Python 3.11 and earlier):
854855

855856
.. code-block:: python
856857
857-
from typing import Any, Callable, TypeVar, cast
858+
from collections.abc import Callable
859+
from typing import Any, TypeVar, cast
858860
859861
F = TypeVar('F', bound=Callable[..., Any])
860862
@@ -887,7 +889,7 @@ for a more faithful type annotation (Python 3.12 syntax):
887889

888890
.. code-block:: python
889891
890-
from typing import Callable
892+
from collections.abc import Callable
891893
892894
def printing_decorator[**P, T](func: Callable[P, T]) -> Callable[P, T]:
893895
def wrapper(*args: P.args, **kwds: P.kwargs) -> T:
@@ -900,7 +902,8 @@ The same is possible using the legacy syntax with :py:class:`~typing.ParamSpec`
900902

901903
.. code-block:: python
902904
903-
from typing import Callable, TypeVar
905+
from collections.abc import Callable
906+
from typing import TypeVar
904907
from typing_extensions import ParamSpec
905908
906909
P = ParamSpec('P')
@@ -917,7 +920,7 @@ alter the signature of the input function (Python 3.12 syntax):
917920

918921
.. code-block:: python
919922
920-
from typing import Callable
923+
from collections.abc import Callable
921924
922925
# We reuse 'P' in the return type, but replace 'T' with 'str'
923926
def stringify[**P, T](func: Callable[P, T]) -> Callable[P, str]:
@@ -937,7 +940,8 @@ Here is the above example using the legacy syntax (Python 3.11 and earlier):
937940

938941
.. code-block:: python
939942
940-
from typing import Callable, TypeVar
943+
from collections.abc import Callable
944+
from typing import TypeVar
941945
from typing_extensions import ParamSpec
942946
943947
P = ParamSpec('P')
@@ -953,7 +957,8 @@ You can also insert an argument in a decorator (Python 3.12 syntax):
953957

954958
.. code-block:: python
955959
956-
from typing import Callable, Concatenate
960+
from collections.abc import Callable
961+
from typing import Concatenate
957962
958963
def printing_decorator[**P, T](func: Callable[P, T]) -> Callable[Concatenate[str, P], T]:
959964
def wrapper(msg: str, /, *args: P.args, **kwds: P.kwargs) -> T:
@@ -971,7 +976,8 @@ Here is the same function using the legacy syntax (Python 3.11 and earlier):
971976

972977
.. code-block:: python
973978
974-
from typing import Callable, TypeVar
979+
from collections.abc import Callable
980+
from typing import TypeVar
975981
from typing_extensions import Concatenate, ParamSpec
976982
977983
P = ParamSpec('P')
@@ -993,7 +999,8 @@ similarly supported via generics (Python 3.12 syntax):
993999

9941000
.. code-block:: python
9951001
996-
from typing import Any, Callable
1002+
from colletions.abc import Callable
1003+
from typing import Any
9971004
9981005
def route[F: Callable[..., Any]](url: str) -> Callable[[F], F]:
9991006
...
@@ -1011,7 +1018,8 @@ Here is the example using the legacy syntax (Python 3.11 and earlier):
10111018

10121019
.. code-block:: python
10131020
1014-
from typing import Any, Callable, TypeVar
1021+
from collections.abc import Callable
1022+
from typing import Any, TypeVar
10151023
10161024
F = TypeVar('F', bound=Callable[..., Any])
10171025
@@ -1027,7 +1035,8 @@ achieved by combining with :py:func:`@overload <typing.overload>` (Python 3.12 s
10271035

10281036
.. code-block:: python
10291037
1030-
from typing import Any, Callable, overload
1038+
from collections.abc import Callable
1039+
from typing import Any, overload
10311040
10321041
# Bare decorator usage
10331042
@overload
@@ -1057,7 +1066,8 @@ Here is the decorator from the example using the legacy syntax
10571066

10581067
.. code-block:: python
10591068
1060-
from typing import Any, Callable, Optional, TypeVar, overload
1069+
from collections.abc import Callable
1070+
from typing import Any, Optional, TypeVar, overload
10611071
10621072
F = TypeVar('F', bound=Callable[..., Any])
10631073
@@ -1077,7 +1087,7 @@ Generic protocols
10771087

10781088
Mypy supports generic protocols (see also :ref:`protocol-types`). Several
10791089
:ref:`predefined protocols <predefined_protocols>` are generic, such as
1080-
:py:class:`Iterable[T] <typing.Iterable>`, and you can define additional
1090+
:py:class:`Iterable[T] <collections.abc.Iterable>`, and you can define additional
10811091
generic protocols. Generic protocols mostly follow the normal rules for
10821092
generic classes. Example (Python 3.12 syntax):
10831093

@@ -1200,7 +1210,7 @@ type aliases (it also supports non-generic type aliases):
12001210

12011211
.. code-block:: python
12021212
1203-
from typing import Iterable, Callable
1213+
from collections.abc import Callable, Iterable
12041214
12051215
type TInt[S] = tuple[int, S]
12061216
type UInt[S] = S | int

0 commit comments

Comments
 (0)