Find the file mypy actually read#
mypy does not import your dependencies. It reads type information from files, and for a library without its own annotations those files are a separate stub package that someone else maintains. The mypy documentation at v2.4.1 describes these types-<library> packages and adds: "Note that many stub packages are not maintained by the original maintainers of the package." A stub package and its library are released on separate schedules, so a lockfile can hold a new library and an old stub.
This constructed module uses a keyword that tabulate 0.10.0 accepts:
from tabulate import tabulate
rows = [["disk", " 91%"], ["memory", " 40%"]]
print(tabulate(rows, headers=["resource", "used"], preserve_whitespace=True))
reveal_type(tabulate)
With mypy 2.4.1, tabulate 0.10.0 and types-tabulate 0.9.0.20241207 installed in one Python 3.11 environment, mypy rejects line 4:
$ mypy report.py
report.py:4: error: Unexpected keyword argument "preserve_whitespace" for "tabulate" [call-arg]
report.py:4: note: "tabulate" defined in "tabulate"
report.py:5: note: Revealed type is "def (tabular_data: ..., headers: ... =, tablefmt: ... =, ..., colalign: typing.Iterable[str | None] | None =, maxcolwidths: ... =, rowalign: ... =, maxheadercolwidths: ... =) -> str"
Found 1 error in 1 file (checked 1 source file)
reveal_type prints the signature mypy believes, and it ends at maxheadercolwidths with no preserve_whitespace. The note "tabulate" defined in "tabulate" names a module, not a file. --verbose names the file:
$ mypy --verbose --no-incremental report.py 2>&1 | grep -i "parsing.*tabulate"
LOG: Parsing .../site-packages/tabulate-stubs/__init__.pyi (tabulate)
LOG: Parsing .../site-packages/tabulate-stubs/version.pyi (tabulate.version)
mypy read tabulate-stubs/__init__.pyi. The installed tabulate package holds only __init__.py and no py.typed marker, so the stub package is mypy's only source of types for it.
Compare the stub with the runtime signature#
Ask the interpreter that will run the code what the function accepts, and which file it loaded:
$ python -c "import tabulate,inspect; print(tabulate.__file__, tabulate.__version__); print('preserve_whitespace' in inspect.signature(tabulate.tabulate).parameters)"
.../site-packages/tabulate/__init__.py 0.10.0
True
$ python report.py
resource used
---------- ------
disk 91%
memory 40%
The stub package says which library version it describes. Its METADATA.toml reads:
$ cat .../site-packages/tabulate-stubs/METADATA.toml
version = "0.9.*"
upstream_repository = "https://github.com/astanin/python-tabulate"
A stub written for 0.9 describing a 0.10 library explains the error: the keyword was added after the stub was written. The runtime check and the stub's own version line are the two facts that turn "mypy is wrong" from a guess into a finding.
Why mypy uses the stub and not the code#
mypy's import rules give a stub precedence over the code it describes. mypy builds its own search path from MYPYPATH, the mypy_path option, the directories of the files on the command line, installed packages marked as safe for type checking, and typeshed. Within one directory it prefers stubs: "if a Python file and a stub file are both present in the same directory on the search path, only the stub file is used." Across directories, "the one found in the earlier directory is used." A local .pyi file in a directory listed in mypy_path can therefore shadow a well-typed installed library, and --verbose is how you see it.
The environment matters too. mypy finds installed packages only in the environment it runs in: "This requires that you install the packages in the Python environment that you use to run mypy." A pre-commit hook or a CI job that installs mypy in its own environment can type-check against stubs that the application environment does not have, or against different versions of them. When the error appears in one place and not another, compare pip list in the environment that ran mypy, not the one that runs the code.
Fix the stub, not the call#
Once the evidence points at the stub, change the stub. Installing the stub release that matches the library clears the error with no change to the code:
$ pip install -q "types-tabulate==0.10.0.20261006"
$ cat .../site-packages/tabulate-stubs/METADATA.toml
version = "0.10.*"
upstream-repository = "https://github.com/astanin/python-tabulate"
$ mypy report.py
Success: no issues found in 1 source file
The same documentation already treats stub versions as something to pin: "If you pin to an older version of mypy (using requirements.txt, for example), it is recommended that you also pin the versions of all your stub package dependencies." Pin the stub next to the library it describes as well, so that an upgrade of one shows up in review beside the other. A # type: ignore[call-arg] on the call would also silence the error, but it stays after the stub is fixed and hides any later argument error on that line; the guide to stale suppression comments covers that risk.
If no matching stub release exists yet, a narrow local stub in a directory on mypy_path, or an upstream fix to typeshed, records the correction in one place instead of at every call site.
When the stub is right#
The same check can come out the other way. In PyYAML 6.0.4, yaml.load requires a Loader argument, and types-PyYAML 6.0.12.20261122 says so:
$ cat load.py
import yaml
config = yaml.load("replicas: 3")
print(config["replicas"])
$ mypy load.py
load.py:3: error: Missing positional argument "Loader" in call to "load" [call-arg]
$ python -c "import yaml,inspect; print(inspect.signature(yaml.load))"
(stream, Loader)
$ python load.py
Traceback (most recent call last):
...
TypeError: load() missing 1 required positional argument: 'Loader'
Here the runtime signature agrees with the stub, and the program fails at the same line mypy reported. Upgrading or removing the stub would hide a real crash. The fix belongs in the code, for example yaml.safe_load.
A second reversal hides in deployment. The tabulate check above ran against 0.10.0 on a development machine. If production installs tabulate 0.9.0 from an older lockfile, the stub for 0.9 is the accurate description of what runs there, and the call fails:
$ python report.py # environment with tabulate 0.9.0
Traceback (most recent call last):
...
TypeError: tabulate() got an unexpected keyword argument 'preserve_whitespace'
Check the runtime signature in the environment you ship, not only the one where mypy ran.
Decide from the two signatures#
| What mypy reports | Evidence to collect | Decision |
|---|---|---|
| Unexpected or missing argument on a third-party call | mypy --verbose file path, the stub's METADATA.toml version, inspect.signature in the deployed environment | If the runtime accepts the call and the stub targets an older version, upgrade or pin the stub |
Same error, runtime raises TypeError on the call | A run of the line, or inspect.signature showing the same parameters as the stub | Fix the code |
| Error only in pre-commit or one CI job | pip list in the environment that ran mypy | Align the stub and library versions in that environment |
Error from a .pyi file under mypy_path | The path in --verbose output | Update or delete the local stub |
| Library version differs between development and production | The lockfile used for deployment | Judge the call against the deployed version |
A type checker that disagrees with another analyzer about the same call is a related problem with its own evidence, covered in the guide to two analyzers that disagree. A runtime TypeError you want to keep fixed is worth turning into a test, as the guide to turning a review comment into a regression test describes.