| 1 |
.. _command-line: |
| 2 |
|
| 3 |
.. program:: mypy |
| 4 |
|
| 5 |
The mypy command line |
| 6 |
===================== |
| 7 |
|
| 8 |
This section documents mypy's command line interface. You can view |
| 9 |
a quick summary of the available flags by running :option:`mypy --help`. |
| 10 |
|
| 11 |
.. note:: |
| 12 |
|
| 13 |
Command line flags are liable to change between releases. |
| 14 |
|
| 15 |
|
| 16 |
Specifying what to type check |
| 17 |
***************************** |
| 18 |
|
| 19 |
By default, you can specify what code you want mypy to type check |
| 20 |
by passing in the paths to what you want to have type checked:: |
| 21 |
|
| 22 |
$ mypy foo.py bar.py some_directory |
| 23 |
|
| 24 |
Note that directories are checked recursively. |
| 25 |
|
| 26 |
Mypy also lets you specify what code to type check in several other |
| 27 |
ways. A short summary of the relevant flags is included below: |
| 28 |
for full details, see :ref:`running-mypy`. |
| 29 |
|
| 30 |
.. option:: -m MODULE, --module MODULE |
| 31 |
|
| 32 |
Asks mypy to type check the provided module. This flag may be |
| 33 |
repeated multiple times. |
| 34 |
|
| 35 |
Mypy *will not* recursively type check any submodules of the provided |
| 36 |
module. |
| 37 |
|
| 38 |
.. option:: -p PACKAGE, --package PACKAGE |
| 39 |
|
| 40 |
Asks mypy to type check the provided package. This flag may be |
| 41 |
repeated multiple times. |
| 42 |
|
| 43 |
Mypy *will* recursively type check any submodules of the provided |
| 44 |
package. This flag is identical to :option:`--module` apart from this |
| 45 |
behavior. |
| 46 |
|
| 47 |
.. option:: -c PROGRAM_TEXT, --command PROGRAM_TEXT |
| 48 |
|
| 49 |
Asks mypy to type check the provided string as a program. |
| 50 |
|
| 51 |
|
| 52 |
.. option:: --exclude |
| 53 |
|
| 54 |
A regular expression that matches file names, directory names and paths |
| 55 |
which mypy should ignore while recursively discovering files to check. |
| 56 |
Use forward slashes on all platforms. |
| 57 |
|
| 58 |
For instance, to avoid discovering any files named `setup.py` you could |
| 59 |
pass ``--exclude '/setup\.py$'``. Similarly, you can ignore discovering |
| 60 |
directories with a given name by e.g. ``--exclude /build/`` or |
| 61 |
those matching a subpath with ``--exclude /project/vendor/``. To ignore |
| 62 |
multiple files / directories / paths, you can provide the --exclude |
| 63 |
flag more than once, e.g ``--exclude '/setup\.py$' --exclude '/build/'``. |
| 64 |
|
| 65 |
Note that this flag only affects recursive directory tree discovery, that |
| 66 |
is, when mypy is discovering files within a directory tree or submodules of |
| 67 |
a package to check. If you pass a file or module explicitly it will still be |
| 68 |
checked. For instance, ``mypy --exclude '/setup.py$' |
| 69 |
but_still_check/setup.py``. |
| 70 |
|
| 71 |
In particular, ``--exclude`` does not affect mypy's discovery of files |
| 72 |
via :ref:`import following <follow-imports>`. You can use a per-module |
| 73 |
:confval:`ignore_errors` config option to silence errors from a given module, |
| 74 |
or a per-module :confval:`follow_imports` config option to additionally avoid |
| 75 |
mypy from following imports and checking code you do not wish to be checked. |
| 76 |
|
| 77 |
Note that mypy will never recursively discover files and directories named |
| 78 |
"site-packages", "node_modules" or "__pycache__", or those whose name starts |
| 79 |
with a period, exactly as ``--exclude |
| 80 |
'/(site-packages|node_modules|__pycache__|\..*)/$'`` would. Mypy will also |
| 81 |
never recursively discover files with extensions other than ``.py`` or |
| 82 |
``.pyi``. |
| 83 |
|
| 84 |
.. option:: --exclude-gitignore |
| 85 |
|
| 86 |
This flag will add everything that matches ``.gitignore`` file(s) to :option:`--exclude`. |
| 87 |
|
| 88 |
|
| 89 |
Optional arguments |
| 90 |
****************** |
| 91 |
|
| 92 |
.. option:: -h, --help |
| 93 |
|
| 94 |
Show help message and exit. |
| 95 |
|
| 96 |
.. option:: -v, --verbose |
| 97 |
|
| 98 |
More verbose messages. |
| 99 |
|
| 100 |
.. option:: -V, --version |
| 101 |
|
| 102 |
Show program's version number and exit. |
| 103 |
|
| 104 |
.. option:: -O FORMAT, --output FORMAT {json} |
| 105 |
|
| 106 |
Set a custom output format. |
| 107 |
|
| 108 |
.. _config-file-flag: |
| 109 |
|
| 110 |
Config file |
| 111 |
*********** |
| 112 |
|
| 113 |
.. option:: --config-file CONFIG_FILE |
| 114 |
|
| 115 |
This flag makes mypy read configuration settings from the given file. |
| 116 |
|
| 117 |
By default settings are read from ``mypy.ini``, ``.mypy.ini``, ``pyproject.toml``, or ``setup.cfg`` |
| 118 |
in the current directory. Settings override mypy's built-in defaults and |
| 119 |
command line flags can override settings. |
| 120 |
|
| 121 |
Specifying :option:`--config-file= <--config-file>` (with no filename) will ignore *all* |
| 122 |
config files. |
| 123 |
|
| 124 |
See :ref:`config-file` for the syntax of configuration files. |
| 125 |
|
| 126 |
.. option:: --warn-unused-configs |
| 127 |
|
| 128 |
This flag makes mypy warn about unused ``[mypy-<pattern>]`` config |
| 129 |
file sections. |
| 130 |
(This requires turning off incremental mode using :option:`--no-incremental`.) |
| 131 |
|
| 132 |
|
| 133 |
.. _import-discovery: |
| 134 |
|
| 135 |
Import discovery |
| 136 |
**************** |
| 137 |
|
| 138 |
The following flags customize how exactly mypy discovers and follows |
| 139 |
imports. |
| 140 |
|
| 141 |
.. option:: --explicit-package-bases |
| 142 |
|
| 143 |
This flag tells mypy that top-level packages will be based in either the |
| 144 |
current directory, or a member of the ``MYPYPATH`` environment variable or |
| 145 |
:confval:`mypy_path` config option. This option is only useful |
| 146 |
in the absence of `__init__.py`. See :ref:`Mapping file |
| 147 |
paths to modules <mapping-paths-to-modules>` for details. |
| 148 |
|
| 149 |
.. option:: --ignore-missing-imports |
| 150 |
|
| 151 |
This flag makes mypy ignore all missing imports. It is equivalent |
| 152 |
to adding ``# type: ignore`` comments to all unresolved imports |
| 153 |
within your codebase. |
| 154 |
|
| 155 |
Note that this flag does *not* suppress errors about missing names |
| 156 |
in successfully resolved modules. For example, if one has the |
| 157 |
following files:: |
| 158 |
|
| 159 |
package/__init__.py |
| 160 |
package/mod.py |
| 161 |
|
| 162 |
Then mypy will generate the following errors with :option:`--ignore-missing-imports`: |
| 163 |
|
| 164 |
.. code-block:: python |
| 165 |
|
| 166 |
import package.unknown # No error, ignored |
| 167 |
x = package.unknown.func() # OK. 'func' is assumed to be of type 'Any' |
| 168 |
|
| 169 |
from package import unknown # No error, ignored |
| 170 |
from package.mod import NonExisting # Error: Module has no attribute 'NonExisting' |
| 171 |
|
| 172 |
For more details, see :ref:`ignore-missing-imports`. |
| 173 |
|
| 174 |
.. option:: --follow-untyped-imports |
| 175 |
|
| 176 |
This flag makes mypy analyze imports from installed packages even if |
| 177 |
missing a :ref:`py.typed marker or stubs <installed-packages>`. |
| 178 |
|
| 179 |
.. warning:: |
| 180 |
|
| 181 |
Note that analyzing all unannotated modules might result in issues |
| 182 |
when analyzing code not designed to be type checked and may significantly |
| 183 |
increase how long mypy takes to run. |
| 184 |
|
| 185 |
.. option:: --follow-imports {normal,silent,skip,error} |
| 186 |
|
| 187 |
This flag adjusts how mypy follows imported modules that were not |
| 188 |
explicitly passed in via the command line. |
| 189 |
|
| 190 |
The default option is ``normal``: mypy will follow and type check |
| 191 |
all modules. For more information on what the other options do, |
| 192 |
see :ref:`Following imports <follow-imports>`. |
| 193 |
|
| 194 |
.. option:: --python-executable EXECUTABLE |
| 195 |
|
| 196 |
This flag will have mypy collect type information from :pep:`561` |
| 197 |
compliant packages installed for the Python executable ``EXECUTABLE``. |
| 198 |
If not provided, mypy will use PEP 561 compliant packages installed for |
| 199 |
the Python executable running mypy. |
| 200 |
|
| 201 |
See :ref:`installed-packages` for more on making PEP 561 compliant packages. |
| 202 |
|
| 203 |
.. option:: --no-site-packages |
| 204 |
|
| 205 |
This flag will disable searching for :pep:`561` compliant packages. This |
| 206 |
will also disable searching for a usable Python executable. |
| 207 |
|
| 208 |
Use this flag if mypy cannot find a Python executable for the version of |
| 209 |
Python being checked, and you don't need to use PEP 561 typed packages. |
| 210 |
Otherwise, use :option:`--python-executable`. |
| 211 |
|
| 212 |
.. option:: --no-silence-site-packages |
| 213 |
|
| 214 |
By default, mypy will suppress any error messages generated within :pep:`561` |
| 215 |
compliant packages. Adding this flag will disable this behavior. |
| 216 |
|
| 217 |
.. option:: --fast-module-lookup |
| 218 |
|
| 219 |
The default logic used to scan through search paths to resolve imports has a |
| 220 |
quadratic worse-case behavior in some cases, which is for instance triggered |
| 221 |
by a large number of folders sharing a top-level namespace as in:: |
| 222 |
|
| 223 |
foo/ |
| 224 |
company/ |
| 225 |
foo/ |
| 226 |
a.py |
| 227 |
bar/ |
| 228 |
company/ |
| 229 |
bar/ |
| 230 |
b.py |
| 231 |
baz/ |
| 232 |
company/ |
| 233 |
baz/ |
| 234 |
c.py |
| 235 |
... |
| 236 |
|
| 237 |
If you are in this situation, you can enable an experimental fast path by |
| 238 |
setting the :option:`--fast-module-lookup` option. |
| 239 |
|
| 240 |
|
| 241 |
.. option:: --no-namespace-packages |
| 242 |
|
| 243 |
This flag disables import discovery of namespace packages (see :pep:`420`). |
| 244 |
In particular, this prevents discovery of packages that don't have an |
| 245 |
``__init__.py`` (or ``__init__.pyi``) file. |
| 246 |
|
| 247 |
This flag affects how mypy finds modules and packages explicitly passed on |
| 248 |
the command line. It also affects how mypy determines fully qualified module |
| 249 |
names for files passed on the command line. See :ref:`Mapping file paths to |
| 250 |
modules <mapping-paths-to-modules>` for details. |
| 251 |
|
| 252 |
|
| 253 |
.. _platform-configuration: |
| 254 |
|
| 255 |
Platform configuration |
| 256 |
********************** |
| 257 |
|
| 258 |
By default, mypy will assume that you intend to run your code using the same |
| 259 |
operating system and Python version you are using to run mypy itself. The |
| 260 |
following flags let you modify this behavior. |
| 261 |
|
| 262 |
For more information on how to use these flags, see :ref:`version_and_platform_checks`. |
| 263 |
|
| 264 |
.. option:: --python-version X.Y |
| 265 |
|
| 266 |
This flag will make mypy type check your code as if it were |
| 267 |
run under Python version X.Y. Without this option, mypy will default to using |
| 268 |
whatever version of Python is running mypy. |
| 269 |
|
| 270 |
This flag will attempt to find a Python executable of the corresponding |
| 271 |
version to search for :pep:`561` compliant packages. If you'd like to |
| 272 |
disable this, use the :option:`--no-site-packages` flag (see |
| 273 |
:ref:`import-discovery` for more details). |
| 274 |
|
| 275 |
.. option:: --platform PLATFORM |
| 276 |
|
| 277 |
This flag will make mypy type check your code as if it were |
| 278 |
run under the given operating system. Without this option, mypy will |
| 279 |
default to using whatever operating system you are currently using. |
| 280 |
|
| 281 |
The ``PLATFORM`` parameter may be any string supported by |
| 282 |
:py:data:`sys.platform`. |
| 283 |
|
| 284 |
.. _always-true: |
| 285 |
|
| 286 |
.. option:: --always-true NAME |
| 287 |
|
| 288 |
This flag will treat all variables named ``NAME`` as |
| 289 |
compile-time constants that are always true. This flag may |
| 290 |
be repeated. |
| 291 |
|
| 292 |
.. option:: --always-false NAME |
| 293 |
|
| 294 |
This flag will treat all variables named ``NAME`` as |
| 295 |
compile-time constants that are always false. This flag may |
| 296 |
be repeated. |
| 297 |
|
| 298 |
|
| 299 |
.. _disallow-dynamic-typing: |
| 300 |
|
| 301 |
Disallow dynamic typing |
| 302 |
*********************** |
| 303 |
|
| 304 |
The ``Any`` type is used to represent a value that has a :ref:`dynamic type <dynamic-typing>`. |
| 305 |
The ``--disallow-any`` family of flags will disallow various uses of the ``Any`` type in |
| 306 |
a module -- this lets us strategically disallow the use of dynamic typing in a controlled way. |
| 307 |
|
| 308 |
The following options are available: |
| 309 |
|
| 310 |
.. option:: --disallow-any-unimported |
| 311 |
|
| 312 |
This flag disallows usage of types that come from unfollowed imports |
| 313 |
(such types become aliases for ``Any``). Unfollowed imports occur either |
| 314 |
when the imported module does not exist or when :option:`--follow-imports=skip <--follow-imports>` |
| 315 |
is set. |
| 316 |
|
| 317 |
.. option:: --disallow-any-expr |
| 318 |
|
| 319 |
This flag disallows all expressions in the module that have type ``Any``. |
| 320 |
If an expression of type ``Any`` appears anywhere in the module |
| 321 |
mypy will output an error unless the expression is immediately |
| 322 |
used as an argument to :py:func:`~typing.cast` or assigned to a variable with an |
| 323 |
explicit type annotation. |
| 324 |
|
| 325 |
In addition, declaring a variable of type ``Any`` |
| 326 |
or casting to type ``Any`` is not allowed. Note that calling functions |
| 327 |
that take parameters of type ``Any`` is still allowed. |
| 328 |
|
| 329 |
.. option:: --disallow-any-decorated |
| 330 |
|
| 331 |
This flag disallows functions that have ``Any`` in their signature |
| 332 |
after decorator transformation. |
| 333 |
|
| 334 |
.. option:: --disallow-any-explicit |
| 335 |
|
| 336 |
This flag disallows explicit ``Any`` in type positions such as type |
| 337 |
annotations and generic type parameters. |
| 338 |
|
| 339 |
.. option:: --disallow-any-generics |
| 340 |
|
| 341 |
This flag disallows usage of generic types that do not specify explicit |
| 342 |
type parameters. For example, you can't use a bare ``x: list``. Instead, you |
| 343 |
must always write something like ``x: list[int]``. |
| 344 |
|
| 345 |
.. option:: --disallow-subclassing-any |
| 346 |
|
| 347 |
This flag reports an error whenever a class subclasses a value of |
| 348 |
type ``Any``. This may occur when the base class is imported from |
| 349 |
a module that doesn't exist (when using |
| 350 |
:option:`--ignore-missing-imports`) or is |
| 351 |
ignored due to :option:`--follow-imports=skip <--follow-imports>` or a |
| 352 |
``# type: ignore`` comment on the ``import`` statement. |
| 353 |
|
| 354 |
Since the module is silenced, the imported class is given a type of ``Any``. |
| 355 |
By default mypy will assume that the subclass correctly inherited |
| 356 |
the base class even though that may not actually be the case. This |
| 357 |
flag makes mypy raise an error instead. |
| 358 |
|
| 359 |
|
| 360 |
.. _untyped-definitions-and-calls: |
| 361 |
|
| 362 |
Untyped definitions and calls |
| 363 |
***************************** |
| 364 |
|
| 365 |
The following flags configure how mypy handles untyped function |
| 366 |
definitions or calls. |
| 367 |
|
| 368 |
.. option:: --disallow-untyped-calls |
| 369 |
|
| 370 |
This flag reports an error whenever a function with type annotations |
| 371 |
calls a function defined without annotations. |
| 372 |
|
| 373 |
.. option:: --untyped-calls-exclude |
| 374 |
|
| 375 |
This flag allows one to selectively disable :option:`--disallow-untyped-calls` |
| 376 |
for functions and methods defined in specific packages, modules, or classes. |
| 377 |
Note that each exclude entry acts as a prefix. For example (assuming there |
| 378 |
are no type annotations for ``third_party_lib`` available): |
| 379 |
|
| 380 |
.. code-block:: python |
| 381 |
|
| 382 |
# mypy --disallow-untyped-calls |
| 383 |
# --untyped-calls-exclude=third_party_lib.module_a |
| 384 |
# --untyped-calls-exclude=foo.A |
| 385 |
from third_party_lib.module_a import some_func |
| 386 |
from third_party_lib.module_b import other_func |
| 387 |
import foo |
| 388 |
|
| 389 |
some_func() # OK, function comes from module `third_party_lib.module_a` |
| 390 |
other_func() # E: Call to untyped function "other_func" in typed context |
| 391 |
|
| 392 |
foo.A().meth() # OK, method was defined in class `foo.A` |
| 393 |
foo.B().meth() # E: Call to untyped function "meth" in typed context |
| 394 |
|
| 395 |
# file foo.py |
| 396 |
class A: |
| 397 |
def meth(self): pass |
| 398 |
class B: |
| 399 |
def meth(self): pass |
| 400 |
|
| 401 |
.. option:: --disallow-untyped-defs |
| 402 |
|
| 403 |
This flag reports an error whenever it encounters a function definition |
| 404 |
without type annotations or with incomplete type annotations. |
| 405 |
(a superset of :option:`--disallow-incomplete-defs`). |
| 406 |
|
| 407 |
For example, it would report an error for :code:`def f(a, b)` and :code:`def f(a: int, b)`. |
| 408 |
|
| 409 |
.. option:: --disallow-incomplete-defs |
| 410 |
|
| 411 |
This flag reports an error whenever it encounters a partly annotated |
| 412 |
function definition, while still allowing entirely unannotated definitions. |
| 413 |
|
| 414 |
For example, it would report an error for :code:`def f(a: int, b)` but not :code:`def f(a, b)`. |
| 415 |
|
| 416 |
.. option:: --check-untyped-defs |
| 417 |
|
| 418 |
This flag is less severe than the previous two options -- it type checks |
| 419 |
the body of every function, regardless of whether it has type annotations. |
| 420 |
(By default the bodies of functions without annotations are not type |
| 421 |
checked.) |
| 422 |
|
| 423 |
It will assume all arguments have type ``Any`` and always infer ``Any`` |
| 424 |
as the return type. |
| 425 |
|
| 426 |
.. option:: --disallow-untyped-decorators |
| 427 |
|
| 428 |
This flag reports an error whenever a function with type annotations |
| 429 |
is decorated with a decorator without annotations. |
| 430 |
|
| 431 |
|
| 432 |
.. _none-and-optional-handling: |
| 433 |
|
| 434 |
None and Optional handling |
| 435 |
************************** |
| 436 |
|
| 437 |
The following flags adjust how mypy handles values of type ``None``. |
| 438 |
|
| 439 |
.. _implicit-optional: |
| 440 |
|
| 441 |
.. option:: --implicit-optional |
| 442 |
|
| 443 |
This flag causes mypy to treat parameters with a ``None`` |
| 444 |
default value as having an implicit optional type (``T | None``). |
| 445 |
|
| 446 |
For example, if this flag is set, mypy would assume that the ``x`` |
| 447 |
parameter is actually of type ``int | None`` in the code snippet below, |
| 448 |
since the default parameter is ``None``: |
| 449 |
|
| 450 |
.. code-block:: python |
| 451 |
|
| 452 |
def foo(x: int = None) -> None: |
| 453 |
print(x) |
| 454 |
|
| 455 |
**Note:** This was disabled by default starting in mypy 0.980. |
| 456 |
|
| 457 |
.. _no_strict_optional: |
| 458 |
|
| 459 |
.. option:: --no-strict-optional |
| 460 |
|
| 461 |
This flag effectively disables checking of optional |
| 462 |
types and ``None`` values. With this option, mypy doesn't |
| 463 |
generally check the use of ``None`` values -- it is treated |
| 464 |
as compatible with every type. |
| 465 |
|
| 466 |
.. warning:: |
| 467 |
|
| 468 |
``--no-strict-optional`` is evil. Avoid using it and definitely do |
| 469 |
not use it without understanding what it does. |
| 470 |
|
| 471 |
|
| 472 |
.. _configuring-warnings: |
| 473 |
|
| 474 |
Configuring warnings |
| 475 |
******************** |
| 476 |
|
| 477 |
The following flags enable warnings for code that is sound but is |
| 478 |
potentially problematic or redundant in some way. |
| 479 |
|
| 480 |
.. option:: --warn-redundant-casts |
| 481 |
|
| 482 |
This flag will make mypy report an error whenever your code uses |
| 483 |
an unnecessary cast that can safely be removed. |
| 484 |
|
| 485 |
.. option:: --warn-unused-ignores |
| 486 |
|
| 487 |
This flag will make mypy report an error whenever your code uses |
| 488 |
a ``# type: ignore`` comment on a line that is not actually |
| 489 |
generating an error message. |
| 490 |
|
| 491 |
This flag, along with the :option:`--warn-redundant-casts` flag, are both |
| 492 |
particularly useful when you are upgrading mypy. Previously, |
| 493 |
you may have needed to add casts or ``# type: ignore`` annotations |
| 494 |
to work around bugs in mypy or missing stubs for 3rd party libraries. |
| 495 |
|
| 496 |
These two flags let you discover cases where either workarounds are |
| 497 |
no longer necessary. |
| 498 |
|
| 499 |
.. option:: --no-warn-no-return |
| 500 |
|
| 501 |
By default, mypy will generate errors when a function is missing |
| 502 |
return statements in some execution paths. The only exceptions |
| 503 |
are when: |
| 504 |
|
| 505 |
- The function has a ``None`` or ``Any`` return type |
| 506 |
- The function has an empty body and is marked as an abstract method, |
| 507 |
is in a protocol class, or is in a stub file |
| 508 |
- The execution path can never return; for example, if an exception |
| 509 |
is always raised |
| 510 |
|
| 511 |
Passing in :option:`--no-warn-no-return` will disable these error |
| 512 |
messages in all cases. |
| 513 |
|
| 514 |
.. option:: --warn-return-any |
| 515 |
|
| 516 |
This flag causes mypy to generate a warning when returning a value |
| 517 |
with type ``Any`` from a function declared with a non-``Any`` return type. |
| 518 |
|
| 519 |
.. option:: --warn-unreachable |
| 520 |
|
| 521 |
This flag will make mypy report an error whenever it encounters |
| 522 |
code determined to be unreachable or redundant after performing type analysis. |
| 523 |
This can be a helpful way of detecting certain kinds of bugs in your code. |
| 524 |
|
| 525 |
For example, enabling this flag will make mypy report that the ``x > 7`` |
| 526 |
check is redundant and that the ``else`` block below is unreachable. |
| 527 |
|
| 528 |
.. code-block:: python |
| 529 |
|
| 530 |
def process(x: int) -> None: |
| 531 |
# Error: Right operand of "or" is never evaluated |
| 532 |
if isinstance(x, int) or x > 7: |
| 533 |
# Error: Unsupported operand types for + ("int" and "str") |
| 534 |
print(x + "bad") |
| 535 |
else: |
| 536 |
# Error: 'Statement is unreachable' error |
| 537 |
print(x + "bad") |
| 538 |
|
| 539 |
To help prevent mypy from generating spurious warnings, the "Statement is |
| 540 |
unreachable" warning will be silenced in exactly three cases: |
| 541 |
|
| 542 |
1. When the unreachable statement is a ``raise`` statement, is an |
| 543 |
``assert False`` statement, or calls a function that has the :py:data:`~typing.NoReturn` |
| 544 |
return type hint. In other words, when the unreachable statement |
| 545 |
throws an error or terminates the program in some way. |
| 546 |
2. When the unreachable statement is ``return NotImplemented``. This |
| 547 |
is allowed by mypy due to its use in operator overloading. |
| 548 |
3. When the unreachable statement was *intentionally* marked as unreachable |
| 549 |
using :ref:`version_and_platform_checks`. |
| 550 |
|
| 551 |
.. note:: |
| 552 |
|
| 553 |
Mypy currently cannot detect and report unreachable or redundant code |
| 554 |
inside any functions using :ref:`value-constrained type variables <value-constrained-type-variables>`. |
| 555 |
|
| 556 |
This limitation will be removed in future releases of mypy. |
| 557 |
|
| 558 |
.. option:: --report-deprecated-as-note |
| 559 |
|
| 560 |
If error code ``deprecated`` is enabled, mypy emits errors if your code |
| 561 |
imports or uses deprecated features. This flag converts such errors to |
| 562 |
notes, causing mypy to eventually finish with a zero exit code. Features |
| 563 |
are considered deprecated when decorated with ``warnings.deprecated``. |
| 564 |
|
| 565 |
.. option:: --deprecated-calls-exclude |
| 566 |
|
| 567 |
This flag allows one to selectively disable :ref:`deprecated<code-deprecated>` warnings |
| 568 |
for functions and methods defined in specific packages, modules, or classes. |
| 569 |
Note that each exclude entry acts as a prefix. For example (assuming ``foo.A.func`` is deprecated): |
| 570 |
|
| 571 |
.. code-block:: python |
| 572 |
|
| 573 |
# mypy --enable-error-code deprecated |
| 574 |
# --deprecated-calls-exclude=foo.A |
| 575 |
import foo |
| 576 |
|
| 577 |
foo.A().func() # OK, the deprecated warning is ignored |
| 578 |
|
| 579 |
# file foo.py |
| 580 |
from typing_extensions import deprecated |
| 581 |
class A: |
| 582 |
@deprecated("Use A.func2 instead") |
| 583 |
def func(self): pass |
| 584 |
|
| 585 |
.. _miscellaneous-strictness-flags: |
| 586 |
|
| 587 |
Miscellaneous strictness flags |
| 588 |
****************************** |
| 589 |
|
| 590 |
This section documents any other flags that do not neatly fall under any |
| 591 |
of the above sections. |
| 592 |
|
| 593 |
.. option:: --allow-untyped-globals |
| 594 |
|
| 595 |
This flag causes mypy to suppress errors caused by not being able to fully |
| 596 |
infer the types of global and class variables. |
| 597 |
|
| 598 |
.. option:: --allow-redefinition |
| 599 |
|
| 600 |
By default, mypy won't allow a variable to be redefined with an |
| 601 |
unrelated type. This flag enables the redefinition of *unannotated* |
| 602 |
variables with an arbitrary type. This also requires |
| 603 |
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is |
| 604 |
enabled by default starting from mypy 2.0. Example: |
| 605 |
|
| 606 |
.. code-block:: python |
| 607 |
|
| 608 |
def maybe_convert(n: int, b: bool) -> int | str: |
| 609 |
if b: |
| 610 |
x = str(n) # Assign "str" |
| 611 |
else: |
| 612 |
x = n # Assign "int" |
| 613 |
# Type of "x" is "int | str" here. |
| 614 |
return x |
| 615 |
|
| 616 |
Without this flag, mypy only supports inferring optional types |
| 617 |
(``X | None``) from multiple assignments. With this option enabled, |
| 618 |
mypy can infer arbitrary union types. |
| 619 |
|
| 620 |
This also enables an unannotated variable to have different types in different |
| 621 |
code locations: |
| 622 |
|
| 623 |
.. code-block:: python |
| 624 |
|
| 625 |
if check(): |
| 626 |
for x in range(n): |
| 627 |
# Type of "x" is "int" here. |
| 628 |
... |
| 629 |
else: |
| 630 |
for x in ['a', 'b']: |
| 631 |
# Type of "x" is "str" here. |
| 632 |
... |
| 633 |
|
| 634 |
Function arguments are special, changing their type within function body |
| 635 |
is allowed even if they are annotated, but that annotation is used to infer |
| 636 |
types of r.h.s. of all subsequent assignments. Such middle-ground semantics |
| 637 |
provides good balance for majority of common use cases. For example: |
| 638 |
|
| 639 |
.. code-block:: python |
| 640 |
|
| 641 |
def process(values: list[float]) -> None: |
| 642 |
if not values: |
| 643 |
values = [0, 0, 0] |
| 644 |
reveal_type(values) # Revealed type is list[float] |
| 645 |
|
| 646 |
Note: We are planning to turn this flag on by default in a future mypy |
| 647 |
release. |
| 648 |
|
| 649 |
.. option:: --allow-redefinition-new |
| 650 |
|
| 651 |
Deprecated alias for :option:`--allow-redefinition <mypy --allow-redefinition>`. |
| 652 |
|
| 653 |
.. option:: --allow-redefinition-old |
| 654 |
|
| 655 |
This is an older, more limited variant of |
| 656 |
:option:`--allow-redefinition <mypy --allow-redefinition>`. |
| 657 |
This flag enables redefinition of a variable with an |
| 658 |
arbitrary type *in some contexts*: only redefinitions within the |
| 659 |
same block and nesting depth as the original definition are allowed. |
| 660 |
|
| 661 |
We have no plans to remove this flag, but |
| 662 |
:option:`--allow-redefinition <mypy --allow-redefinition>` |
| 663 |
is recommended for new use cases. |
| 664 |
|
| 665 |
Example where this can be useful: |
| 666 |
|
| 667 |
.. code-block:: python |
| 668 |
|
| 669 |
def process(items: list[str]) -> None: |
| 670 |
# 'items' has type list[str] |
| 671 |
items = [item.split() for item in items] |
| 672 |
# 'items' now has type list[list[str]] |
| 673 |
|
| 674 |
The variable must be used before it can be redefined: |
| 675 |
|
| 676 |
.. code-block:: python |
| 677 |
|
| 678 |
def process(items: list[str]) -> None: |
| 679 |
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet |
| 680 |
print(items) |
| 681 |
items = "100" # valid, items now has type str |
| 682 |
items = int(items) # valid, items now has type int |
| 683 |
|
| 684 |
.. option:: --no-local-partial-types |
| 685 |
|
| 686 |
Disable local partial types to enable legacy type inference mode for |
| 687 |
containers. |
| 688 |
|
| 689 |
Local partial types prevent inferring a container type for a variable, when |
| 690 |
the initial assignment happens at module top level or in a class body, and |
| 691 |
the container item type is only set in a function. Example: |
| 692 |
|
| 693 |
.. code-block:: python |
| 694 |
|
| 695 |
a = [] # Need type annotation unless using --no-local-partial-types |
| 696 |
|
| 697 |
def func() -> None: |
| 698 |
a.append(1) |
| 699 |
|
| 700 |
reveal_type(a) # "list[int]" if using --no-local-partial-types |
| 701 |
|
| 702 |
Local partial types are enabled by default starting from mypy 2.0. The |
| 703 |
mypy daemon requires local partial types. |
| 704 |
|
| 705 |
.. option:: --no-implicit-reexport |
| 706 |
|
| 707 |
By default, imported values to a module are treated as exported and mypy allows |
| 708 |
other modules to import them. This flag changes the behavior to not re-export unless |
| 709 |
the item is imported using from-as or is included in ``__all__``. Note this is |
| 710 |
always treated as enabled for stub files. For example: |
| 711 |
|
| 712 |
.. code-block:: python |
| 713 |
|
| 714 |
# This won't re-export the value |
| 715 |
from foo import bar |
| 716 |
|
| 717 |
# Neither will this |
| 718 |
from foo import bar as bang |
| 719 |
|
| 720 |
# This will re-export it as bar and allow other modules to import it |
| 721 |
from foo import bar as bar |
| 722 |
|
| 723 |
# This will also re-export bar |
| 724 |
from foo import bar |
| 725 |
__all__ = ['bar'] |
| 726 |
|
| 727 |
|
| 728 |
.. option:: --strict-equality |
| 729 |
|
| 730 |
By default, mypy allows always-false comparisons like ``42 == 'no'``. |
| 731 |
Use this flag to prohibit such comparisons of non-overlapping types, and |
| 732 |
similar identity and container checks: |
| 733 |
|
| 734 |
.. code-block:: python |
| 735 |
|
| 736 |
items: list[int] |
| 737 |
if 'some string' in items: # Error: non-overlapping container check! |
| 738 |
... |
| 739 |
|
| 740 |
text: str |
| 741 |
if text != b'other bytes': # Error: non-overlapping equality check! |
| 742 |
... |
| 743 |
|
| 744 |
assert text is not None # OK, check against None is allowed |
| 745 |
|
| 746 |
|
| 747 |
.. option:: --strict-equality-for-none |
| 748 |
|
| 749 |
This flag extends :option:`--strict-equality <mypy --strict-equality>` for checks |
| 750 |
against ``None``: |
| 751 |
|
| 752 |
.. code-block:: python |
| 753 |
|
| 754 |
text: str |
| 755 |
assert text is not None # Error: non-overlapping identity check! |
| 756 |
|
| 757 |
Note that :option:`--strict-equality-for-none <mypy --strict-equality-for-none>` |
| 758 |
only works in combination with :option:`--strict-equality <mypy --strict-equality>`. |
| 759 |
|
| 760 |
.. option:: --no-strict-bytes |
| 761 |
|
| 762 |
Treat ``bytearray`` and ``memoryview`` as subtypes of ``bytes``. This is not true |
| 763 |
at runtime and can lead to unexpected behavior. This was the default behavior prior |
| 764 |
to mypy 2.0. |
| 765 |
|
| 766 |
.. code-block:: python |
| 767 |
|
| 768 |
def f(buf: bytes) -> None: |
| 769 |
assert isinstance(buf, bytes) # Raises runtime AssertionError with bytearray/memoryview |
| 770 |
with open("binary_file", "wb") as fp: |
| 771 |
fp.write(buf) |
| 772 |
|
| 773 |
# Using --no-strict-bytes disables the following errors |
| 774 |
f(bytearray(b"")) # Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" |
| 775 |
f(memoryview(b"")) # Argument 1 to "f" has incompatible type "memoryview"; expected "bytes" |
| 776 |
|
| 777 |
# If `f` accepts any object that implements the buffer protocol, |
| 778 |
# consider using Buffer instead: |
| 779 |
from collections.abc import Buffer # "from typing_extensions" in Python 3.11 and earlier |
| 780 |
|
| 781 |
def f(buf: Buffer) -> None: |
| 782 |
with open("binary_file", "wb") as fp: |
| 783 |
fp.write(buf) |
| 784 |
|
| 785 |
f(b"") # Ok |
| 786 |
f(bytearray(b"")) # Ok |
| 787 |
f(memoryview(b"")) # Ok |
| 788 |
|
| 789 |
|
| 790 |
.. option:: --extra-checks |
| 791 |
|
| 792 |
This flag enables additional checks that are technically correct but may be |
| 793 |
impractical. In particular, it prohibits partial overlap in ``TypedDict`` updates, |
| 794 |
and makes arguments prepended via ``Concatenate`` positional-only. For example: |
| 795 |
|
| 796 |
.. code-block:: python |
| 797 |
|
| 798 |
from typing import TypedDict |
| 799 |
|
| 800 |
class Foo(TypedDict): |
| 801 |
a: int |
| 802 |
|
| 803 |
class Bar(TypedDict): |
| 804 |
a: int |
| 805 |
b: int |
| 806 |
|
| 807 |
def test(foo: Foo, bar: Bar) -> None: |
| 808 |
# This is technically unsafe since foo can have a subtype of Foo at |
| 809 |
# runtime, where type of key "b" is incompatible with int, see below |
| 810 |
bar.update(foo) |
| 811 |
|
| 812 |
class Bad(Foo): |
| 813 |
b: str |
| 814 |
bad: Bad = {"a": 0, "b": "no"} |
| 815 |
test(bad, bar) |
| 816 |
|
| 817 |
In future more checks may be added to this flag if: |
| 818 |
|
| 819 |
* The corresponding use cases are rare, thus not justifying a dedicated |
| 820 |
strictness flag. |
| 821 |
|
| 822 |
* The new check cannot be supported as an opt-in error code. |
| 823 |
|
| 824 |
.. option:: --strict |
| 825 |
|
| 826 |
This flag mode enables a defined subset of optional error-checking flags. |
| 827 |
This subset primarily includes checks for inadvertent type unsoundness (i.e |
| 828 |
strict will catch type errors as long as intentional methods like type ignore |
| 829 |
or casting were not used.) |
| 830 |
|
| 831 |
Note: the :option:`--warn-unreachable` flag |
| 832 |
is not automatically enabled by the strict flag. |
| 833 |
|
| 834 |
The strict flag does not take precedence over other strict-related flags. |
| 835 |
Directly specifying a flag of alternate behavior will override the |
| 836 |
behavior of strict, regardless of the order in which they are passed. |
| 837 |
You can see the list of flags enabled by strict mode in the full |
| 838 |
:option:`mypy --help` output. |
| 839 |
|
| 840 |
Note: the exact list of flags enabled by running :option:`--strict` may change |
| 841 |
over time. |
| 842 |
|
| 843 |
.. include:: strict_list.rst |
| 844 |
.. |
| 845 |
The above file is autogenerated and included during html generation. |
| 846 |
(That's an include directive, and this is a comment.) |
| 847 |
It would be fine to generate it at some other time instead, |
| 848 |
theoretically, but we already had a convenient hook during html gen. |
| 849 |
|
| 850 |
|
| 851 |
.. option:: --disable-error-code |
| 852 |
|
| 853 |
This flag allows disabling one or multiple error codes globally. |
| 854 |
See :ref:`error-codes` for more information. |
| 855 |
|
| 856 |
.. code-block:: python |
| 857 |
|
| 858 |
# no flag |
| 859 |
x = 'a string' |
| 860 |
x.trim() # error: "str" has no attribute "trim" [attr-defined] |
| 861 |
|
| 862 |
# When using --disable-error-code attr-defined |
| 863 |
x = 'a string' |
| 864 |
x.trim() |
| 865 |
|
| 866 |
.. option:: --enable-error-code |
| 867 |
|
| 868 |
This flag allows enabling one or multiple error codes globally. |
| 869 |
See :ref:`error-codes` for more information. |
| 870 |
|
| 871 |
Note: This flag will override disabled error codes from the |
| 872 |
:option:`--disable-error-code <mypy --disable-error-code>` flag. |
| 873 |
|
| 874 |
.. code-block:: python |
| 875 |
|
| 876 |
# When using --disable-error-code attr-defined |
| 877 |
x = 'a string' |
| 878 |
x.trim() |
| 879 |
|
| 880 |
# --disable-error-code attr-defined --enable-error-code attr-defined |
| 881 |
x = 'a string' |
| 882 |
x.trim() # error: "str" has no attribute "trim" [attr-defined] |
| 883 |
|
| 884 |
|
| 885 |
.. _configuring-error-messages: |
| 886 |
|
| 887 |
Configuring error messages |
| 888 |
************************** |
| 889 |
|
| 890 |
The following flags let you adjust how much detail mypy displays |
| 891 |
in error messages. |
| 892 |
|
| 893 |
.. option:: --show-error-context |
| 894 |
|
| 895 |
This flag will precede all errors with "note" messages explaining the |
| 896 |
context of the error. For example, consider the following program: |
| 897 |
|
| 898 |
.. code-block:: python |
| 899 |
|
| 900 |
class Test: |
| 901 |
def foo(self, x: int) -> int: |
| 902 |
return x + "bar" |
| 903 |
|
| 904 |
Mypy normally displays an error message that looks like this:: |
| 905 |
|
| 906 |
main.py:3: error: Unsupported operand types for + ("int" and "str") |
| 907 |
|
| 908 |
If we enable this flag, the error message now looks like this:: |
| 909 |
|
| 910 |
main.py: note: In member "foo" of class "Test": |
| 911 |
main.py:3: error: Unsupported operand types for + ("int" and "str") |
| 912 |
|
| 913 |
.. option:: --show-column-numbers |
| 914 |
|
| 915 |
This flag will add column offsets to error messages. |
| 916 |
For example, the following indicates an error in line 12, column 9 |
| 917 |
(note that column offsets are 0-based):: |
| 918 |
|
| 919 |
main.py:12:9: error: Unsupported operand types for / ("int" and "str") |
| 920 |
|
| 921 |
.. option:: --show-error-code-links |
| 922 |
|
| 923 |
This flag will also display a link to error code documentation, anchored to the error code reported by mypy. |
| 924 |
The corresponding error code will be highlighted within the documentation page. |
| 925 |
If we enable this flag, the error message now looks like this:: |
| 926 |
|
| 927 |
main.py:3: error: Unsupported operand types for - ("int" and "str") [operator] |
| 928 |
main.py:3: note: See 'https://mypy.rtfd.io/en/stable/_refs.html#code-operator' for more info |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
.. option:: --show-error-end |
| 933 |
|
| 934 |
This flag will make mypy show not just that start position where |
| 935 |
an error was detected, but also the end position of the relevant expression. |
| 936 |
This way various tools can easily highlight the whole error span. The format is |
| 937 |
``file:line:column:end_line:end_column``. This option implies |
| 938 |
``--show-column-numbers``. |
| 939 |
|
| 940 |
.. option:: --hide-error-codes |
| 941 |
|
| 942 |
This flag will hide the error code ``[<code>]`` from error messages. By default, the error |
| 943 |
code is shown after each error message:: |
| 944 |
|
| 945 |
prog.py:1: error: "str" has no attribute "trim" [attr-defined] |
| 946 |
|
| 947 |
See :ref:`error-codes` for more information. |
| 948 |
|
| 949 |
.. option:: --pretty |
| 950 |
|
| 951 |
Use visually nicer output in error messages: use soft word wrap, |
| 952 |
show source code snippets, and show error location markers. |
| 953 |
|
| 954 |
.. option:: --no-color-output |
| 955 |
|
| 956 |
This flag will disable color output in error messages, enabled by default. |
| 957 |
|
| 958 |
.. option:: --no-error-summary |
| 959 |
|
| 960 |
This flag will disable error summary. By default mypy shows a summary line |
| 961 |
including total number of errors, number of files with errors, and number |
| 962 |
of files checked. |
| 963 |
|
| 964 |
.. option:: --show-absolute-path |
| 965 |
|
| 966 |
Show absolute paths to files. |
| 967 |
|
| 968 |
.. option:: --soft-error-limit N |
| 969 |
|
| 970 |
This flag will adjust the limit after which mypy will (sometimes) |
| 971 |
disable reporting most additional errors. The limit only applies |
| 972 |
if it seems likely that most of the remaining errors will not be |
| 973 |
useful or they may be overly noisy. If ``N`` is negative, there is |
| 974 |
no limit. The default limit is -1. |
| 975 |
|
| 976 |
|
| 977 |
.. _incremental: |
| 978 |
|
| 979 |
Incremental mode |
| 980 |
**************** |
| 981 |
|
| 982 |
By default, mypy will store type information into a cache. Mypy |
| 983 |
will use this information to avoid unnecessary recomputation when |
| 984 |
it type checks your code again. This can help speed up the type |
| 985 |
checking process, especially when most parts of your program have |
| 986 |
not changed since the previous mypy run. |
| 987 |
|
| 988 |
If you want to speed up how long it takes to recheck your code |
| 989 |
beyond what incremental mode can offer, try running mypy in |
| 990 |
:ref:`daemon mode <mypy_daemon>`. |
| 991 |
|
| 992 |
.. option:: --no-incremental |
| 993 |
|
| 994 |
This flag disables incremental mode: mypy will no longer reference |
| 995 |
the cache when re-run. |
| 996 |
|
| 997 |
Note that mypy will still write out to the cache even when |
| 998 |
incremental mode is disabled: see the :option:`--cache-dir` flag below |
| 999 |
for more details. |
| 1000 |
|
| 1001 |
.. option:: --cache-dir DIR |
| 1002 |
|
| 1003 |
By default, mypy stores all cache data inside of a folder named |
| 1004 |
``.mypy_cache`` in the current directory. This flag lets you |
| 1005 |
change this folder. This flag can also be useful for controlling |
| 1006 |
cache use when using :ref:`remote caching <remote-cache>`. |
| 1007 |
|
| 1008 |
This setting will override the ``MYPY_CACHE_DIR`` environment |
| 1009 |
variable if it is set. |
| 1010 |
|
| 1011 |
Mypy will also always write to the cache even when incremental |
| 1012 |
mode is disabled so it can "warm up" the cache. To disable |
| 1013 |
writing to the cache, use ``--cache-dir=/dev/null`` (UNIX) |
| 1014 |
or ``--cache-dir=nul`` (Windows). |
| 1015 |
|
| 1016 |
.. option:: --no-sqlite-cache |
| 1017 |
|
| 1018 |
Avoid using `SQLite`_ database to store the cache, instead write cache data |
| 1019 |
out to individual files. |
| 1020 |
|
| 1021 |
.. option:: --cache-fine-grained |
| 1022 |
|
| 1023 |
Include fine-grained dependency information in the cache for the mypy daemon. |
| 1024 |
|
| 1025 |
.. option:: --skip-version-check |
| 1026 |
|
| 1027 |
By default, mypy will ignore cache data generated by a different |
| 1028 |
version of mypy. This flag disables that behavior. |
| 1029 |
|
| 1030 |
.. option:: --skip-cache-mtime-checks |
| 1031 |
|
| 1032 |
Skip cache internal consistency checks based on mtime. |
| 1033 |
|
| 1034 |
|
| 1035 |
.. _parallel: |
| 1036 |
|
| 1037 |
Parallel type-checking |
| 1038 |
********************** |
| 1039 |
|
| 1040 |
By default, mypy checks all modules in the same Python process. This can be slow |
| 1041 |
for large code bases. Mypy offers experimental parallel type-checking mode using |
| 1042 |
multiple worker processes. In parallel mode, modules that do not depend om each |
| 1043 |
other are type-checked in parallel. :ref:`Incremental cache <incremental>` is |
| 1044 |
used to manage most of the shared state. Parallel type-checking also requires |
| 1045 |
:option:`--local-partial-types <mypy --no-local-partial-types>`, which is |
| 1046 |
enabled by default starting from mypy 2.0. |
| 1047 |
|
| 1048 |
.. option:: -n NUMBER, --num-workers NUMBER |
| 1049 |
|
| 1050 |
Use ``NUMBER`` parallel worker processes (in addition to the coordinator |
| 1051 |
process) to perform type-checking. Specifying ``--num-workers 0`` (default) |
| 1052 |
disables parallel checking. Automatic detection of the optimal number |
| 1053 |
of workers is not supported yet. |
| 1054 |
|
| 1055 |
This setting will override the ``MYPY_NUM_WORKERS`` environment |
| 1056 |
variable if it is set. |
| 1057 |
|
| 1058 |
Notes: |
| 1059 |
|
| 1060 |
* An import cycle is always processed as a whole by a worker process. Thus, |
| 1061 |
avoiding large import cycles in your code will *significantly* improve |
| 1062 |
type-checking speed. |
| 1063 |
|
| 1064 |
* Specifying a number of workers that is larger than the number of *physical* |
| 1065 |
CPU cores is not beneficial, since mypy is usually CPU bound. Best way to |
| 1066 |
tune the number of workers on a given machine is to start from 3-4 workers |
| 1067 |
and increase the number while you see a performance improvement. |
| 1068 |
|
| 1069 |
* Parallel mode requires and automatically enables :option:`--native-parser`. |
| 1070 |
|
| 1071 |
|
| 1072 |
Advanced options |
| 1073 |
**************** |
| 1074 |
|
| 1075 |
The following flags are useful mostly for people who are interested |
| 1076 |
in developing or debugging mypy internals. |
| 1077 |
|
| 1078 |
.. option:: --pdb |
| 1079 |
|
| 1080 |
This flag will invoke the Python debugger when mypy encounters |
| 1081 |
a fatal error. |
| 1082 |
|
| 1083 |
.. option:: --show-traceback, --tb |
| 1084 |
|
| 1085 |
If set, this flag will display a full traceback when mypy |
| 1086 |
encounters a fatal error. |
| 1087 |
|
| 1088 |
.. option:: --raise-exceptions |
| 1089 |
|
| 1090 |
Raise exception on fatal error. |
| 1091 |
|
| 1092 |
.. option:: --custom-typing-module MODULE |
| 1093 |
|
| 1094 |
This flag lets you use a custom module as a substitute for the |
| 1095 |
:py:mod:`typing` module. |
| 1096 |
|
| 1097 |
.. option:: --custom-typeshed-dir DIR |
| 1098 |
|
| 1099 |
This flag specifies the directory where mypy looks for standard library typeshed |
| 1100 |
stubs, instead of the typeshed that ships with mypy. This is |
| 1101 |
primarily intended to make it easier to test typeshed changes before |
| 1102 |
submitting them upstream, but also allows you to use a forked version of |
| 1103 |
typeshed. |
| 1104 |
|
| 1105 |
Note that this doesn't affect third-party library stubs. To test third-party stubs, |
| 1106 |
for example try ``MYPYPATH=stubs/six mypy ...``. |
| 1107 |
|
| 1108 |
.. _warn-incomplete-stub: |
| 1109 |
|
| 1110 |
.. option:: --warn-incomplete-stub |
| 1111 |
|
| 1112 |
This flag modifies both the :option:`--disallow-untyped-defs` and |
| 1113 |
:option:`--disallow-incomplete-defs` flags so they also report errors |
| 1114 |
if stubs in typeshed are missing type annotations or has incomplete |
| 1115 |
annotations. If both flags are missing, :option:`--warn-incomplete-stub` |
| 1116 |
also does nothing. |
| 1117 |
|
| 1118 |
This flag is mainly intended to be used by people who want contribute |
| 1119 |
to typeshed and would like a convenient way to find gaps and omissions. |
| 1120 |
|
| 1121 |
If you want mypy to report an error when your codebase *uses* an untyped |
| 1122 |
function, whether that function is defined in typeshed or not, use the |
| 1123 |
:option:`--disallow-untyped-calls` flag. See :ref:`untyped-definitions-and-calls` |
| 1124 |
for more details. |
| 1125 |
|
| 1126 |
.. _shadow-file: |
| 1127 |
|
| 1128 |
.. option:: --shadow-file SOURCE_FILE SHADOW_FILE |
| 1129 |
|
| 1130 |
When mypy is asked to type check ``SOURCE_FILE``, this flag makes mypy |
| 1131 |
read from and type check the contents of ``SHADOW_FILE`` instead. However, |
| 1132 |
diagnostics will continue to refer to ``SOURCE_FILE``. |
| 1133 |
|
| 1134 |
Specifying this argument multiple times (``--shadow-file X1 Y1 --shadow-file X2 Y2``) |
| 1135 |
will allow mypy to perform multiple substitutions. |
| 1136 |
|
| 1137 |
This allows tooling to create temporary files with helpful modifications |
| 1138 |
without having to change the source file in place. For example, suppose we |
| 1139 |
have a pipeline that adds ``reveal_type`` for certain variables. |
| 1140 |
This pipeline is run on ``original.py`` to produce ``temp.py``. |
| 1141 |
Running ``mypy --shadow-file original.py temp.py original.py`` will then |
| 1142 |
cause mypy to type check the contents of ``temp.py`` instead of ``original.py``, |
| 1143 |
but error messages will still reference ``original.py``. |
| 1144 |
|
| 1145 |
.. option:: --native-parser |
| 1146 |
|
| 1147 |
This enables fast Rust-based parser that parses directly to mypy AST. |
| 1148 |
It will become the default parser in one of the next mypy releases. |
| 1149 |
|
| 1150 |
|
| 1151 |
Report generation |
| 1152 |
***************** |
| 1153 |
|
| 1154 |
If these flags are set, mypy will generate a report in the specified |
| 1155 |
format into the specified directory. |
| 1156 |
|
| 1157 |
.. option:: --any-exprs-report DIR |
| 1158 |
|
| 1159 |
Causes mypy to generate a text file report documenting how many |
| 1160 |
expressions of type ``Any`` are present within your codebase. |
| 1161 |
|
| 1162 |
.. option:: --cobertura-xml-report DIR |
| 1163 |
|
| 1164 |
Causes mypy to generate a Cobertura XML type checking coverage report. |
| 1165 |
|
| 1166 |
To generate this report, you must either manually install the `lxml`_ |
| 1167 |
library or specify mypy installation with the setuptools extra |
| 1168 |
``mypy[reports]``. |
| 1169 |
|
| 1170 |
.. option:: --html-report / --xslt-html-report DIR |
| 1171 |
|
| 1172 |
Causes mypy to generate an HTML type checking coverage report. |
| 1173 |
|
| 1174 |
To generate this report, you must either manually install the `lxml`_ |
| 1175 |
library or specify mypy installation with the setuptools extra |
| 1176 |
``mypy[reports]``. |
| 1177 |
|
| 1178 |
.. option:: --linecount-report DIR |
| 1179 |
|
| 1180 |
Causes mypy to generate a text file report documenting the functions |
| 1181 |
and lines that are typed and untyped within your codebase. |
| 1182 |
|
| 1183 |
.. option:: --linecoverage-report DIR |
| 1184 |
|
| 1185 |
Causes mypy to generate a JSON file that maps each source file's |
| 1186 |
absolute filename to a list of line numbers that belong to typed |
| 1187 |
functions in that file. |
| 1188 |
|
| 1189 |
.. option:: --lineprecision-report DIR |
| 1190 |
|
| 1191 |
Causes mypy to generate a flat text file report with per-module |
| 1192 |
statistics of how many lines are typechecked etc. |
| 1193 |
|
| 1194 |
.. option:: --txt-report / --xslt-txt-report DIR |
| 1195 |
|
| 1196 |
Causes mypy to generate a text file type checking coverage report. |
| 1197 |
|
| 1198 |
To generate this report, you must either manually install the `lxml`_ |
| 1199 |
library or specify mypy installation with the setuptools extra |
| 1200 |
``mypy[reports]``. |
| 1201 |
|
| 1202 |
.. option:: --xml-report DIR |
| 1203 |
|
| 1204 |
Causes mypy to generate an XML type checking coverage report. |
| 1205 |
|
| 1206 |
To generate this report, you must either manually install the `lxml`_ |
| 1207 |
library or specify mypy installation with the setuptools extra |
| 1208 |
``mypy[reports]``. |
| 1209 |
|
| 1210 |
|
| 1211 |
Enabling incomplete/experimental features |
| 1212 |
***************************************** |
| 1213 |
|
| 1214 |
.. option:: --enable-incomplete-feature {PreciseTupleTypes,InlineTypedDict} |
| 1215 |
|
| 1216 |
Some features may require several mypy releases to implement, for example |
| 1217 |
due to their complexity, potential for backwards incompatibility, or |
| 1218 |
ambiguous semantics that would benefit from feedback from the community. |
| 1219 |
You can enable such features for early preview using this flag. Note that |
| 1220 |
it is not guaranteed that all features will be ultimately enabled by |
| 1221 |
default. In *rare cases* we may decide to not go ahead with certain |
| 1222 |
features. |
| 1223 |
|
| 1224 |
List of currently incomplete/experimental features: |
| 1225 |
|
| 1226 |
* ``PreciseTupleTypes``: this feature will infer more precise tuple types in |
| 1227 |
various scenarios. Before variadic types were added to the Python type system |
| 1228 |
by :pep:`646`, it was impossible to express a type like "a tuple with |
| 1229 |
at least two integers". The best type available was ``tuple[int, ...]``. |
| 1230 |
Therefore, mypy applied very lenient checking for variable-length tuples. |
| 1231 |
Now this type can be expressed as ``tuple[int, int, *tuple[int, ...]]``. |
| 1232 |
For such more precise types (when explicitly *defined* by a user) mypy, |
| 1233 |
for example, warns about unsafe index access, and generally handles them |
| 1234 |
in a type-safe manner. However, to avoid problems in existing code, mypy |
| 1235 |
does not *infer* these precise types when it technically can. Here are |
| 1236 |
notable examples where ``PreciseTupleTypes`` infers more precise types: |
| 1237 |
|
| 1238 |
.. code-block:: python |
| 1239 |
|
| 1240 |
numbers: tuple[int, ...] |
| 1241 |
|
| 1242 |
more_numbers = (1, *numbers, 1) |
| 1243 |
reveal_type(more_numbers) |
| 1244 |
# Without PreciseTupleTypes: tuple[int, ...] |
| 1245 |
# With PreciseTupleTypes: tuple[int, *tuple[int, ...], int] |
| 1246 |
|
| 1247 |
other_numbers = (1, 1) + numbers |
| 1248 |
reveal_type(other_numbers) |
| 1249 |
# Without PreciseTupleTypes: tuple[int, ...] |
| 1250 |
# With PreciseTupleTypes: tuple[int, int, *tuple[int, ...]] |
| 1251 |
|
| 1252 |
if len(numbers) > 2: |
| 1253 |
reveal_type(numbers) |
| 1254 |
# Without PreciseTupleTypes: tuple[int, ...] |
| 1255 |
# With PreciseTupleTypes: tuple[int, int, int, *tuple[int, ...]] |
| 1256 |
else: |
| 1257 |
reveal_type(numbers) |
| 1258 |
# Without PreciseTupleTypes: tuple[int, ...] |
| 1259 |
# With PreciseTupleTypes: tuple[()] | tuple[int] | tuple[int, int] |
| 1260 |
|
| 1261 |
* ``InlineTypedDict``: this feature enables non-standard syntax for inline |
| 1262 |
:ref:`TypedDicts <typeddict>`, for example: |
| 1263 |
|
| 1264 |
.. code-block:: python |
| 1265 |
|
| 1266 |
def test_values() -> {"width": int, "description": str}: |
| 1267 |
return {"width": 42, "description": "test"} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
Miscellaneous |
| 1272 |
************* |
| 1273 |
|
| 1274 |
.. option:: --install-types |
| 1275 |
|
| 1276 |
This flag causes mypy to install known missing stub packages for |
| 1277 |
third-party libraries using pip. It will display the pip command |
| 1278 |
that will be run, and expects a confirmation before installing |
| 1279 |
anything. For security reasons, these stubs are limited to only a |
| 1280 |
small subset of manually selected packages that have been |
| 1281 |
verified by the typeshed team. These packages include only stub |
| 1282 |
files and no executable code. |
| 1283 |
|
| 1284 |
If you use this option without providing any files or modules to |
| 1285 |
type check, mypy will install stub packages suggested during the |
| 1286 |
previous mypy run. If there are files or modules to type check, |
| 1287 |
mypy first type checks those, and proposes to install missing |
| 1288 |
stubs at the end of the run, but only if any missing modules were |
| 1289 |
detected. |
| 1290 |
|
| 1291 |
.. note:: |
| 1292 |
|
| 1293 |
This is new in mypy 0.900. Previous mypy versions included a |
| 1294 |
selection of third-party package stubs, instead of having |
| 1295 |
them installed separately. |
| 1296 |
|
| 1297 |
.. option:: --non-interactive |
| 1298 |
|
| 1299 |
When used together with :option:`--install-types <mypy |
| 1300 |
--install-types>`, this causes mypy to install all suggested stub |
| 1301 |
packages using pip without asking for confirmation, and then |
| 1302 |
continues to perform type checking using the installed stubs, if |
| 1303 |
some files or modules are provided to type check. |
| 1304 |
|
| 1305 |
This is implemented as up to two mypy runs internally. The first run |
| 1306 |
is used to find missing stub packages, and output is shown from |
| 1307 |
this run only if no missing stub packages were found. If missing |
| 1308 |
stub packages were found, they are installed and then another run |
| 1309 |
is performed. |
| 1310 |
|
| 1311 |
.. option:: --junit-xml JUNIT_XML_OUTPUT_FILE |
| 1312 |
|
| 1313 |
Causes mypy to generate a JUnit XML test result document with |
| 1314 |
type checking results. This can make it easier to integrate mypy |
| 1315 |
with continuous integration (CI) tools. |
| 1316 |
|
| 1317 |
.. option:: --junit-format {global,per_file} |
| 1318 |
|
| 1319 |
If --junit-xml is set, specifies format. |
| 1320 |
global (default): single test with all errors; |
| 1321 |
per_file: one test entry per file with failures. |
| 1322 |
|
| 1323 |
.. option:: --find-occurrences CLASS.MEMBER |
| 1324 |
|
| 1325 |
This flag will make mypy print out all usages of a class member |
| 1326 |
based on static type information. This feature is experimental. |
| 1327 |
|
| 1328 |
.. option:: --scripts-are-modules |
| 1329 |
|
| 1330 |
This flag will give command line arguments that appear to be |
| 1331 |
scripts (i.e. files whose name does not end in ``.py``) |
| 1332 |
a module name derived from the script name rather than the fixed |
| 1333 |
name :py:mod:`__main__`. |
| 1334 |
|
| 1335 |
This lets you check more than one script in a single mypy invocation. |
| 1336 |
(The default :py:mod:`__main__` is technically more correct, but if you |
| 1337 |
have many scripts that import a large package, the behavior enabled |
| 1338 |
by this flag is often more convenient.) |
| 1339 |
|
| 1340 |
.. _lxml: https://pypi.org/project/lxml/ |
| 1341 |
.. _SQLite: https://www.sqlite.org/ |