Skip to content

Commit

Permalink
Better errors for missing imports (#13636)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
hauntsaninja authored Sep 9, 2022
1 parent 520b83e commit 8147b0c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ often suggest the name of the stub distribution:

.. code-block:: text
prog.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
prog.py:1: error: Library stubs not installed for "yaml"
prog.py:1: note: Hint: "python3 -m pip install types-PyYAML"
...
Expand Down
6 changes: 3 additions & 3 deletions docs/source/installed_packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ stubs.)
If you have installed typed packages in another Python installation or
environment, mypy won't automatically find them. One option is to
install another copy of those packages in the environment in which you
use to run mypy. Alternatively, you can use the
installed mypy. Alternatively, you can use the
:option:`--python-executable <mypy --python-executable>` flag to point
to the target Python executable, and mypy will find packages installed
for that Python executable.
to the Python executable for another environment, and mypy will find
packages installed for that Python executable.

Note that mypy does not support some more advanced import features,
such as zip imports and custom import hooks.
Expand Down
9 changes: 7 additions & 2 deletions docs/source/running_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ the import. This can cause errors that look like the following:
.. code-block:: text
main.py:1: error: Skipping analyzing 'django': module is installed, but missing library stubs or py.typed marker
main.py:2: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
main.py:2: error: Library stubs not installed for "requests"
main.py:3: error: Cannot find implementation or library stub for module named "this_module_does_not_exist"
If you get any of these errors on an import, mypy will assume the type of that
Expand Down Expand Up @@ -243,7 +243,7 @@ the library, you will get a message like this:

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

If you've already installed the relevant third-party libraries in an environment
other than the one mypy is running in, you can use :option:`--python-executable
<mypy --python-executable>` flag to point to the Python executable for that
environment, and mypy will find packages installed for that Python executable.

.. _missing-type-hints-for-third-party-library:

Cannot find implementation or library stub
Expand Down
3 changes: 1 addition & 2 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2736,8 +2736,7 @@ def module_not_found(
else:
daemon = manager.options.fine_grained_incremental
msg, notes = reason.error_message_templates(daemon)
pyver = "%d.%d" % manager.options.python_version
errors.report(line, 0, msg.format(module=target, pyver=pyver), code=codes.IMPORT)
errors.report(line, 0, msg.format(module=target), code=codes.IMPORT)
top_level, second_level = get_top_two_prefixes(target)
if second_level in legacy_bundled_packages:
top_level = second_level
Expand Down
4 changes: 1 addition & 3 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def error_message_templates(self, daemon: bool) -> tuple[str, list[str]]:
)
notes = [doc_link]
elif self is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED:
msg = (
'Library stubs not installed for "{module}" (or incompatible with Python {pyver})'
)
msg = 'Library stubs not installed for "{module}"'
notes = ['Hint: "python3 -m pip install {stub_dist}"']
if not daemon:
notes.append(
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fine-grained-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -2198,11 +2198,11 @@ import waitress
[file a.py.3]
import requests
[out]
a.py:1: error: Library stubs not installed for "waitress" (or incompatible with Python 3.7)
a.py:1: error: Library stubs not installed for "waitress"
a.py:1: note: Hint: "python3 -m pip install types-waitress"
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
==
==
a.py:1: error: Library stubs not installed for "requests" (or incompatible with Python 3.7)
a.py:1: error: Library stubs not installed for "requests"
a.py:1: note: Hint: "python3 -m pip install types-requests"
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
4 changes: 2 additions & 2 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ from scribe import x
import maxminddb # Python 3 stubs available for maxminddb
import foobar_asdf
[out]
_testIgnoreImportIfNoPython3StubAvailable.py:4: error: Library stubs not installed for "maxminddb" (or incompatible with Python 3.7)
_testIgnoreImportIfNoPython3StubAvailable.py:4: error: Library stubs not installed for "maxminddb"
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: Hint: "python3 -m pip install types-maxminddb"
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: (or run "mypy --install-types" to install all missing stub packages)
_testIgnoreImportIfNoPython3StubAvailable.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Expand All @@ -1574,7 +1574,7 @@ import maxminddb
[out]
_testNoPython3StubAvailable.py:1: error: Cannot find implementation or library stub for module named "scribe"
_testNoPython3StubAvailable.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
_testNoPython3StubAvailable.py:3: error: Library stubs not installed for "maxminddb" (or incompatible with Python 3.7)
_testNoPython3StubAvailable.py:3: error: Library stubs not installed for "maxminddb"
_testNoPython3StubAvailable.py:3: note: Hint: "python3 -m pip install types-maxminddb"
_testNoPython3StubAvailable.py:3: note: (or run "mypy --install-types" to install all missing stub packages)

Expand Down

0 comments on commit 8147b0c

Please sign in to comment.