python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.57k stars 2.84k forks source link

Incorrect diagnostic on missing submodule #17846

Open jolaf opened 2 months ago

jolaf commented 2 months ago

Bug Report

When untyped module exists but submodule does not, mypy reports that submodule is installed.

To Reproduce

$ pip install fabric
import fabric
from fabric.not_exists import something
$ python3 test.py
Traceback (most recent call last):
  File "/media/sf_d/WWPass/git/spm/test.py", line 2, in <module>
    from fabric.not_exists import something
ModuleNotFoundError: No module named 'fabric.not_exists'

$ mypy test.py
test.py:1:1: error: Skipping analyzing "fabric": module is installed, but missing library stubs or py.typed marker  [import-untyped]
    import fabric
    ^
test.py:2:1: error: Skipping analyzing "fabric.not_exists": module is installed, but missing library stubs or py.typed marker  [import-untyped]
    from fabric.not_exists import something
    ^
test.py:2:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 2 errors in 1 file (checked 1 source file)

Expected Behavior Error in line 2 should be saying that fabric.not_exists does not exist.

Actual Behavior Error in line 2 says that fabric.not_exists is installed, which it not true and misleading.

Your Environment

$ mypy --version
mypy 1.11.2 (compiled: yes)

$ python3 --version
Python 3.12.3

$ pip list | grep fabric
fabric 3.2.2
brianschubert commented 2 months ago

Perhaps the wording "module or parent package" would be clearer?

jolaf commented 2 months ago

If parent package is untyped, then report should name the parent package, not the submodule.

Also, from my point of view, the most important thing to report is that submodule does not exist.

Reporting that parent package is not typed is much less important.

hauntsaninja commented 2 months ago

The behaviour makes sense in the context of extension modules, where currently "extension module missing a type stub" and "missing third party pure Python module" are indistinguishable to mypy.

Mypy could extend its import resolution to find extension modules, though