Skip to content

Commit d0ebee4

Browse files
hauntsaninjaJelleZijlstracdce8p
authored
Warn about --follow-untyped-imports (#18249)
Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Marc Mueller <[email protected]>
1 parent a53cf3d commit d0ebee4

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

docs/source/command_line.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ imports.
168168

169169
.. option:: --follow-untyped-imports
170170

171-
This flag makes mypy analyze imports without stubs or a py.typed marker.
171+
This flag makes mypy analyze imports from installed packages even if
172+
missing a :ref:`py.typed marker or stubs <installed-packages>`.
173+
174+
.. warning::
175+
176+
Note that analyzing all unannotated modules might result in issues
177+
when analyzing code not designed to be type checked and may significantly
178+
increase how long mypy takes to run.
172179

173180
.. option:: --follow-imports {normal,silent,skip,error}
174181

docs/source/config_file.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,18 @@ section of the command line docs.
320320
:type: boolean
321321
:default: False
322322

323-
Typechecks imports from modules that do not have stubs or a py.typed marker.
323+
Makes mypy analyze imports from installed packages even if missing a
324+
:ref:`py.typed marker or stubs <installed-packages>`.
324325

325326
If this option is used in a per-module section, the module name should
326327
match the name of the *imported* module, not the module containing the
327-
import statement. Note that scanning all unannotated modules might
328-
significantly increase the runtime of your mypy calls.
328+
import statement.
329+
330+
.. warning::
331+
332+
Note that analyzing all unannotated modules might result in issues
333+
when analyzing code not designed to be type checked and may significantly
334+
increase how long mypy takes to run.
329335

330336
.. confval:: follow_imports
331337

docs/source/running_mypy.rst

+29-6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ If you are getting this error, try to obtain type hints for the library you're u
277277
to the library -- see our documentation on creating
278278
:ref:`PEP 561 compliant packages <installed-packages>`.
279279

280+
4. Force mypy to analyze the library as best as it can (as if the library provided
281+
a ``py.typed`` file), despite it likely missing any type annotations. In general,
282+
the quality of type checking will be poor and mypy may have issues when
283+
analyzing code not designed to be type checked.
284+
285+
You can do this via setting the
286+
:option:`--follow-untyped-imports <mypy --follow-untyped-imports>`
287+
command line flag or :confval:`follow_untyped_imports` config file option to True.
288+
This option can be specified on a per-module basis as well::
289+
290+
# mypy.ini
291+
[mypy-untyped_package.*]
292+
follow_untyped_imports = True
293+
294+
# pyproject.toml
295+
[[tool.mypy.overrides]]
296+
module = ["untyped_package.*"]
297+
follow_untyped_imports = true
298+
280299
If you are unable to find any existing type hints nor have time to write your
281300
own, you can instead *suppress* the errors.
282301

@@ -295,9 +314,15 @@ not catch errors in its use.
295314
all import errors associated with that library and that library alone by
296315
adding the following section to your config file::
297316

317+
# mypy.ini
298318
[mypy-foobar.*]
299319
ignore_missing_imports = True
300320

321+
# pyproject.toml
322+
[[tool.mypy.overrides]]
323+
module = ["foobar.*"]
324+
ignore_missing_imports = true
325+
301326
Note: this option is equivalent to adding a ``# type: ignore`` to every
302327
import of ``foobar`` in your codebase. For more information, see the
303328
documentation about configuring
@@ -311,22 +336,20 @@ not catch errors in its use.
311336

312337
You can also set :confval:`disable_error_code`, like so::
313338

339+
# mypy.ini
314340
[mypy]
315341
disable_error_code = import-untyped
316342

343+
# pyproject.toml
344+
[tool.mypy]
345+
disable_error_code = ["import-untyped"]
317346

318347
You can also set the :option:`--ignore-missing-imports <mypy --ignore-missing-imports>`
319348
command line flag or set the :confval:`ignore_missing_imports` config file
320349
option to True in the *global* section of your mypy config file. We
321350
recommend avoiding ``--ignore-missing-imports`` if possible: it's equivalent
322351
to adding a ``# type: ignore`` to all unresolved imports in your codebase.
323352

324-
4. To make mypy typecheck imports from modules without stubs or a py.typed
325-
marker, you can set the :option:`--follow-untyped-imports <mypy --follow-untyped-imports>`
326-
command line flag or set the :confval:`follow_untyped_imports` config file option to True,
327-
either in the global section of your mypy config file, or individually on a
328-
per-module basis.
329-
330353

331354
Library stubs not installed
332355
---------------------------

0 commit comments

Comments
 (0)