Sub topic 13
Silencing errors deliberately: ignore comments and cast
This chapter assumes you can run mypy or Pyright on a project and read their error messages. It also assumes you know what Any means. Sometimes a checker reports an error that you cannot fix with better annotations. The cause might be a library with poor types, a limit in the checker, or code that is correct in a way the checker cannot prove. Python gives you two tools for these cases: ignore comments and cast.
Ignore comments
An ignore comment suppresses the errors on one line. Each checker has its own form.
from typing import cast
def parse(value: object) -> str:
text = cast(str, value) # we know callers pass a str
return text.strip()
result: int = "abc" # type: ignore[assignment]
other: int = "abc" # pyright: ignore[reportAssignmentType]# type: ignore[assignment]is understood by mypy, and Pyright also honors it by default. The part in brackets is a mypy error code.# pyright: ignore[reportAssignmentType]is understood only by Pyright. The part in brackets is a Pyright rule name.
Always name the error code or rule. A bare # type: ignore hides every error on the line, including new ones that appear later. A targeted ignore hides only the error you meant to hide.
Using cast
cast(T, value) tells the checker to treat value as type T. It does nothing at runtime and returns value unchanged. In the example above, the checker treats text as str. If a caller passes an int, the program fails at .strip(), because nothing verified the claim.
flowchart TDUse cast when you know something the checker cannot. Prefer isinstance narrowing when a real check is possible, because it verifies the claim at runtime.
Keeping suppressions under control
Each suppression is a spot where the checker no longer protects you. These habits limit the damage:
- Add a short reason next to each ignore, such as
# type: ignore[attr-defined] # missing in old stubs. - Report unused ignores. In mypy, set
warn_unused_ignores = true, which strict mode already enables. In Pyright, setreportUnnecessaryTypeIgnoreComment = true. Both flag ignores that no longer suppress anything, so stale ones get removed. - Prefer the narrowest scope. Ignore one line, not a whole file.
[tool.mypy]
warn_unused_ignores = true
[tool.pyright]
reportUnnecessaryTypeIgnoreComment = trueCommon mistakes
- Using a bare
# type: ignoreand hiding unrelated errors. - Using
castto silence an error that points at a real bug. - Putting the ignore on the wrong line. For a multi-line statement, the comment usually belongs on the line where the checker reports the error.
Exercise
A function receives data: object and you write cast(dict[str, int], data). What happens at runtime if data is actually a list, and what would be a safer approach?
Show answer
Nothing happens at the cast itself, because it performs no check. The program fails later, when the code treats the list as a dict. A safer approach is if isinstance(data, dict): to narrow the type with a real runtime check, and handle the other case explicitly.