Sub topic 12
Turning on strict checking
This chapter assumes you can run mypy and Pyright, set them up in pyproject.toml, and read their error messages. It also assumes you know that unannotated code is treated as Any.
What strict mode changes
By default, both checkers go easy on code that lacks annotations. Strict mode removes that leniency. Functions without annotations become errors, and values of unknown type can no longer pass through unnoticed.
def add(a, b):
return a + b
def total(values: list) -> int:
return sum(values)In strict mode, mypy reports Function is missing a type annotation [no-untyped-def] for add. It reports Missing type parameters for generic type "list" [type-arg] for total. Pyright strict reports reportMissingParameterType and reportUnknownParameterType for add. It reports reportMissingTypeArgument for total. The fix is to write list[int] and to annotate a and b.
Enabling it in configuration
The strict = true setting from the configuration chapter is a shortcut for a bundle of flags.
[tool.mypy]
strict = true
[tool.pyright]
typeCheckingMode = "strict"The two tools define strict differently:
| Aspect | mypy | Pyright |
|---|---|---|
| Meaning of strict | A fixed set of flags, including `disallow_untyped_defs`, `disallow_any_generics`, `warn_return_any`, `warn_unused_ignores`, `no_implicit_reexport` and `strict_equality` | A ruleset that turns many `report...` rules into errors, including the unknown-type and unused-import rules |
| Less strict levels | One default level | `basic`, `standard` (the default), then `strict` |
| Per-file switch | `# mypy: strict` is not supported; use per-module overrides | `# pyright: strict` at the top of a file |
| Handling of `Any` | Allows implicit `Any` unless flags forbid it | Reports values of "unknown" type, which come from missing annotations |
mypy's flag list can change between releases, so check mypy --help for your version.
Strict for some code, lenient for the rest
You rarely have to choose all or nothing. Keep strict mode on globally and relax it for the modules that are not ready yet:
[tool.mypy]
strict = true
[[tool.mypy.overrides]]
module = "legacy.*"
disallow_untyped_defs = false
disallow_incomplete_defs = falseYou can also go the other way. Leave the global setting lenient and mark finished files with # pyright: strict. Pyright then holds only those files to the strict rules.
Common errors under strict mode
warn_return_anyfires when a function declared to returnstractually returns anAnyvalue. Annotate the source of theAny, or usecastif you are sure.warn_unused_ignoresflags# type: ignorecomments that no longer suppress anything. Delete them.no_implicit_reexportrejects importing a name from a module that only imported it itself. Import from the original module, or list the name in__all__.- Pyright's unknown-type rules can fire in a chain. One untyped function leaves each variable that uses its result unknown. Fix the first one and most of the rest disappear.
Exercise
A project has strict = true for mypy. The function def load(path): return open(path).read() produces an error. Which flag causes it, and what is a minimal fix?
Show answer
disallow_untyped_defs reports the missing annotations (no-untyped-def). Annotate the parameter and the return type: def load(path: str) -> str:.