Skip to content

Commit 2523095

Browse files
authored
Some minor documentation tweaks (#14847)
I reviewed documentation changes to be included in mypy 1.1.
1 parent b6cb0ed commit 2523095

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

docs/source/error_code_list.rst

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Check that assignment target is not a method [method-assign]
344344

345345
In general, assigning to a method on class object or instance (a.k.a.
346346
monkey-patching) is ambiguous in terms of types, since Python's static type
347-
system cannot express difference between bound and unbound callable types.
347+
system cannot express the difference between bound and unbound callable types.
348348
Consider this example:
349349

350350
.. code-block:: python
@@ -355,18 +355,18 @@ Consider this example:
355355
356356
def h(self: A) -> None: pass
357357
358-
A.f = h # type of h is Callable[[A], None]
359-
A().f() # this works
360-
A.f = A().g # type of A().g is Callable[[], None]
361-
A().f() # but this also works at runtime
358+
A.f = h # Type of h is Callable[[A], None]
359+
A().f() # This works
360+
A.f = A().g # Type of A().g is Callable[[], None]
361+
A().f() # ...but this also works at runtime
362362
363363
To prevent the ambiguity, mypy will flag both assignments by default. If this
364-
error code is disabled, mypy will treat all method assignments r.h.s. as unbound,
365-
so the second assignment will still generate an error.
364+
error code is disabled, mypy will treat the assigned value in all method assignments as unbound,
365+
so only the second assignment will still generate an error.
366366

367367
.. note::
368368

369-
This error code is a sub-error code of a wider ``[assignment]`` code.
369+
This error code is a subcode of the more general ``[assignment]`` code.
370370

371371
Check type variable values [type-var]
372372
-------------------------------------
@@ -456,11 +456,11 @@ Example:
456456
Check TypedDict items [typeddict-item]
457457
--------------------------------------
458458

459-
When constructing a ``TypedDict`` object, mypy checks that each key and value is compatible
460-
with the ``TypedDict`` type that is inferred from the surrounding context.
459+
When constructing a TypedDict object, mypy checks that each key and value is compatible
460+
with the TypedDict type that is inferred from the surrounding context.
461461

462-
When getting a ``TypedDict`` item, mypy checks that the key
463-
exists. When assigning to a ``TypedDict``, mypy checks that both the
462+
When getting a TypedDict item, mypy checks that the key
463+
exists. When assigning to a TypedDict, mypy checks that both the
464464
key and the value are valid.
465465

466466
Example:
@@ -480,10 +480,13 @@ Example:
480480
Check TypedDict Keys [typeddict-unknown-key]
481481
--------------------------------------------
482482

483-
When constructing a ``TypedDict`` object, mypy checks whether the definition
484-
contains unknown keys. For convenience's sake, mypy will not generate an error
485-
when a ``TypedDict`` has extra keys if it's passed to a function as an argument.
486-
However, it will generate an error when these are created. Example:
483+
When constructing a TypedDict object, mypy checks whether the
484+
definition contains unknown keys, to catch invalid keys and
485+
misspellings. On the other hand, mypy will not generate an error when
486+
a previously constructed TypedDict value with extra keys is passed
487+
to a function as an argument, since TypedDict values support
488+
structural subtyping ("static duck typing") and the keys are assumed
489+
to have been validated at the point of construction. Example:
487490

488491
.. code-block:: python
489492
@@ -502,23 +505,23 @@ However, it will generate an error when these are created. Example:
502505
a: Point = {"x": 1, "y": 4}
503506
b: Point3D = {"x": 2, "y": 5, "z": 6}
504507
505-
# OK
506-
add_x_coordinates(a, b)
508+
add_x_coordinates(a, b) # OK
509+
507510
# Error: Extra key "z" for TypedDict "Point" [typeddict-unknown-key]
508511
add_x_coordinates(a, {"x": 1, "y": 4, "z": 5})
509512
510-
511-
Setting an unknown value on a ``TypedDict`` will also generate this error:
513+
Setting a TypedDict item using an unknown key will also generate this
514+
error, since it could be a misspelling:
512515

513516
.. code-block:: python
514517
515518
a: Point = {"x": 1, "y": 2}
516519
# Error: Extra key "z" for TypedDict "Point" [typeddict-unknown-key]
517520
a["z"] = 3
518521
519-
520-
Whereas reading an unknown value will generate the more generic/serious
521-
``typeddict-item``:
522+
Reading an unknown key will generate the more general (and serious)
523+
``typeddict-item`` error, which is likely to result in an exception at
524+
runtime:
522525

523526
.. code-block:: python
524527
@@ -528,7 +531,7 @@ Whereas reading an unknown value will generate the more generic/serious
528531
529532
.. note::
530533

531-
This error code is a sub-error code of a wider ``[typeddict-item]`` code.
534+
This error code is a subcode of the wider ``[typeddict-item]`` code.
532535

533536
Check that type of target is known [has-type]
534537
---------------------------------------------
@@ -810,8 +813,8 @@ Check that literal is used where expected [literal-required]
810813
There are some places where only a (string) literal value is expected for
811814
the purposes of static type checking, for example a ``TypedDict`` key, or
812815
a ``__match_args__`` item. Providing a ``str``-valued variable in such contexts
813-
will result in an error. Note however, in many cases you can use ``Final``,
814-
or ``Literal`` variables, for example:
816+
will result in an error. Note that in many cases you can also use ``Final``
817+
or ``Literal`` variables. Example:
815818

816819
.. code-block:: python
817820

docs/source/error_codes.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ So one can e.g. enable some code globally, disable it for all tests in
114114
the corresponding config section, and then re-enable it with an inline
115115
comment in some specific test.
116116

117-
Sub-error codes of other error codes
118-
------------------------------------
117+
Subcodes of error codes
118+
-----------------------
119119

120-
In rare cases (mostly for backwards compatibility reasons), some error
121-
code may be covered by another, wider error code. For example, an error with
120+
In some cases, mostly for backwards compatibility reasons, an error
121+
code may be covered also by another, wider error code. For example, an error with
122122
code ``[method-assign]`` can be ignored by ``# type: ignore[assignment]``.
123123
Similar logic works for disabling error codes globally. If a given error code
124-
is a sub code of another one, it must mentioned in the docs for the narrower
125-
code. This hierarchy is not nested, there cannot be sub-error codes of other
126-
sub-error codes.
124+
is a subcode of another one, it will be mentioned in the documentation for the narrower
125+
code. This hierarchy is not nested: there cannot be subcodes of other
126+
subcodes.

0 commit comments

Comments
 (0)