Skip to content

Commit b75d935

Browse files
authored
Make error more self-explanatory when missing stubs or py.typed (#11262)
Also re-order docs and make them more greppable Helps with e.g. complaints in #9944 Co-authored-by: hauntsaninja <>
1 parent edd7ce9 commit b75d935

File tree

2 files changed

+59
-55
lines changed

2 files changed

+59
-55
lines changed

docs/source/running_mypy.rst

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ Missing imports
133133
***************
134134

135135
When you import a module, mypy may report that it is unable to follow
136-
the import.
137-
138-
This can cause errors that look like the following:
136+
the import. This can cause errors that look like the following:
139137

140138
.. code-block:: text
141139
142-
main.py:1: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
143-
main.py:2: error: Skipping analyzing 'django': found module but no type hints or library stubs
140+
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)
144142
main.py:3: error: Cannot find implementation or library stub for module named "this_module_does_not_exist"
145143
146144
If you get any of these errors on an import, mypy will assume the type of that
@@ -155,55 +153,14 @@ attribute of the module will automatically succeed:
155153
# But this type checks, and x will have type 'Any'
156154
x = does_not_exist.foobar()
157155
158-
The next sections describe what each error means and recommended next steps.
159-
160-
Library stubs not installed
161-
---------------------------
162-
163-
If mypy can't find stubs for a third-party library, and it knows that stubs exist for
164-
the library, you will get a message like this:
156+
The next sections describe what each of these errors means and recommended next steps; scroll to
157+
the section that matches your error.
165158

166-
.. code-block:: text
167159

168-
main.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
169-
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
170-
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
160+
Missing library stubs or py.typed marker
161+
----------------------------------------
171162

172-
You can resolve the issue by running the suggested pip command or
173-
commands. Alternatively, you can use :option:`--install-types <mypy
174-
--install-types>` to install all known missing stubs:
175-
176-
.. code-block:: text
177-
178-
mypy --install-types
179-
180-
This installs any stub packages that were suggested in the previous
181-
mypy run. You can also use your normal mypy command line with the
182-
extra :option:`--install-types <mypy --install-types>` option to
183-
install missing stubs at the end of the run (if any were found).
184-
185-
Use :option:`--install-types <mypy --install-types>` with
186-
:option:`--non-interactive <mypy --non-interactive>` to install all suggested
187-
stub packages without asking for confirmation, *and* type check your
188-
code, in a single command:
189-
190-
.. code-block:: text
191-
192-
mypy --install-types --non-interactive src/
193-
194-
This can be useful in Continuous Integration jobs if you'd prefer not
195-
to manage stub packages manually. This is somewhat slower than
196-
explicitly installing stubs before running mypy, since it may type
197-
check your code twice -- the first time to find the missing stubs, and
198-
the second time to type check your code properly after mypy has
199-
installed the stubs.
200-
201-
.. _missing-type-hints-for-third-party-library:
202-
203-
Missing type hints for third party library
204-
------------------------------------------
205-
206-
If you are getting a "Skipping analyzing X: found module but no type hints or library stubs",
163+
If you are getting a ``Skipping analyzing X: module is installed, but missing library stubs or py.typed marker``,
207164
error, this means mypy was able to find the module you were importing, but no
208165
corresponding type hints.
209166

@@ -277,10 +234,54 @@ will continue to be of type ``Any``.
277234
We recommend using this approach only as a last resort: it's equivalent
278235
to adding a ``# type: ignore`` to all unresolved imports in your codebase.
279236

280-
Unable to find module
281-
---------------------
282237

283-
If you are getting a "Cannot find implementation or library stub for module"
238+
Library stubs not installed
239+
---------------------------
240+
241+
If mypy can't find stubs for a third-party library, and it knows that stubs exist for
242+
the library, you will get a message like this:
243+
244+
.. code-block:: text
245+
246+
main.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
247+
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
248+
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
249+
250+
You can resolve the issue by running the suggested pip command or
251+
commands. Alternatively, you can use :option:`--install-types <mypy
252+
--install-types>` to install all known missing stubs:
253+
254+
.. code-block:: text
255+
256+
mypy --install-types
257+
258+
This installs any stub packages that were suggested in the previous
259+
mypy run. You can also use your normal mypy command line with the
260+
extra :option:`--install-types <mypy --install-types>` option to
261+
install missing stubs at the end of the run (if any were found).
262+
263+
Use :option:`--install-types <mypy --install-types>` with
264+
:option:`--non-interactive <mypy --non-interactive>` to install all suggested
265+
stub packages without asking for confirmation, *and* type check your
266+
code, in a single command:
267+
268+
.. code-block:: text
269+
270+
mypy --install-types --non-interactive src/
271+
272+
This can be useful in Continuous Integration jobs if you'd prefer not
273+
to manage stub packages manually. This is somewhat slower than
274+
explicitly installing stubs before running mypy, since it may type
275+
check your code twice -- the first time to find the missing stubs, and
276+
the second time to type check your code properly after mypy has
277+
installed the stubs.
278+
279+
.. _missing-type-hints-for-third-party-library:
280+
281+
Cannot find implementation or library stub
282+
------------------------------------------
283+
284+
If you are getting a ``Cannot find implementation or library stub for module``
284285
error, this means mypy was not able to find the module you are trying to
285286
import, whether it comes bundled with type hints or not. If you are getting
286287
this error, try:

mypy/modulefinder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def error_message_templates(self, daemon: bool) -> Tuple[str, List[str]]:
7070
notes = ["You may be running mypy in a subpackage, "
7171
"mypy should be run on the package root"]
7272
elif self is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
73-
msg = 'Skipping analyzing "{module}": found module but no type hints or library stubs'
73+
msg = (
74+
'Skipping analyzing "{module}": module is installed, but missing library stubs '
75+
'or py.typed marker'
76+
)
7477
notes = [doc_link]
7578
elif self is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED:
7679
msg = (

0 commit comments

Comments
 (0)