[attr-defined] after each error message. Error codes allow you to selectively silence or enable specific types of errors.
Understanding error codes
Error codes serve two main purposes:Selective silencing
Use
# type: ignore[code] to ignore specific errors without suppressing other important errors.Documentation
Error codes link to documentation explaining why the error occurred and how to fix it.
Displaying error codes
By default, error codes are shown in error messages:Silencing specific error codes
Per-line silencing
Use# type: ignore[code] to silence specific errors on a line:
Global enabling/disabling
Use command-line flags to enable or disable error codes globally:Per-module configuration
Configure error codes for specific modules:Per-file configuration
Use inline comments at the top of a file:Common error codes
Here are the most frequently used error codes from Mypy’s source:Type errors
Check that attribute exists.
Check that name is defined.
Check number, names, and kinds of arguments in calls.
Check argument types in calls.
Check that return value is compatible with signature.
Check that assigned value is compatible with target.
Check indexing operations.
Check that operator is valid for operands.
Import errors
Require that imported module can be found or has stubs.
Require that imported module can be found. (Subcode of
import)Require that imported module has stubs. (Subcode of
import)Definition errors
Check that every function has type annotations.Enable with:
--disallow-untyped-defs or --enable-error-code no-untyped-defDisallow calling untyped functions from typed functions.Enable with:
--disallow-untyped-calls or --enable-error-code no-untyped-callRequire variable annotation if type can’t be inferred.
Optional error codes
These error codes are disabled by default and must be explicitly enabled:Check that cast changes type of expression.Enable with:
--warn-redundant-casts or --enable-error-code redundant-castCheck that types in comparisons overlap.
Warn about unreachable statements or expressions.Enable with:
--warn-unreachable or --enable-error-code unreachableWarn about expressions always true in boolean contexts.Enable with:
--enable-error-code truthy-boolEnsure that all Enable with:
# type: ignore comments are necessary.--warn-unused-ignores or --enable-error-code unused-ignoreWarn about Enable with:
# type: ignore comments without error codes.--enable-error-code ignore-without-codeEnsure that all awaitable values are used.Enable with:
--enable-error-code unused-awaitableWarn about variables defined only in some execution paths.Enable with:
--enable-error-code possibly-undefinedRequire Enable with:
@override decorator for overridden methods.--enable-error-code explicit-overrideError code subcodes
Some error codes are subcodes of broader codes. When you ignore a parent code, subcodes are also ignored:import subcodes
import subcodes
import-not-found: Module implementation not foundimport-untyped: Module stubs not found
assignment subcodes
assignment subcodes
method-assign: Assignment target is a method
typeddict-item subcodes
typeddict-item subcodes
typeddict-unknown-key: Unknown key in TypedDict
Configuration hierarchy
Error code configuration follows this precedence:- Inline file configuration (
# mypy: enable-error-code=...) - Highest - Per-module config sections - Adjusts global settings
- Global configuration (command-line or config file) - Lowest
Best practices
Always specify error codes in type ignores
Always specify error codes in type ignores
Enable strict codes incrementally
Enable strict codes incrementally
Start with high-value codes and expand coverage:
Use per-module configs for legacy code
Use per-module configs for legacy code
Document why codes are disabled
Document why codes are disabled
Requiring error codes
Enableignore-without-code to require error codes in all # type: ignore comments: