Skip to content

Commit

Permalink
Improve missing import message (#7698)
Browse files Browse the repository at this point in the history
It can be difficult to understand the missing module error message at times.

Changes

>  Cannot find module named '{}'

to

>  Cannot find implementation or library stub for module named '{}'

This improves #4542.
  • Loading branch information
Lewis Cowles authored and JukkaL committed Oct 28, 2019
1 parent fdcbb74 commit 70f2857
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 153 deletions.
2 changes: 1 addition & 1 deletion docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ Example:

.. code-block:: python
# Error: Cannot find module named 'acme' [import]
# Error: Cannot find implementation or library stub for module named 'acme' [import]
import acme
See :ref:`ignore-missing-imports` for how to work around these errors.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/existing_code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ find or that don't have stub files:

.. code-block:: text
core/config.py:7: error: Cannot find module named 'frobnicate'
core/model.py:9: error: Cannot find module named 'acme'
core/config.py:7: error: Cannot find implementation or library stub for module named 'frobnicate'
core/model.py:9: error: Cannot find implementation or library stub for module named 'acme'
...
This is normal, and you can easily ignore these errors. For example,
Expand Down
2 changes: 1 addition & 1 deletion docs/source/running_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ This can cause a lot of errors that look like the following::

main.py:1: error: No library stub file for standard library module 'antigravity'
main.py:2: error: No library stub file for module 'flask'
main.py:3: error: Cannot find module named 'this_module_does_not_exist'
main.py:3: error: Cannot find implementation or library stub for module named 'this_module_does_not_exist'

There are several different things you can try doing, depending on the exact
nature of the module.
Expand Down
7 changes: 6 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,12 @@ def module_not_found(manager: BuildManager, line: int, caller_state: State,
errors.report(line, 0, stub_msg, severity='note', only_once=True, code=codes.IMPORT)
else:
note = "See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports"
errors.report(line, 0, "Cannot find module named '{}'".format(target), code=codes.IMPORT)
errors.report(
line,
0,
"Cannot find implementation or library stub for module named '{}'".format(target),
code=codes.IMPORT
)
errors.report(line, 0, note, severity='note', only_once=True, code=codes.IMPORT)
errors.set_import_context(save_import_context)

Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testpep561.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SimpleMsg(Enum):


class NamespaceMsg(Enum):
cfm_beta = ("{tempfile}:4: error: Cannot find module named "
cfm_beta = ("{tempfile}:4: error: Cannot find implementation or library stub for module named "
"'typedpkg_ns.ns.dne'")
help_note = ('{tempfile}:4: note: See https://mypy.readthedocs.io/en/latest/'
'running_mypy.html#missing-imports')
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,12 @@ if int() is str(): # E: Non-overlapping identity check (left operand type: "int
[case testErrorCodeMissingModule]
from defusedxml import xyz # E: No library stub file for module 'defusedxml' [import] \
# N: (Stub files are from https://github.com/python/typeshed)
from nonexistent import foobar # E: Cannot find module named 'nonexistent' [import] \
from nonexistent import foobar # E: Cannot find implementation or library stub for module named 'nonexistent' [import] \
# N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
import nonexistent2 # E: Cannot find module named 'nonexistent2' [import]
from nonexistent3 import * # E: Cannot find module named 'nonexistent3' [import]
import nonexistent2 # E: Cannot find implementation or library stub for module named 'nonexistent2' [import]
from nonexistent3 import * # E: Cannot find implementation or library stub for module named 'nonexistent3' [import]
from pkg import bad # E: Module 'pkg' has no attribute 'bad' [attr-defined]
from pkg.bad2 import bad3 # E: Cannot find module named 'pkg.bad2' [import]
from pkg.bad2 import bad3 # E: Cannot find implementation or library stub for module named 'pkg.bad2' [import]
[file pkg/__init__.py]

[case testErrorCodeAlreadyDefined]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ main:2: note: (Using --follow-imports=error, module not passed on command line)
[case testIgnoreMissingImportsFalse]
from mod import x
[out]
main:1: error: Cannot find module named 'mod'
main:1: error: Cannot find implementation or library stub for module named 'mod'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testIgnoreMissingImportsTrue]
Expand Down Expand Up @@ -610,7 +610,7 @@ from missing import MyType
def f(x: MyType) -> None:
pass
[out]
main:2: error: Cannot find module named 'missing'
main:2: error: Cannot find implementation or library stub for module named 'missing'
main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: Argument 1 to "f" becomes "Any" due to an unfollowed import

Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ x = 1
[rechecked n]
[stale]
[out2]
tmp/n.py:1: error: Cannot find module named 'm'
tmp/n.py:1: error: Cannot find implementation or library stub for module named 'm'
tmp/n.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testDeleteFileWithinCycle]
Expand Down Expand Up @@ -3382,7 +3382,7 @@ import a
[out1]

[out2]
main:2: error: Cannot find module named 'a'
main:2: error: Cannot find implementation or library stub for module named 'a'
main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testIncrementalInheritanceAddAnnotation]
Expand Down Expand Up @@ -3525,7 +3525,7 @@ def f() -> None: pass
def f(x: int) -> None: pass
[out]
[out2]
main:1: error: Cannot find module named 'p.q'
main:1: error: Cannot find implementation or library stub for module named 'p.q'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
[out3]
main:2: error: Too few arguments for "f"
Expand Down Expand Up @@ -4140,10 +4140,10 @@ def __getattr__(attr: str) -> Any: ...
# empty
[builtins fixtures/module.pyi]
[out]
tmp/c.py:1: error: Cannot find module named 'a.b.c'
tmp/c.py:1: error: Cannot find implementation or library stub for module named 'a.b.c'
tmp/c.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
[out2]
tmp/c.py:1: error: Cannot find module named 'a.b.c'
tmp/c.py:1: error: Cannot find implementation or library stub for module named 'a.b.c'
tmp/c.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testAddedMissingStubs]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func(f) # E: Argument 1 to "func" has incompatible type "Union[Literal['foo'],
[case testLiteralDisallowAny]
from typing import Any
from typing_extensions import Literal
from missing_module import BadAlias # E: Cannot find module named 'missing_module' \
from missing_module import BadAlias # E: Cannot find implementation or library stub for module named 'missing_module' \
# N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

a: Literal[Any] # E: Parameter 1 of Literal[...] cannot be of type "Any"
Expand Down
54 changes: 27 additions & 27 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ else:
import nonexistent
None + ''
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")

Expand All @@ -220,7 +220,7 @@ m.x = ''
[file m.py]
x = 1
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Expand All @@ -233,7 +233,7 @@ m.x = ''
[file m.py]
x = 1
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Expand All @@ -242,32 +242,32 @@ main:4: error: Incompatible types in assignment (expression has type "str", vari
import nonexistent, another
None + ''
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:1: error: Cannot find module named 'another'
main:1: error: Cannot find implementation or library stub for module named 'another'
main:2: error: Unsupported left operand type for + ("None")

[case testTypeCheckWithUnknownModule5]
import nonexistent as x
None + ''
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")

[case testTypeCheckWithUnknownModuleUsingFromImport]
from nonexistent import x
None + ''
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")

[case testTypeCheckWithUnknownModuleUsingImportStar]
from nonexistent import *
None + ''
[out]
main:1: error: Cannot find module named 'nonexistent'
main:1: error: Cannot find implementation or library stub for module named 'nonexistent'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")

Expand All @@ -276,24 +276,24 @@ import xyz
xyz.foo()
xyz()
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testAccessingUnknownModule2]
import xyz, bar
xyz.foo()
bar()
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:1: error: Cannot find module named 'bar'
main:1: error: Cannot find implementation or library stub for module named 'bar'

[case testAccessingUnknownModule3]
import xyz as z
xyz.foo()
z()
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Name 'xyz' is not defined

Expand All @@ -302,14 +302,14 @@ from xyz import y, z
y.foo()
z()
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testAccessingNameImportedFromUnknownModule2]
from xyz import *
y
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Name 'y' is not defined

Expand All @@ -318,14 +318,14 @@ from xyz import y as z
y
z
[out]
main:1: error: Cannot find module named 'xyz'
main:1: error: Cannot find implementation or library stub for module named 'xyz'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Name 'y' is not defined

[case testUnknownModuleRedefinition]
# Error messages differ with the new analyzer

import xab # E: Cannot find module named 'xab' # N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
import xab # E: Cannot find implementation or library stub for module named 'xab' # N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
def xab(): pass # E: Name 'xab' already defined (possibly by an import)

[case testAccessingUnknownModuleFromOtherModule]
Expand All @@ -336,7 +336,7 @@ x.z
import nonexistent
[builtins fixtures/module.pyi]
[out]
tmp/x.py:1: error: Cannot find module named 'nonexistent'
tmp/x.py:1: error: Cannot find implementation or library stub for module named 'nonexistent'
tmp/x.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:3: error: Module has no attribute "z"

Expand All @@ -346,7 +346,7 @@ def f():
def foobar(): pass
foobar('')
[out]
main:2: error: Cannot find module named 'foobar'
main:2: error: Cannot find implementation or library stub for module named 'foobar'
main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: Too many arguments for "foobar"

Expand All @@ -356,7 +356,7 @@ def f():
def x(): pass
x('')
[out]
main:2: error: Cannot find module named 'foobar'
main:2: error: Cannot find implementation or library stub for module named 'foobar'
main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: Too many arguments for "x"

Expand Down Expand Up @@ -2177,8 +2177,8 @@ import c

[out]
-- TODO: it would be better for this to be in the other order
tmp/b.py:1: error: Cannot find module named 'c'
main:1: error: Cannot find module named 'c'
tmp/b.py:1: error: Cannot find implementation or library stub for module named 'c'
main:1: error: Cannot find implementation or library stub for module named 'c'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testIndirectFromImportWithinCycle1]
Expand Down Expand Up @@ -2312,7 +2312,7 @@ from typing import Any
def __getattr__(attr: str) -> Any: ...
[builtins fixtures/module.pyi]
[out]
main:1: error: Cannot find module named 'a.b'
main:1: error: Cannot find implementation or library stub for module named 'a.b'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testModuleGetattrInit4]
Expand Down Expand Up @@ -2357,12 +2357,12 @@ def __getattr__(attr: str) -> Any: ...
# empty (i.e. complete subpackage)
[builtins fixtures/module.pyi]
[out]
main:1: error: Cannot find module named 'a.b.c.d'
main:1: error: Cannot find implementation or library stub for module named 'a.b.c.d'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:1: error: Cannot find module named 'a.b.c'
main:1: error: Cannot find implementation or library stub for module named 'a.b.c'

[case testModuleGetattrInit8a]
import a.b.c # E: Cannot find module named 'a.b.c' # N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
import a.b.c # E: Cannot find implementation or library stub for module named 'a.b.c' # N: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
import a.d # OK
[file a/__init__.pyi]
from typing import Any
Expand All @@ -2388,7 +2388,7 @@ def __getattr__(attr: str) -> Any: ...
ignore_missing_imports = True
[builtins fixtures/module.pyi]
[out]
main:3: error: Cannot find module named 'a.b.d'
main:3: error: Cannot find implementation or library stub for module named 'a.b.d'
main:3: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testIndirectFromImportWithinCycleUsedAsBaseClass-skip]
Expand Down Expand Up @@ -2619,7 +2619,7 @@ from foo.bar import x
[file foo/bar.py]
x = 0
[out]
main:1: error: Cannot find module named 'foo.bar'
main:1: error: Cannot find implementation or library stub for module named 'foo.bar'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

[case testNamespacePackage]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-python2.test
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ main:8: error: Argument 2 to "f" has incompatible type "int"; expected "Tuple[in
import asyncio
import Bastion
[out]
main:1: error: Cannot find module named 'asyncio'
main:1: error: Cannot find implementation or library stub for module named 'asyncio'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: No library stub file for standard library module 'Bastion'
main:2: note: (Stub files are from https://github.com/python/typeshed)
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-semanal-error.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ m.foo()
m.x = m.y
1() # E
[out]
main:1: error: Cannot find module named 'm'
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: "int" not callable

Expand All @@ -28,7 +28,7 @@ x.foo()
x.a = x.b
1() # E
[out]
main:1: error: Cannot find module named 'm'
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:4: error: "int" not callable

Expand All @@ -37,7 +37,7 @@ from m import * # E
x # E
1() # E
[out]
main:1: error: Cannot find module named 'm'
main:1: error: Cannot find implementation or library stub for module named 'm'
main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
main:2: error: Name 'x' is not defined
main:3: error: "int" not callable
Expand Down
Loading

0 comments on commit 70f2857

Please sign in to comment.