.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "MYPY" "1" "Dec 21, 2023" "" "Mypy" .SH NAME mypy \- Optional static typing for Python .SH SYNOPSIS .sp \fBmypy\fP [\-h] [\-v] [\-V] [\-m \fIMODULE\fP] [\-p \fIPACKAGE\fP] [\-c \fIPROGRAM_TEXT\fP] [\fIOPTIONS\fP\&...] [\fIFILES\fP ...] .SH DESCRIPTION .sp Mypy is a static type checker for Python 3 and Python 2.7. If you sprinkle your code with type annotations, mypy can type check your code and find common bugs. As mypy is a static analyzer, or a lint\-like tool, the type annotations are just hints for mypy and don’t interfere when running your program. You run your program with a standard Python interpreter, and the annotations are treated effectively as comments. .sp Using the Python 3 function annotation syntax (using the PEP 484 notation) or a comment\-based annotation syntax for Python 2 code, you will be able to efficiently annotate your code and use mypy to check the code for common errors. Mypy has a powerful and easy\-to\-use type system with modern features such as type inference, generics, callable types, tuple types, union types, and structural subtyping. .sp Mypy is invoked with the paths the user needs to check: .INDENT 0.0 .INDENT 3.5 .sp .EX $ mypy foo.py bar.py some_directory .EE .UNINDENT .UNINDENT .sp The directories are checked recursively to find Python source files. .SH OPTIONS .sp This section documents mypy\(aqs command line interface. You can view a quick summary of the available flags by running \fI\%mypy \-\-help\fP\&. .sp \fBNOTE:\fP .INDENT 0.0 .INDENT 3.5 Command line flags are liable to change between releases. .UNINDENT .UNINDENT .SS Specifying what to type check .sp By default, you can specify what code you want mypy to type check by passing in the paths to what you want to have type checked: .INDENT 0.0 .INDENT 3.5 .sp .EX $ mypy foo.py bar.py some_directory .EE .UNINDENT .UNINDENT .sp Note that directories are checked recursively. .sp Mypy also lets you specify what code to type check in several other ways. A short summary of the relevant flags is included below: for full details, see running\-mypy\&. .INDENT 0.0 .TP .B \-m MODULE, \-\-module MODULE Asks mypy to type check the provided module. This flag may be repeated multiple times. .sp Mypy \fIwill not\fP recursively type check any submodules of the provided module. .UNINDENT .INDENT 0.0 .TP .B \-p PACKAGE, \-\-package PACKAGE Asks mypy to type check the provided package. This flag may be repeated multiple times. .sp Mypy \fIwill\fP recursively type check any submodules of the provided package. This flag is identical to \fI\%\-\-module\fP apart from this behavior. .UNINDENT .INDENT 0.0 .TP .B \-c PROGRAM_TEXT, \-\-command PROGRAM_TEXT Asks mypy to type check the provided string as a program. .UNINDENT .INDENT 0.0 .TP .B \-\-exclude A regular expression that matches file names, directory names and paths which mypy should ignore while recursively discovering files to check. Use forward slashes on all platforms. .sp For instance, to avoid discovering any files named \fIsetup.py\fP you could pass \fB\-\-exclude \(aq/setup\e.py$\(aq\fP\&. Similarly, you can ignore discovering directories with a given name by e.g. \fB\-\-exclude /build/\fP or those matching a subpath with \fB\-\-exclude /project/vendor/\fP\&. To ignore multiple files / directories / paths, you can provide the \-\-exclude flag more than once, e.g \fB\-\-exclude \(aq/setup\e.py$\(aq \-\-exclude \(aq/build/\(aq\fP\&. .sp Note that this flag only affects recursive directory tree discovery, that is, when mypy is discovering files within a directory tree or submodules of a package to check. If you pass a file or module explicitly it will still be checked. For instance, \fBmypy \-\-exclude \(aq/setup.py$\(aq but_still_check/setup.py\fP\&. .sp In particular, \fB\-\-exclude\fP does not affect mypy\(aqs import following\&. You can use a per\-module .nf :confval:\(gafollow_imports\(ga .fi config option to additionally avoid mypy from following imports and checking code you do not wish to be checked. .sp Note that mypy will never recursively discover files and directories named \(dqsite\-packages\(dq, \(dqnode_modules\(dq or \(dq__pycache__\(dq, or those whose name starts with a period, exactly as \fB\-\-exclude \(aq/(site\-packages|node_modules|__pycache__|\e..*)/$\(aq\fP would. Mypy will also never recursively discover files with extensions other than \fB\&.py\fP or \fB\&.pyi\fP\&. .UNINDENT .SS Optional arguments .INDENT 0.0 .TP .B \-h, \-\-help Show help message and exit. .UNINDENT .INDENT 0.0 .TP .B \-v, \-\-verbose More verbose messages. .UNINDENT .INDENT 0.0 .TP .B \-V, \-\-version Show program\(aqs version number and exit. .UNINDENT .SS Config file .INDENT 0.0 .TP .B \-\-config\-file CONFIG_FILE This flag makes mypy read configuration settings from the given file. .sp By default settings are read from \fBmypy.ini\fP, \fB\&.mypy.ini\fP, \fBpyproject.toml\fP, or \fBsetup.cfg\fP in the current directory. Settings override mypy\(aqs built\-in defaults and command line flags can override settings. .sp Specifying \fI\%\-\-config\-file=\fP (with no filename) will ignore \fIall\fP config files. .sp See config\-file for the syntax of configuration files. .UNINDENT .INDENT 0.0 .TP .B \-\-warn\-unused\-configs This flag makes mypy warn about unused \fB[mypy\-]\fP config file sections. (This requires turning off incremental mode using \fI\%\-\-no\-incremental\fP\&.) .UNINDENT .SS Import discovery .sp The following flags customize how exactly mypy discovers and follows imports. .INDENT 0.0 .TP .B \-\-explicit\-package\-bases This flag tells mypy that top\-level packages will be based in either the current directory, or a member of the \fBMYPYPATH\fP environment variable or .nf :confval:\(gamypy_path\(ga .fi config option. This option is only useful in the absence of \fI__init__.py\fP\&. See Mapping file paths to modules for details. .UNINDENT .INDENT 0.0 .TP .B \-\-ignore\-missing\-imports This flag makes mypy ignore all missing imports. It is equivalent to adding \fB# type: ignore\fP comments to all unresolved imports within your codebase. .sp Note that this flag does \fInot\fP suppress errors about missing names in successfully resolved modules. For example, if one has the following files: .INDENT 7.0 .INDENT 3.5 .sp .EX package/__init__.py package/mod.py .EE .UNINDENT .UNINDENT .sp Then mypy will generate the following errors with \fI\%\-\-ignore\-missing\-imports\fP: .INDENT 7.0 .INDENT 3.5 .sp .EX import package.unknown # No error, ignored x = package.unknown.func() # OK. \(aqfunc\(aq is assumed to be of type \(aqAny\(aq from package import unknown # No error, ignored from package.mod import NonExisting # Error: Module has no attribute \(aqNonExisting\(aq .EE .UNINDENT .UNINDENT .sp For more details, see ignore\-missing\-imports\&. .UNINDENT .INDENT 0.0 .TP .B \-\-follow\-imports {normal,silent,skip,error} This flag adjusts how mypy follows imported modules that were not explicitly passed in via the command line. .sp The default option is \fBnormal\fP: mypy will follow and type check all modules. For more information on what the other options do, see Following imports\&. .UNINDENT .INDENT 0.0 .TP .B \-\-python\-executable EXECUTABLE This flag will have mypy collect type information from \fI\%PEP 561\fP compliant packages installed for the Python executable \fBEXECUTABLE\fP\&. If not provided, mypy will use PEP 561 compliant packages installed for the Python executable running mypy. .sp See installed\-packages for more on making PEP 561 compliant packages. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-site\-packages This flag will disable searching for \fI\%PEP 561\fP compliant packages. This will also disable searching for a usable Python executable. .sp Use this flag if mypy cannot find a Python executable for the version of Python being checked, and you don\(aqt need to use PEP 561 typed packages. Otherwise, use \fI\%\-\-python\-executable\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-silence\-site\-packages By default, mypy will suppress any error messages generated within \fI\%PEP 561\fP compliant packages. Adding this flag will disable this behavior. .UNINDENT .INDENT 0.0 .TP .B \-\-fast\-module\-lookup The default logic used to scan through search paths to resolve imports has a quadratic worse\-case behavior in some cases, which is for instance triggered by a large number of folders sharing a top\-level namespace as in: .INDENT 7.0 .INDENT 3.5 .sp .EX foo/ company/ foo/ a.py bar/ company/ bar/ b.py baz/ company/ baz/ c.py \&... .EE .UNINDENT .UNINDENT .sp If you are in this situation, you can enable an experimental fast path by setting the \fI\%\-\-fast\-module\-lookup\fP option. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-namespace\-packages This flag disables import discovery of namespace packages (see \fI\%PEP 420\fP). In particular, this prevents discovery of packages that don\(aqt have an \fB__init__.py\fP (or \fB__init__.pyi\fP) file. .sp This flag affects how mypy finds modules and packages explicitly passed on the command line. It also affects how mypy determines fully qualified module names for files passed on the command line. See Mapping file paths to modules for details. .UNINDENT .SS Platform configuration .sp By default, mypy will assume that you intend to run your code using the same operating system and Python version you are using to run mypy itself. The following flags let you modify this behavior. .sp For more information on how to use these flags, see version_and_platform_checks\&. .INDENT 0.0 .TP .B \-\-python\-version X.Y This flag will make mypy type check your code as if it were run under Python version X.Y. Without this option, mypy will default to using whatever version of Python is running mypy. .sp This flag will attempt to find a Python executable of the corresponding version to search for \fI\%PEP 561\fP compliant packages. If you\(aqd like to disable this, use the \fI\%\-\-no\-site\-packages\fP flag (see \fI\%Import discovery\fP for more details). .UNINDENT .INDENT 0.0 .TP .B \-\-platform PLATFORM This flag will make mypy type check your code as if it were run under the given operating system. Without this option, mypy will default to using whatever operating system you are currently using. .sp The \fBPLATFORM\fP parameter may be any string supported by \fBsys.platform\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-always\-true NAME This flag will treat all variables named \fBNAME\fP as compile\-time constants that are always true. This flag may be repeated. .UNINDENT .INDENT 0.0 .TP .B \-\-always\-false NAME This flag will treat all variables named \fBNAME\fP as compile\-time constants that are always false. This flag may be repeated. .UNINDENT .SS Disallow dynamic typing .sp The \fBAny\fP type is used to represent a value that has a dynamic type\&. The \fB\-\-disallow\-any\fP family of flags will disallow various uses of the \fBAny\fP type in a module \-\- this lets us strategically disallow the use of dynamic typing in a controlled way. .sp The following options are available: .INDENT 0.0 .TP .B \-\-disallow\-any\-unimported This flag disallows usage of types that come from unfollowed imports (such types become aliases for \fBAny\fP). Unfollowed imports occur either when the imported module does not exist or when \fI\%\-\-follow\-imports=skip\fP is set. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-any\-expr This flag disallows all expressions in the module that have type \fBAny\fP\&. If an expression of type \fBAny\fP appears anywhere in the module mypy will output an error unless the expression is immediately used as an argument to \fBcast()\fP or assigned to a variable with an explicit type annotation. .sp In addition, declaring a variable of type \fBAny\fP or casting to type \fBAny\fP is not allowed. Note that calling functions that take parameters of type \fBAny\fP is still allowed. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-any\-decorated This flag disallows functions that have \fBAny\fP in their signature after decorator transformation. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-any\-explicit This flag disallows explicit \fBAny\fP in type positions such as type annotations and generic type parameters. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-any\-generics This flag disallows usage of generic types that do not specify explicit type parameters. For example, you can\(aqt use a bare \fBx: list\fP\&. Instead, you must always write something like \fBx: list[int]\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-subclassing\-any This flag reports an error whenever a class subclasses a value of type \fBAny\fP\&. This may occur when the base class is imported from a module that doesn\(aqt exist (when using \fI\%\-\-ignore\-missing\-imports\fP) or is ignored due to \fI\%\-\-follow\-imports=skip\fP or a \fB# type: ignore\fP comment on the \fBimport\fP statement. .sp Since the module is silenced, the imported class is given a type of \fBAny\fP\&. By default mypy will assume that the subclass correctly inherited the base class even though that may not actually be the case. This flag makes mypy raise an error instead. .UNINDENT .SS Untyped definitions and calls .sp The following flags configure how mypy handles untyped function definitions or calls. .INDENT 0.0 .TP .B \-\-disallow\-untyped\-calls This flag reports an error whenever a function with type annotations calls a function defined without annotations. .UNINDENT .INDENT 0.0 .TP .B \-\-untyped\-calls\-exclude This flag allows to selectively disable \fI\%\-\-disallow\-untyped\-calls\fP for functions and methods defined in specific packages, modules, or classes. Note that each exclude entry acts as a prefix. For example (assuming there are no type annotations for \fBthird_party_lib\fP available): .INDENT 7.0 .INDENT 3.5 .sp .EX # mypy \-\-disallow\-untyped\-calls # \-\-untyped\-calls\-exclude=third_party_lib.module_a # \-\-untyped\-calls\-exclude=foo.A from third_party_lib.module_a import some_func from third_party_lib.module_b import other_func import foo some_func() # OK, function comes from module \(gathird_party_lib.module_a\(ga other_func() # E: Call to untyped function \(dqother_func\(dq in typed context foo.A().meth() # OK, method was defined in class \(gafoo.A\(ga foo.B().meth() # E: Call to untyped function \(dqmeth\(dq in typed context # file foo.py class A: def meth(self): pass class B: def meth(self): pass .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-untyped\-defs This flag reports an error whenever it encounters a function definition without type annotations or with incomplete type annotations. (a superset of \fI\%\-\-disallow\-incomplete\-defs\fP). .sp For example, it would report an error for \fBdef f(a, b)\fP and \fBdef f(a: int, b)\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-incomplete\-defs This flag reports an error whenever it encounters a partly annotated function definition, while still allowing entirely unannotated definitions. .sp For example, it would report an error for \fBdef f(a: int, b)\fP but not \fBdef f(a, b)\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-check\-untyped\-defs This flag is less severe than the previous two options \-\- it type checks the body of every function, regardless of whether it has type annotations. (By default the bodies of functions without annotations are not type checked.) .sp It will assume all arguments have type \fBAny\fP and always infer \fBAny\fP as the return type. .UNINDENT .INDENT 0.0 .TP .B \-\-disallow\-untyped\-decorators This flag reports an error whenever a function with type annotations is decorated with a decorator without annotations. .UNINDENT .SS None and Optional handling .sp The following flags adjust how mypy handles values of type \fBNone\fP\&. For more details, see no_strict_optional\&. .INDENT 0.0 .TP .B \-\-implicit\-optional This flag causes mypy to treat arguments with a \fBNone\fP default value as having an implicit \fBOptional\fP type. .sp For example, if this flag is set, mypy would assume that the \fBx\fP parameter is actually of type \fBOptional[int]\fP in the code snippet below since the default parameter is \fBNone\fP: .INDENT 7.0 .INDENT 3.5 .sp .EX def foo(x: int = None) \-> None: print(x) .EE .UNINDENT .UNINDENT .sp \fBNote:\fP This was disabled by default starting in mypy 0.980. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-strict\-optional This flag disables strict checking of \fBOptional\fP types and \fBNone\fP values. With this option, mypy doesn\(aqt generally check the use of \fBNone\fP values \-\- they are valid everywhere. See no_strict_optional for more about this feature. .sp \fBNote:\fP Strict optional checking was enabled by default starting in mypy 0.600, and in previous versions it had to be explicitly enabled using \fB\-\-strict\-optional\fP (which is still accepted). .UNINDENT .SS Configuring warnings .sp The following flags enable warnings for code that is sound but is potentially problematic or redundant in some way. .INDENT 0.0 .TP .B \-\-warn\-redundant\-casts This flag will make mypy report an error whenever your code uses an unnecessary cast that can safely be removed. .UNINDENT .INDENT 0.0 .TP .B \-\-warn\-unused\-ignores This flag will make mypy report an error whenever your code uses a \fB# type: ignore\fP comment on a line that is not actually generating an error message. .sp This flag, along with the \fI\%\-\-warn\-redundant\-casts\fP flag, are both particularly useful when you are upgrading mypy. Previously, you may have needed to add casts or \fB# type: ignore\fP annotations to work around bugs in mypy or missing stubs for 3rd party libraries. .sp These two flags let you discover cases where either workarounds are no longer necessary. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-warn\-no\-return By default, mypy will generate errors when a function is missing return statements in some execution paths. The only exceptions are when: .INDENT 7.0 .IP \(bu 2 The function has a \fBNone\fP or \fBAny\fP return type .IP \(bu 2 The function has an empty body and is marked as an abstract method, is in a protocol class, or is in a stub file .IP \(bu 2 .INDENT 2.0 .TP .B The execution path can never return; for example, if an exception is always raised .UNINDENT .UNINDENT .sp Passing in \fI\%\-\-no\-warn\-no\-return\fP will disable these error messages in all cases. .UNINDENT .INDENT 0.0 .TP .B \-\-warn\-return\-any This flag causes mypy to generate a warning when returning a value with type \fBAny\fP from a function declared with a non\-\fBAny\fP return type. .UNINDENT .INDENT 0.0 .TP .B \-\-warn\-unreachable This flag will make mypy report an error whenever it encounters code determined to be unreachable or redundant after performing type analysis. This can be a helpful way of detecting certain kinds of bugs in your code. .sp For example, enabling this flag will make mypy report that the \fBx > 7\fP check is redundant and that the \fBelse\fP block below is unreachable. .INDENT 7.0 .INDENT 3.5 .sp .EX def process(x: int) \-> None: # Error: Right operand of \(dqor\(dq is never evaluated if isinstance(x, int) or x > 7: # Error: Unsupported operand types for + (\(dqint\(dq and \(dqstr\(dq) print(x + \(dqbad\(dq) else: # Error: \(aqStatement is unreachable\(aq error print(x + \(dqbad\(dq) .EE .UNINDENT .UNINDENT .sp To help prevent mypy from generating spurious warnings, the \(dqStatement is unreachable\(dq warning will be silenced in exactly two cases: .INDENT 7.0 .IP 1. 3 When the unreachable statement is a \fBraise\fP statement, is an \fBassert False\fP statement, or calls a function that has the \fBNoReturn\fP return type hint. In other words, when the unreachable statement throws an error or terminates the program in some way. .IP 2. 3 When the unreachable statement was \fIintentionally\fP marked as unreachable using version_and_platform_checks\&. .UNINDENT .sp \fBNOTE:\fP .INDENT 7.0 .INDENT 3.5 Mypy currently cannot detect and report unreachable or redundant code inside any functions using type\-variable\-value\-restriction\&. .sp This limitation will be removed in future releases of mypy. .UNINDENT .UNINDENT .UNINDENT .SS Miscellaneous strictness flags .sp This section documents any other flags that do not neatly fall under any of the above sections. .INDENT 0.0 .TP .B \-\-allow\-untyped\-globals This flag causes mypy to suppress errors caused by not being able to fully infer the types of global and class variables. .UNINDENT .INDENT 0.0 .TP .B \-\-allow\-redefinition By default, mypy won\(aqt allow a variable to be redefined with an unrelated type. This flag enables redefinition of a variable with an arbitrary type \fIin some contexts\fP: only redefinitions within the same block and nesting depth as the original definition are allowed. Example where this can be useful: .INDENT 7.0 .INDENT 3.5 .sp .EX def process(items: list[str]) \-> None: # \(aqitems\(aq has type list[str] items = [item.split() for item in items] # \(aqitems\(aq now has type list[list[str]] .EE .UNINDENT .UNINDENT .sp The variable must be used before it can be redefined: .INDENT 7.0 .INDENT 3.5 .sp .EX def process(items: list[str]) \-> None: items = \(dqmypy\(dq # invalid redefinition to str because the variable hasn\(aqt been used yet print(items) items = \(dq100\(dq # valid, items now has type str items = int(items) # valid, items now has type int .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-local\-partial\-types In mypy, the most common cases for partial types are variables initialized using \fBNone\fP, but without explicit \fBOptional\fP annotations. By default, mypy won\(aqt check partial types spanning module top level or class top level. This flag changes the behavior to only allow partial types at local level, therefore it disallows inferring variable type for \fBNone\fP from two assignments in different scopes. For example: .INDENT 7.0 .INDENT 3.5 .sp .EX from typing import Optional a = None # Need type annotation here if using \-\-local\-partial\-types b: Optional[int] = None class Foo: bar = None # Need type annotation here if using \-\-local\-partial\-types baz: Optional[int] = None def __init__(self) \-> None: self.bar = 1 reveal_type(Foo().bar) # Union[int, None] without \-\-local\-partial\-types .EE .UNINDENT .UNINDENT .sp Note: this option is always implicitly enabled in mypy daemon and will become enabled by default for mypy in a future release. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-implicit\-reexport By default, imported values to a module are treated as exported and mypy allows other modules to import them. This flag changes the behavior to not re\-export unless the item is imported using from\-as or is included in \fB__all__\fP\&. Note this is always treated as enabled for stub files. For example: .INDENT 7.0 .INDENT 3.5 .sp .EX # This won\(aqt re\-export the value from foo import bar # Neither will this from foo import bar as bang # This will re\-export it as bar and allow other modules to import it from foo import bar as bar # This will also re\-export bar from foo import bar __all__ = [\(aqbar\(aq] .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-strict\-equality By default, mypy allows always\-false comparisons like \fB42 == \(aqno\(aq\fP\&. Use this flag to prohibit such comparisons of non\-overlapping types, and similar identity and container checks: .INDENT 7.0 .INDENT 3.5 .sp .EX from typing import Text items: list[int] if \(aqsome string\(aq in items: # Error: non\-overlapping container check! ... text: Text if text != b\(aqother bytes\(aq: # Error: non\-overlapping equality check! ... assert text is not None # OK, check against None is allowed as a special case. .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-extra\-checks This flag enables additional checks that are technically correct but may be impractical in real code. In particular, it prohibits partial overlap in \fBTypedDict\fP updates, and makes arguments prepended via \fBConcatenate\fP positional\-only. For example: .INDENT 7.0 .INDENT 3.5 .sp .EX from typing import TypedDict class Foo(TypedDict): a: int class Bar(TypedDict): a: int b: int def test(foo: Foo, bar: Bar) \-> None: # This is technically unsafe since foo can have a subtype of Foo at # runtime, where type of key \(dqb\(dq is incompatible with int, see below bar.update(foo) class Bad(Foo): b: str bad: Bad = {\(dqa\(dq: 0, \(dqb\(dq: \(dqno\(dq} test(bad, bar) .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-strict This flag mode enables all optional error checking flags. You can see the list of flags enabled by strict mode in the full \fI\%mypy \-\-help\fP output. .sp Note: the exact list of flags enabled by running \fI\%\-\-strict\fP may change over time. .UNINDENT .INDENT 0.0 .TP .B \-\-disable\-error\-code This flag allows disabling one or multiple error codes globally. See error\-codes for more information. .INDENT 7.0 .INDENT 3.5 .sp .EX # no flag x = \(aqa string\(aq x.trim() # error: \(dqstr\(dq has no attribute \(dqtrim\(dq [attr\-defined] # When using \-\-disable\-error\-code attr\-defined x = \(aqa string\(aq x.trim() .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-enable\-error\-code This flag allows enabling one or multiple error codes globally. See error\-codes for more information. .sp Note: This flag will override disabled error codes from the \fI\%\-\-disable\-error\-code\fP flag. .INDENT 7.0 .INDENT 3.5 .sp .EX # When using \-\-disable\-error\-code attr\-defined x = \(aqa string\(aq x.trim() # \-\-disable\-error\-code attr\-defined \-\-enable\-error\-code attr\-defined x = \(aqa string\(aq x.trim() # error: \(dqstr\(dq has no attribute \(dqtrim\(dq [attr\-defined] .EE .UNINDENT .UNINDENT .UNINDENT .SS Configuring error messages .sp The following flags let you adjust how much detail mypy displays in error messages. .INDENT 0.0 .TP .B \-\-show\-error\-context This flag will precede all errors with \(dqnote\(dq messages explaining the context of the error. For example, consider the following program: .INDENT 7.0 .INDENT 3.5 .sp .EX class Test: def foo(self, x: int) \-> int: return x + \(dqbar\(dq .EE .UNINDENT .UNINDENT .sp Mypy normally displays an error message that looks like this: .INDENT 7.0 .INDENT 3.5 .sp .EX main.py:3: error: Unsupported operand types for + (\(dqint\(dq and \(dqstr\(dq) .EE .UNINDENT .UNINDENT .sp If we enable this flag, the error message now looks like this: .INDENT 7.0 .INDENT 3.5 .sp .EX main.py: note: In member \(dqfoo\(dq of class \(dqTest\(dq: main.py:3: error: Unsupported operand types for + (\(dqint\(dq and \(dqstr\(dq) .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-show\-column\-numbers This flag will add column offsets to error messages. For example, the following indicates an error in line 12, column 9 (note that column offsets are 0\-based): .INDENT 7.0 .INDENT 3.5 .sp .EX main.py:12:9: error: Unsupported operand types for / (\(dqint\(dq and \(dqstr\(dq) .EE .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-show\-error\-end This flag will make mypy show not just that start position where an error was detected, but also the end position of the relevant expression. This way various tools can easily highlight the whole error span. The format is \fBfile:line:column:end_line:end_column\fP\&. This option implies \fB\-\-show\-column\-numbers\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-hide\-error\-codes This flag will hide the error code \fB[]\fP from error messages. By default, the error code is shown after each error message: .INDENT 7.0 .INDENT 3.5 .sp .EX prog.py:1: error: \(dqstr\(dq has no attribute \(dqtrim\(dq [attr\-defined] .EE .UNINDENT .UNINDENT .sp See error\-codes for more information. .UNINDENT .INDENT 0.0 .TP .B \-\-pretty Use visually nicer output in error messages: use soft word wrap, show source code snippets, and show error location markers. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-color\-output This flag will disable color output in error messages, enabled by default. .UNINDENT .INDENT 0.0 .TP .B \-\-no\-error\-summary This flag will disable error summary. By default mypy shows a summary line including total number of errors, number of files with errors, and number of files checked. .UNINDENT .INDENT 0.0 .TP .B \-\-show\-absolute\-path Show absolute paths to files. .UNINDENT .INDENT 0.0 .TP .B \-\-soft\-error\-limit N This flag will adjust the limit after which mypy will (sometimes) disable reporting most additional errors. The limit only applies if it seems likely that most of the remaining errors will not be useful or they may be overly noisy. If \fBN\fP is negative, there is no limit. The default limit is \-1. .UNINDENT .INDENT 0.0 .TP .B \-\-force\-uppercase\-builtins Always use \fBList\fP instead of \fBlist\fP in error messages, even on Python 3.9+. .UNINDENT .INDENT 0.0 .TP .B \-\-force\-union\-syntax Always use \fBUnion[]\fP and \fBOptional[]\fP for union types in error messages (instead of the \fB|\fP operator), even on Python 3.10+. .UNINDENT .SS Incremental mode .sp By default, mypy will store type information into a cache. Mypy will use this information to avoid unnecessary recomputation when it type checks your code again. This can help speed up the type checking process, especially when most parts of your program have not changed since the previous mypy run. .sp If you want to speed up how long it takes to recheck your code beyond what incremental mode can offer, try running mypy in daemon mode\&. .INDENT 0.0 .TP .B \-\-no\-incremental This flag disables incremental mode: mypy will no longer reference the cache when re\-run. .sp Note that mypy will still write out to the cache even when incremental mode is disabled: see the \fI\%\-\-cache\-dir\fP flag below for more details. .UNINDENT .INDENT 0.0 .TP .B \-\-cache\-dir DIR By default, mypy stores all cache data inside of a folder named \fB\&.mypy_cache\fP in the current directory. This flag lets you change this folder. This flag can also be useful for controlling cache use when using remote caching\&. .sp This setting will override the \fBMYPY_CACHE_DIR\fP environment variable if it is set. .sp Mypy will also always write to the cache even when incremental mode is disabled so it can \(dqwarm up\(dq the cache. To disable writing to the cache, use \fB\-\-cache\-dir=/dev/null\fP (UNIX) or \fB\-\-cache\-dir=nul\fP (Windows). .UNINDENT .INDENT 0.0 .TP .B \-\-sqlite\-cache Use an \fI\%SQLite\fP database to store the cache. .UNINDENT .INDENT 0.0 .TP .B \-\-cache\-fine\-grained Include fine\-grained dependency information in the cache for the mypy daemon. .UNINDENT .INDENT 0.0 .TP .B \-\-skip\-version\-check By default, mypy will ignore cache data generated by a different version of mypy. This flag disables that behavior. .UNINDENT .INDENT 0.0 .TP .B \-\-skip\-cache\-mtime\-checks Skip cache internal consistency checks based on mtime. .UNINDENT .SS Advanced options .sp The following flags are useful mostly for people who are interested in developing or debugging mypy internals. .INDENT 0.0 .TP .B \-\-pdb This flag will invoke the Python debugger when mypy encounters a fatal error. .UNINDENT .INDENT 0.0 .TP .B \-\-show\-traceback, \-\-tb If set, this flag will display a full traceback when mypy encounters a fatal error. .UNINDENT .INDENT 0.0 .TP .B \-\-raise\-exceptions Raise exception on fatal error. .UNINDENT .INDENT 0.0 .TP .B \-\-custom\-typing\-module MODULE This flag lets you use a custom module as a substitute for the \fBtyping\fP module. .UNINDENT .INDENT 0.0 .TP .B \-\-custom\-typeshed\-dir DIR This flag specifies the directory where mypy looks for standard library typeshed stubs, instead of the typeshed that ships with mypy. This is primarily intended to make it easier to test typeshed changes before submitting them upstream, but also allows you to use a forked version of typeshed. .sp Note that this doesn\(aqt affect third\-party library stubs. To test third\-party stubs, for example try \fBMYPYPATH=stubs/six mypy ...\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-warn\-incomplete\-stub This flag modifies both the \fI\%\-\-disallow\-untyped\-defs\fP and \fI\%\-\-disallow\-incomplete\-defs\fP flags so they also report errors if stubs in typeshed are missing type annotations or has incomplete annotations. If both flags are missing, \fI\%\-\-warn\-incomplete\-stub\fP also does nothing. .sp This flag is mainly intended to be used by people who want contribute to typeshed and would like a convenient way to find gaps and omissions. .sp If you want mypy to report an error when your codebase \fIuses\fP an untyped function, whether that function is defined in typeshed or not, use the \fI\%\-\-disallow\-untyped\-calls\fP flag. See \fI\%Untyped definitions and calls\fP for more details. .UNINDENT .INDENT 0.0 .TP .B \-\-shadow\-file SOURCE_FILE SHADOW_FILE When mypy is asked to type check \fBSOURCE_FILE\fP, this flag makes mypy read from and type check the contents of \fBSHADOW_FILE\fP instead. However, diagnostics will continue to refer to \fBSOURCE_FILE\fP\&. .sp Specifying this argument multiple times (\fB\-\-shadow\-file X1 Y1 \-\-shadow\-file X2 Y2\fP) will allow mypy to perform multiple substitutions. .sp This allows tooling to create temporary files with helpful modifications without having to change the source file in place. For example, suppose we have a pipeline that adds \fBreveal_type\fP for certain variables. This pipeline is run on \fBoriginal.py\fP to produce \fBtemp.py\fP\&. Running \fBmypy \-\-shadow\-file original.py temp.py original.py\fP will then cause mypy to type check the contents of \fBtemp.py\fP instead of \fBoriginal.py\fP, but error messages will still reference \fBoriginal.py\fP\&. .UNINDENT .SS Report generation .sp If these flags are set, mypy will generate a report in the specified format into the specified directory. .INDENT 0.0 .TP .B \-\-any\-exprs\-report DIR Causes mypy to generate a text file report documenting how many expressions of type \fBAny\fP are present within your codebase. .UNINDENT .INDENT 0.0 .TP .B \-\-cobertura\-xml\-report DIR Causes mypy to generate a Cobertura XML type checking coverage report. .sp To generate this report, you must either manually install the \fI\%lxml\fP library or specify mypy installation with the setuptools extra \fBmypy[reports]\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-html\-report / \-\-xslt\-html\-report DIR Causes mypy to generate an HTML type checking coverage report. .sp To generate this report, you must either manually install the \fI\%lxml\fP library or specify mypy installation with the setuptools extra \fBmypy[reports]\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-linecount\-report DIR Causes mypy to generate a text file report documenting the functions and lines that are typed and untyped within your codebase. .UNINDENT .INDENT 0.0 .TP .B \-\-linecoverage\-report DIR Causes mypy to generate a JSON file that maps each source file\(aqs absolute filename to a list of line numbers that belong to typed functions in that file. .UNINDENT .INDENT 0.0 .TP .B \-\-lineprecision\-report DIR Causes mypy to generate a flat text file report with per\-module statistics of how many lines are typechecked etc. .UNINDENT .INDENT 0.0 .TP .B \-\-txt\-report / \-\-xslt\-txt\-report DIR Causes mypy to generate a text file type checking coverage report. .sp To generate this report, you must either manually install the \fI\%lxml\fP library or specify mypy installation with the setuptools extra \fBmypy[reports]\fP\&. .UNINDENT .INDENT 0.0 .TP .B \-\-xml\-report DIR Causes mypy to generate an XML type checking coverage report. .sp To generate this report, you must either manually install the \fI\%lxml\fP library or specify mypy installation with the setuptools extra \fBmypy[reports]\fP\&. .UNINDENT .SS Enabling incomplete/experimental features .INDENT 0.0 .TP .B \-\-enable\-incomplete\-feature FEATURE Some features may require several mypy releases to implement, for example due to their complexity, potential for backwards incompatibility, or ambiguous semantics that would benefit from feedback from the community. You can enable such features for early preview using this flag. Note that it is not guaranteed that all features will be ultimately enabled by default. In \fIrare cases\fP we may decide to not go ahead with certain features. .UNINDENT .sp List of currently incomplete/experimental features: .INDENT 0.0 .IP \(bu 2 \fBPreciseTupleTypes\fP: this feature will infer more precise tuple types in various scenarios. Before variadic types were added to the Python type system by \fI\%PEP 646\fP, it was impossible to express a type like \(dqa tuple with at least two integers\(dq. The best type available was \fBtuple[int, ...]\fP\&. Therefore, mypy applied very lenient checking for variable\-length tuples. Now this type can be expressed as \fBtuple[int, int, *tuple[int, ...]]\fP\&. For such more precise types (when explicitly \fIdefined\fP by a user) mypy, for example, warns about unsafe index access, and generally handles them in a type\-safe manner. However, to avoid problems in existing code, mypy does not \fIinfer\fP these precise types when it technically can. Here are notable examples where \fBPreciseTupleTypes\fP infers more precise types: .INDENT 2.0 .INDENT 3.5 .sp .EX numbers: tuple[int, ...] more_numbers = (1, *numbers, 1) reveal_type(more_numbers) # Without PreciseTupleTypes: tuple[int, ...] # With PreciseTupleTypes: tuple[int, *tuple[int, ...], int] other_numbers = (1, 1) + numbers reveal_type(other_numbers) # Without PreciseTupleTypes: tuple[int, ...] # With PreciseTupleTypes: tuple[int, int, *tuple[int, ...]] if len(numbers) > 2: reveal_type(numbers) # Without PreciseTupleTypes: tuple[int, ...] # With PreciseTupleTypes: tuple[int, int, int, *tuple[int, ...]] else: reveal_type(numbers) # Without PreciseTupleTypes: tuple[int, ...] # With PreciseTupleTypes: tuple[()] | tuple[int] | tuple[int, int] .EE .UNINDENT .UNINDENT .UNINDENT .SS Miscellaneous .INDENT 0.0 .TP .B \-\-install\-types This flag causes mypy to install known missing stub packages for third\-party libraries using pip. It will display the pip command that will be run, and expects a confirmation before installing anything. For security reasons, these stubs are limited to only a small subset of manually selected packages that have been verified by the typeshed team. These packages include only stub files and no executable code. .sp If you use this option without providing any files or modules to type check, mypy will install stub packages suggested during the previous mypy run. If there are files or modules to type check, mypy first type checks those, and proposes to install missing stubs at the end of the run, but only if any missing modules were detected. .sp \fBNOTE:\fP .INDENT 7.0 .INDENT 3.5 This is new in mypy 0.900. Previous mypy versions included a selection of third\-party package stubs, instead of having them installed separately. .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B \-\-non\-interactive When used together with \fI\%\-\-install\-types\fP, this causes mypy to install all suggested stub packages using pip without asking for confirmation, and then continues to perform type checking using the installed stubs, if some files or modules are provided to type check. .sp This is implemented as up to two mypy runs internally. The first run is used to find missing stub packages, and output is shown from this run only if no missing stub packages were found. If missing stub packages were found, they are installed and then another run is performed. .UNINDENT .INDENT 0.0 .TP .B \-\-junit\-xml JUNIT_XML Causes mypy to generate a JUnit XML test result document with type checking results. This can make it easier to integrate mypy with continuous integration (CI) tools. .UNINDENT .INDENT 0.0 .TP .B \-\-find\-occurrences CLASS.MEMBER This flag will make mypy print out all usages of a class member based on static type information. This feature is experimental. .UNINDENT .INDENT 0.0 .TP .B \-\-scripts\-are\-modules This flag will give command line arguments that appear to be scripts (i.e. files whose name does not end in \fB\&.py\fP) a module name derived from the script name rather than the fixed name \fB__main__\fP\&. .sp This lets you check more than one script in a single mypy invocation. (The default \fB__main__\fP is technically more correct, but if you have many scripts that import a large package, the behavior enabled by this flag is often more convenient.) .UNINDENT .SH ENVIRONMENT .INDENT 0.0 .TP \fBMYPYPATH\fP Additional module search path entries. The format is the same as the shell\(aqs \fI$PATH\fP: one or more directory pathnames separated by colons. .UNINDENT .SH SEE ALSO .sp \fBdmypy\fP(1) .sp Full documentation is available online at: \fI\%http://mypy.readthedocs.io/en/latest/getting_started.html\fP or locally at: \fI\%/usr/share/doc/mypy/html\fP (requires \fImypy\-doc\fP package). .SH AUTHOR Jukka Lehtosalo and contributors .\" Generated by docutils manpage writer. .