@@ -323,6 +323,38 @@ Example:
323
323
else :
324
324
raise ValueError (' not defined for zero' )
325
325
326
+ .. _code-empty-body :
327
+
328
+ Check that functions don't have empty bodies outside stubs [empty-body]
329
+ -----------------------------------------------------------------------
330
+
331
+ This error code is similar to the ``[return] `` code but is emitted specifically
332
+ for functions and methods with empty bodies (if they are annotated with
333
+ non-trivial return type). Such a distinction exists because in some contexts
334
+ an empty body can be valid, for example for an abstract method or in a stub
335
+ file. Also old versions of mypy used to unconditionally allow functions with
336
+ empty bodies, so having a dedicated error code simplifies cross-version
337
+ compatibility.
338
+
339
+ Note that empty bodies are allowed for methods in *protocols *, and such methods
340
+ are considered implicitly abstract:
341
+
342
+ .. code-block :: python
343
+
344
+ from abc import abstractmethod
345
+ from typing import Protocol
346
+
347
+ class RegularABC :
348
+ @abstractmethod
349
+ def foo (self ) -> int :
350
+ pass # OK
351
+ def bar (self ) -> int :
352
+ pass # Error: Missing return statement [empty-body]
353
+
354
+ class Proto (Protocol ):
355
+ def bar (self ) -> int :
356
+ pass # OK
357
+
326
358
.. _code-return-value :
327
359
328
360
Check that return value is compatible [return-value]
@@ -947,6 +979,28 @@ otherwise unused variable:
947
979
948
980
_ = f() # No error
949
981
982
+ .. _code-top-level-await :
983
+
984
+ Warn about top level await expressions [top-level-await]
985
+ --------------------------------------------------------
986
+
987
+ This error code is separate from the general ``[syntax] `` errors, because in
988
+ some environments (e.g. IPython) a top level ``await `` is allowed. In such
989
+ environments a user may want to use ``--disable-error-code=top-level-await ``,
990
+ that allows to still have errors for other improper uses of ``await ``, for
991
+ example:
992
+
993
+ .. code-block :: python
994
+
995
+ async def f () -> None :
996
+ ...
997
+
998
+ top = await f() # Error: "await" outside function [top-level-await]
999
+
1000
+ def g () -> None :
1001
+ # This is a blocker error and cannot be silenced.
1002
+ await f() # Error: "await" outside coroutine ("async def")
1003
+
950
1004
.. _code-assert-type :
951
1005
952
1006
Check types in assert_type [assert-type]
@@ -978,6 +1032,27 @@ Functions will always evaluate to true in boolean contexts.
978
1032
if f: # Error: Function "Callable[[], Any]" could always be true in boolean context [truthy-function]
979
1033
pass
980
1034
1035
+ .. _code-str-format :
1036
+
1037
+ Check that string formatting/interpolation is type-safe [str-format]
1038
+ --------------------------------------------------------------------
1039
+
1040
+ Mypy will check that f-strings, ``str.format() `` calls, and ``% `` interpolations
1041
+ are valid (when corresponding template is a literal string). This includes
1042
+ checking number and types of replacements, for example:
1043
+
1044
+ .. code-block :: python
1045
+
1046
+ # Error: Cannot find replacement for positional format specifier 1 [str-format]
1047
+ " {} and {}" .format(" spam" )
1048
+ " {} and {}" .format(" spam" , " eggs" ) # OK
1049
+ # Error: Not all arguments converted during string formatting [str-format]
1050
+ " {} and {}" .format(" spam" , " eggs" , " cheese" )
1051
+
1052
+ # Error: Incompatible types in string interpolation
1053
+ # (expression has type "float", placeholder has type "int") [str-format]
1054
+ " {:d}" .format(3.14 )
1055
+
981
1056
.. _code-str-bytes-safe :
982
1057
983
1058
Check for implicit bytes coercions [str-bytes-safe]
@@ -998,6 +1073,28 @@ Warn about cases where a bytes object may be converted to a string in an unexpec
998
1073
print (f " The alphabet starts with { b!r } " ) # The alphabet starts with b'abc'
999
1074
print (f " The alphabet starts with { b.decode(' utf-8' )} " ) # The alphabet starts with abc
1000
1075
1076
+ .. _code-annotation-unchecked :
1077
+
1078
+ Notify about an annotation in an unchecked function [annotation-unchecked]
1079
+ --------------------------------------------------------------------------
1080
+
1081
+ Sometimes a user may accidentally omit an annotation for a function, and mypy
1082
+ will not check the body of this function (unless one uses
1083
+ :option: `--check-untyped-defs <mypy --check-untyped-defs> ` or
1084
+ :option: `--disallow-untyped-defs <mypy --disallow-untyped-defs> `). To avoid
1085
+ such situations go unnoticed, mypy will show a note, if there are any type
1086
+ annotations in an unchecked function:
1087
+
1088
+ .. code-block :: python
1089
+
1090
+ def test_assignment (): # "-> None" return annotation is missing
1091
+ # Note: By default the bodies of untyped functions are not checked,
1092
+ # consider using --check-untyped-defs [annotation-unchecked]
1093
+ x: int = " no way"
1094
+
1095
+ Note that mypy will still exit with return code ``0 ``, since such behaviour is
1096
+ specified by :pep: `484 `.
1097
+
1001
1098
.. _code-syntax :
1002
1099
1003
1100
Report syntax errors [syntax]
0 commit comments