Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
mypy 1.17.0 documentation
Logo
mypy 1.17.0 documentation

First steps

Type system reference

Configuring and running mypy

Miscellaneous

Project Links

Back to top

Error codes

Mypy can optionally display an error code such as[attr-defined]after each error message. Error codes serve two purposes:

  1. It’s possible to silence specific error codes on a line using#type:ignore[code]. This way you won’t accidentally ignore other,potentially more serious errors.

  2. The error code can be used to find documentation about the error.The next two topics (Error codes enabled by default andError codes for optional checks) document the various error codesmypy can report.

Most error codes are shared between multiple related error messages.Error codes may change in future mypy releases.

Silencing errors based on error codes

You can use a special comment#type:ignore[code,...] to onlyignore errors with a specific error code (or codes) on a particularline. This can be used even if you have not configured mypy to showerror codes.

This example shows how to ignore an error about an imported name mypythinks is undefined:

# 'foo' is defined in 'foolib', even though mypy can't see the# definition.fromfoolibimportfoo# type: ignore[attr-defined]

Enabling/disabling specific error codes globally

There are command-line flags and config file settings for enablingcertain optional error codes, such as--disallow-untyped-defs,which enables theno-untyped-def error code.

You can use--enable-error-codeand--disable-error-codeto enable or disable specific error codes that don’t have a dedicatedcommand-line flag or config file setting.

Per-module enabling/disabling error codes

You can useconfiguration file sections to enable ordisable specific error codes only in some modules. For example, thismypy.iniconfig will enable non-annotated empty containers in tests, while keepingother parts of code checked in strict mode:

[mypy]strict=True[mypy-tests.*]allow_untyped_defs=Trueallow_untyped_calls=Truedisable_error_code=var-annotated, has-type

Note that per-module enabling/disabling acts as override over the globaloptions. So that you don’t need to repeat the error code lists for eachmodule if you have them in global config section. For example:

[mypy]enable_error_code=truthy-bool, ignore-without-code, unused-awaitable[mypy-extensions.*]disable_error_code=unused-awaitable

The above config will allow unused awaitables in extension modules, but willstill keep the other two error codes enabled. The overall logic is following:

  • Command line and/or config main section set global error codes

  • Individual config sectionsadjust them per glob/module

  • Inline#mypy:disable-error-code="..." and#mypy:enable-error-code="..."comments can furtheradjust them for a specific file.For example:

# mypy: enable-error-code="truthy-bool, ignore-without-code"

So one can e.g. enable some code globally, disable it for all tests inthe corresponding config section, and then re-enable it with an inlinecomment in some specific test.

Subcodes of error codes

In some cases, mostly for backwards compatibility reasons, an errorcode may be covered also by another, wider error code. For example, an error withcode[method-assign] can be ignored by#type:ignore[assignment].Similar logic works for disabling error codes globally. If a given error codeis a subcode of another one, it will be mentioned in the documentation for the narrowercode. This hierarchy is not nested: there cannot be subcodes of othersubcodes.

Requiring error codes

It’s possible to require error codes be specified intype:ignore comments.Seeignore-without-code for more information.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp