python / python/mypy

mypy sometimes emits fewer errors when running on multiple files at once

Open
#14,348 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Hello there,

I am witnessing (rare) cases where mypy emits fewer errors when running on multiple files at the same time than when running on each file individually.

More specifically, I noticed that mypy can fail to report an error when an import for a submodule is missing a one file but present in another file.

I failed to find anything relevant in the documentation or in the existing issues, so I thought I'd let you know. Below is my attempt at a minimal reproducible example.

To Reproduce

(Since multiple files are needed, here is an archive with the reproduction case: repro.zip)

Given a module mymodule with nested modules a and b, and this file foo.py:

import mymodule.a


def blah() -> None:
    mymodule.b.fn()


blah()

As expected, when called on foo.py, mypy emits an error:

$ mypy --cache-dir /dev/null foo.py
foo.py:5: error: Module has no attribute "b"
Found 1 error in 1 file (checked 1 source file)

yet if I add to mypy's command line a second file which does import mymodule.b, say bar.py:

import mymodule.b

then mypy does not emit the error any more, even though I would expect it to:

$ mypy --cache-dir /dev/null foo.py bar.py
Success: no issues found in 2 source files

Your Environment

I used mypy 0.991 on Ubuntu 22.04 with python 3.10.6; I can reproduce this behavior with master, as well as python 3.8, and older versions of mypy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Unpack repro.zip and run the two commands from the report with foo.py and bar.py to reproduce the differing diagnostics. Trace how imports of mymodule.a and mymodule.b are handled when files are checked together, then ensure the missing-attribute error remains reported in both invocations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.