@@ -344,7 +344,7 @@ Check that assignment target is not a method [method-assign]
344
344
345
345
In general, assigning to a method on class object or instance (a.k.a.
346
346
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.
348
348
Consider this example:
349
349
350
350
.. code-block :: python
@@ -355,18 +355,18 @@ Consider this example:
355
355
356
356
def h (self : A) -> None : pass
357
357
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
362
362
363
363
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.
366
366
367
367
.. note ::
368
368
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.
370
370
371
371
Check type variable values [type-var]
372
372
-------------------------------------
@@ -456,11 +456,11 @@ Example:
456
456
Check TypedDict items [typeddict-item]
457
457
--------------------------------------
458
458
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.
461
461
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
464
464
key and the value are valid.
465
465
466
466
Example:
@@ -480,10 +480,13 @@ Example:
480
480
Check TypedDict Keys [typeddict-unknown-key]
481
481
--------------------------------------------
482
482
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:
487
490
488
491
.. code-block :: python
489
492
@@ -502,23 +505,23 @@ However, it will generate an error when these are created. Example:
502
505
a: Point = {" x" : 1 , " y" : 4 }
503
506
b: Point3D = {" x" : 2 , " y" : 5 , " z" : 6 }
504
507
505
- # OK
506
- add_x_coordinates(a, b)
508
+ add_x_coordinates(a, b) # OK
509
+
507
510
# Error: Extra key "z" for TypedDict "Point" [typeddict-unknown-key]
508
511
add_x_coordinates(a, {" x" : 1 , " y" : 4 , " z" : 5 })
509
512
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 :
512
515
513
516
.. code-block :: python
514
517
515
518
a: Point = {" x" : 1 , " y" : 2 }
516
519
# Error: Extra key "z" for TypedDict "Point" [typeddict-unknown-key]
517
520
a[" z" ] = 3
518
521
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 :
522
525
523
526
.. code-block :: python
524
527
@@ -528,7 +531,7 @@ Whereas reading an unknown value will generate the more generic/serious
528
531
529
532
.. note ::
530
533
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.
532
535
533
536
Check that type of target is known [has-type]
534
537
---------------------------------------------
@@ -810,8 +813,8 @@ Check that literal is used where expected [literal-required]
810
813
There are some places where only a (string) literal value is expected for
811
814
the purposes of static type checking, for example a ``TypedDict `` key, or
812
815
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 :
815
818
816
819
.. code-block :: python
817
820
0 commit comments