Skip to main content

How to Ignore Specific MyPy Errors

Β· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Ignoring errors with MyPy should be a precise, surgical process, not a broad suppression. The most effective way to handle known, persistent issues (like dynamically assigned attributes or known type inconsistencies in third-party libraries) is by using the # type: ignore comment combined with the exact MyPy Error Code.

An experienced developer always aims for the narrowest possible scope of suppression to maintain maximum type-checking coverage across the rest of the codebase.

1. The Core Technique: Line-Level Ignore with Error Code​

The best practice is to place the ignore comment on the line causing the error and specify the exact error code in square brackets ([...]).

Mechanism: # type: ignore[error-code]​

This approach is superior to a simple # type: ignore because:

  1. Clarity: It documents why the line is ignored.
  2. Safety: If the code changes and the original error code is fixed, MyPy will raise a new error saying the ignore tag is unused (if warn_unused_ignores = True is set in your config), preventing redundant suppression.

Example A: Ignoring a Dynamic Attribute (attr-defined)​

This occurs when you assign an attribute to an object that wasn't explicitly defined in its class (a common dynamic Python pattern).

# Assume this class is defined elsewhere without slots
class DynamicObject:
pass

def process_data(data: dict):
# MyPy cannot know that 'status' will exist on obj.
obj = DynamicObject()

# ❌ MyPy Error: "DynamicObject" has no attribute "status"
obj.status = data.get("status_code", 200) # type: ignore[attr-defined]

# This line is still checked for other errors!
return obj

Example B: Ignoring Untyped Call/Function (no-untyped-def)​

This occurs when calling a function from a legacy or third-party library that lacks type hints.

# Assume 'untyped_lib' has a function 'calculate_raw' without annotations
import untyped_lib

def process_result(x: float):
# ❌ MyPy Error: Cannot infer type of 'calculate_raw'
# The return type might be unknown (Any)
raw_output = untyped_lib.calculate_raw(x) # type: ignore[no-untyped-call]

# We must explicitly cast or handle the type here if proceeding:
return int(raw_output)

2. Ignoring Specific Errors for an Entire Module​

If an entire file relies on a highly dynamic pattern (e.g., a file dedicated to ORM reflection or metaprogramming) and generates dozens of a single type of error, you can suppress that error type at the module level via your configuration file (pyproject.toml or mypy.ini).

Mechanism: Configuration File Per-Module Settings​

This is far better than using line-level ignores everywhere in the file.

# pyproject.toml
[tool.mypy]
# Global settings...

# Target a specific module (e.g., my_project.dynamic_models)
[tool.mypy.my_project.dynamic_models]

# Tell MyPy to ignore all 'attr-defined' errors only in this file/module.
disable_error_codes = [
"attr-defined",
"operator", # e.g., if operators are overloaded in a way MyPy struggles with
]

Scope: The attr-defined error is now suppressed for the entire dynamic_models module, but all other MyPy checks (like ensuring int is not assigned to a str) remain active.


3. Ignoring Missing Imports for a Library​

If a third-party library is not typed and is not included in the Typeshed stubs, MyPy will fail the build due to a "Cannot find implementation or library stub" error.

Mechanism: Per-Module ignore_missing_imports​

This is the recommended way to silence these errors, as it keeps the global ignore_missing_imports flag off, which should generally be avoided.

# pyproject.toml

# Global settings...
[tool.mypy.untyped_vendor_lib]
# Ignore the "Cannot find module" error for this specific library
ignore_missing_imports = True

Summary Table of Ignore Techniques​

ScopeMechanismConfigurationBest Use Case
Line-Level# type: ignore[error-code]Source Code CommentSingle, isolated dynamic code line.
Module-Leveldisable_error_codes = [...]pyproject.tomlEntire module relies on a dynamic pattern (e.g., ORM reflection).
Library-Levelignore_missing_imports = Truepyproject.tomlExternal library lacks type stubs.

Sources and Further Reading​

  1. MyPy Documentation - Error Codes List
  2. MyPy Documentation - Suppressing Errors
  3. MyPy Documentation - Configuration File (disabling errors per module)
  4. Real Python - Advanced MyPy Configuration