@@ -344,7 +344,7 @@ Check that assignment target is not a method [method-assign]
344344
345345In general, assigning to a method on class object or instance (a.k.a.
346346monkey-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.
348348Consider 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
371371Check 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
464464key and the value are valid.
465465
466466Example:
@@ -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
533536Check that type of target is known [has-type]
534537---------------------------------------------
@@ -810,8 +813,8 @@ Check that literal is used where expected [literal-required]
810813There are some places where only a (string) literal value is expected for
811814the purposes of static type checking, for example a ``TypedDict `` key, or
812815a ``__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
0 commit comments