Skip to content

Commit 8147b0c

Browse files
authored
Better errors for missing imports (#13636)
Fixes #13633 - Incompatible stubs aren't really a thing (that is visible to mypy at module find time) now that Python 2 support is dead. - Adjust some documentation wording to clarify use of `--python-executable`
1 parent 520b83e commit 8147b0c

File tree

7 files changed

+17
-15
lines changed

7 files changed

+17
-15
lines changed

docs/source/getting_started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ often suggest the name of the stub distribution:
436436

437437
.. code-block:: text
438438
439-
prog.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
439+
prog.py:1: error: Library stubs not installed for "yaml"
440440
prog.py:1: note: Hint: "python3 -m pip install types-PyYAML"
441441
...
442442

docs/source/installed_packages.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ stubs.)
5757
If you have installed typed packages in another Python installation or
5858
environment, mypy won't automatically find them. One option is to
5959
install another copy of those packages in the environment in which you
60-
use to run mypy. Alternatively, you can use the
60+
installed mypy. Alternatively, you can use the
6161
:option:`--python-executable <mypy --python-executable>` flag to point
62-
to the target Python executable, and mypy will find packages installed
63-
for that Python executable.
62+
to the Python executable for another environment, and mypy will find
63+
packages installed for that Python executable.
6464

6565
Note that mypy does not support some more advanced import features,
6666
such as zip imports and custom import hooks.

docs/source/running_mypy.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ the import. This can cause errors that look like the following:
138138
.. code-block:: text
139139
140140
main.py:1: error: Skipping analyzing 'django': module is installed, but missing library stubs or py.typed marker
141-
main.py:2: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
141+
main.py:2: error: Library stubs not installed for "requests"
142142
main.py:3: error: Cannot find implementation or library stub for module named "this_module_does_not_exist"
143143
144144
If you get any of these errors on an import, mypy will assume the type of that
@@ -243,7 +243,7 @@ the library, you will get a message like this:
243243

244244
.. code-block:: text
245245
246-
main.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
246+
main.py:1: error: Library stubs not installed for "yaml"
247247
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
248248
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
249249
@@ -276,6 +276,11 @@ check your code twice -- the first time to find the missing stubs, and
276276
the second time to type check your code properly after mypy has
277277
installed the stubs.
278278

279+
If you've already installed the relevant third-party libraries in an environment
280+
other than the one mypy is running in, you can use :option:`--python-executable
281+
<mypy --python-executable>` flag to point to the Python executable for that
282+
environment, and mypy will find packages installed for that Python executable.
283+
279284
.. _missing-type-hints-for-third-party-library:
280285

281286
Cannot find implementation or library stub

mypy/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,8 +2736,7 @@ def module_not_found(
27362736
else:
27372737
daemon = manager.options.fine_grained_incremental
27382738
msg, notes = reason.error_message_templates(daemon)
2739-
pyver = "%d.%d" % manager.options.python_version
2740-
errors.report(line, 0, msg.format(module=target, pyver=pyver), code=codes.IMPORT)
2739+
errors.report(line, 0, msg.format(module=target), code=codes.IMPORT)
27412740
top_level, second_level = get_top_two_prefixes(target)
27422741
if second_level in legacy_bundled_packages:
27432742
top_level = second_level

mypy/modulefinder.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ def error_message_templates(self, daemon: bool) -> tuple[str, list[str]]:
8989
)
9090
notes = [doc_link]
9191
elif self is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED:
92-
msg = (
93-
'Library stubs not installed for "{module}" (or incompatible with Python {pyver})'
94-
)
92+
msg = 'Library stubs not installed for "{module}"'
9593
notes = ['Hint: "python3 -m pip install {stub_dist}"']
9694
if not daemon:
9795
notes.append(

test-data/unit/fine-grained-modules.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,11 +2198,11 @@ import waitress
21982198
[file a.py.3]
21992199
import requests
22002200
[out]
2201-
a.py:1: error: Library stubs not installed for "waitress" (or incompatible with Python 3.7)
2201+
a.py:1: error: Library stubs not installed for "waitress"
22022202
a.py:1: note: Hint: "python3 -m pip install types-waitress"
22032203
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
22042204
==
22052205
==
2206-
a.py:1: error: Library stubs not installed for "requests" (or incompatible with Python 3.7)
2206+
a.py:1: error: Library stubs not installed for "requests"
22072207
a.py:1: note: Hint: "python3 -m pip install types-requests"
22082208
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

test-data/unit/pythoneval.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ from scribe import x
15621562
import maxminddb # Python 3 stubs available for maxminddb
15631563
import foobar_asdf
15641564
[out]
1565-
_testIgnoreImportIfNoPython3StubAvailable.py:4: error: Library stubs not installed for "maxminddb" (or incompatible with Python 3.7)
1565+
_testIgnoreImportIfNoPython3StubAvailable.py:4: error: Library stubs not installed for "maxminddb"
15661566
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: Hint: "python3 -m pip install types-maxminddb"
15671567
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: (or run "mypy --install-types" to install all missing stub packages)
15681568
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
@@ -1574,7 +1574,7 @@ import maxminddb
15741574
[out]
15751575
_testNoPython3StubAvailable.py:1: error: Cannot find implementation or library stub for module named "scribe"
15761576
_testNoPython3StubAvailable.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
1577-
_testNoPython3StubAvailable.py:3: error: Library stubs not installed for "maxminddb" (or incompatible with Python 3.7)
1577+
_testNoPython3StubAvailable.py:3: error: Library stubs not installed for "maxminddb"
15781578
_testNoPython3StubAvailable.py:3: note: Hint: "python3 -m pip install types-maxminddb"
15791579
_testNoPython3StubAvailable.py:3: note: (or run "mypy --install-types" to install all missing stub packages)
15801580

0 commit comments

Comments
 (0)