Skip to content

Commit 48957aa

Browse files
committed
Various documentation updates (mostly about error codes) (#12680)
Document some additional error codes and some general doc updates for the 0.950 release.
1 parent b84cda9 commit 48957aa

File tree

5 files changed

+100
-14
lines changed

5 files changed

+100
-14
lines changed

docs/source/command_line.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,27 +616,29 @@ of the above sections.
616616
.. option:: --disable-error-code
617617

618618
This flag allows disabling one or multiple error codes globally.
619+
See :ref:`error-codes` for more information.
619620

620621
.. code-block:: python
621622
622623
# no flag
623624
x = 'a string'
624625
x.trim() # error: "str" has no attribute "trim" [attr-defined]
625626
626-
# --disable-error-code attr-defined
627+
# When using --disable-error-code attr-defined
627628
x = 'a string'
628629
x.trim()
629630
630631
.. option:: --enable-error-code
631632

632633
This flag allows enabling one or multiple error codes globally.
634+
See :ref:`error-codes` for more information.
633635

634-
Note: This flag will override disabled error codes from the --disable-error-code
635-
flag
636+
Note: This flag will override disabled error codes from the
637+
:option:`--disable-error-code <mypy --disable-error-code>` flag.
636638

637639
.. code-block:: python
638640
639-
# --disable-error-code attr-defined
641+
# When using --disable-error-code attr-defined
640642
x = 'a string'
641643
x.trim()
642644

docs/source/error_code_list.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,43 @@ implementation.
679679
def func(value):
680680
pass # actual implementation
681681
682+
Check that coroutine return value is used [unused-coroutine]
683+
------------------------------------------------------------
684+
685+
Mypy ensures that return values of async def functions are not
686+
ignored, as this is usually a programming error, as the coroutine
687+
won't be executed at the call site.
688+
689+
.. code-block:: python
690+
691+
async def f() -> None:
692+
...
693+
694+
async def g() -> None:
695+
f() # Error: missing await
696+
await f() # OK
697+
698+
You can work around this error by assigning the result to a temporary,
699+
otherwise unused variable:
700+
701+
.. code-block:: python
702+
703+
_ = f() # No error
704+
705+
Check types in assert_type [assert-type]
706+
----------------------------------------
707+
708+
The inferred type for an expression passed to ``assert_type`` must match
709+
the provided type.
710+
711+
.. code-block:: python
712+
713+
from typing_extensions import assert_type
714+
715+
assert_type([1], list[int]) # OK
716+
717+
assert_type([1], list[str]) # Error
718+
682719
Report syntax errors [syntax]
683720
-----------------------------
684721

docs/source/error_code_list2.rst

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ mypy generates an error if it thinks that an expression is redundant.
200200

201201
.. code-block:: python
202202
203-
# mypy: enable-error-code redundant-expr
203+
# Use "mypy --enable-error-code redundant-expr ..."
204204
205205
def example(x: int) -> None:
206206
# Error: Left operand of "and" is always true [redundant-expr]
@@ -222,7 +222,7 @@ since unless implemented by a sub-type, the expression will always evaluate to t
222222

223223
.. code-block:: python
224224
225-
# mypy: enable-error-code truthy-bool
225+
# Use "mypy --enable-error-code truthy-bool ..."
226226
227227
class Foo:
228228
pass
@@ -237,7 +237,8 @@ This check might falsely imply an error. For example, ``Iterable`` does not impl
237237

238238
.. code-block:: python
239239
240-
# mypy: enable-error-code truthy-bool
240+
# Use "mypy -enable-error-code truthy-bool ..."
241+
241242
from typing import Iterable
242243
243244
def transform(items: Iterable[int]) -> Iterable[int]:
@@ -270,7 +271,7 @@ Example:
270271

271272
.. code-block:: python
272273
273-
# mypy: enable-error-code ignore-without-code
274+
# Use "mypy --enable-error-code ignore-without-code ..."
274275
275276
class Foo:
276277
def __init__(self, name: str) -> None:
@@ -288,3 +289,32 @@ Example:
288289
# This line warns correctly about the typo in the attribute name
289290
# Error: "Foo" has no attribute "nme"; maybe "name"?
290291
f.nme = 42 # type: ignore[assignment]
292+
293+
Check that awaitable return value is used [unused-awaitable]
294+
------------------------------------------------------------
295+
296+
If you use :option:`--enable-error-code unused-awaitable <mypy --enable-error-code>`,
297+
mypy generates an error if you don't use a returned value that defines ``__await__``.
298+
299+
Example:
300+
301+
.. code-block:: python
302+
303+
# Use "mypy --enable-error-code unused-awaitable ..."
304+
305+
import asyncio
306+
307+
async def f() -> int: ...
308+
309+
async def g() -> None:
310+
# Error: Value of type "Task[int]" must be used
311+
# Are you missing an await?
312+
asyncio.create_task(f())
313+
314+
You can assign the value to a temporary, otherwise unused to variable to
315+
silence the error:
316+
317+
.. code-block:: python
318+
319+
async def g() -> None:
320+
_ = asyncio.create_task(f()) # No error

docs/source/error_codes.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Displaying error codes
2424
----------------------
2525

2626
Error codes are not displayed by default. Use :option:`--show-error-codes <mypy --show-error-codes>`
27-
or config `show_error_codes = True` to display error codes. Error codes are shown inside square brackets:
27+
or config ``show_error_codes = True`` to display error codes. Error codes are shown inside square brackets:
2828

2929
.. code-block:: text
3030
@@ -46,11 +46,8 @@ line. This can be used even if you have not configured mypy to show
4646
error codes. Currently it's only possible to disable arbitrary error
4747
codes on individual lines using this comment.
4848

49-
.. note::
50-
51-
There are command-line flags and config file settings for enabling
52-
certain optional error codes, such as :option:`--disallow-untyped-defs <mypy --disallow-untyped-defs>`,
53-
which enables the ``no-untyped-def`` error code.
49+
You can also use :option:`--disable-error-code <mypy --disable-error-code>`
50+
to disable specific error codes globally.
5451

5552
This example shows how to ignore an error about an imported name mypy
5653
thinks is undefined:
@@ -60,3 +57,15 @@ thinks is undefined:
6057
# 'foo' is defined in 'foolib', even though mypy can't see the
6158
# definition.
6259
from foolib import foo # type: ignore[attr-defined]
60+
61+
62+
Enabling specific error codes
63+
-----------------------------
64+
65+
There are command-line flags and config file settings for enabling
66+
certain optional error codes, such as :option:`--disallow-untyped-defs <mypy --disallow-untyped-defs>`,
67+
which enables the ``no-untyped-def`` error code.
68+
69+
You can use :option:`--enable-error-code <mypy --enable-error-code>` to
70+
enable specific error codes that don't have a dedicated command-line
71+
flag or config file setting.

docs/source/installed_packages.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ you can create such packages.
2525
:pep:`561` specifies how a package can declare that it supports
2626
type checking.
2727

28+
.. note::
29+
30+
New versions of stub packages often use type system features not
31+
supported by older, and even fairly recent mypy versions. If you
32+
pin to an older version of mypy (using ``requirements.txt``, for
33+
example), it is recommended that you also pin the versions of all
34+
your stub package dependencies.
35+
2836
Using installed packages with mypy (PEP 561)
2937
********************************************
3038

0 commit comments

Comments
 (0)