Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about --follow-untyped-imports #18249

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ imports.

.. option:: --follow-untyped-imports

This flag makes mypy analyze imports without stubs or a py.typed marker.
This flag makes mypy analyze imports from installed packages even if
missing a :ref:`py.typed marker or stubs <installed-packages>`.

.. warning::

Note that analyzing all unannotated modules might result in issues
when analyzing code not designed to be type checked and may significantly
increase how long mypy takes to run.

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

Expand Down
12 changes: 9 additions & 3 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,18 @@ section of the command line docs.
:type: boolean
:default: False

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

If this option is used in a per-module section, the module name should
match the name of the *imported* module, not the module containing the
import statement. Note that scanning all unannotated modules might
significantly increase the runtime of your mypy calls.
import statement.
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

.. warning::

Note that analyzing all unannotated modules might result in issues
when analyzing code not designed to be type checked and may significantly
increase how long mypy takes to run.

.. confval:: follow_imports

Expand Down
35 changes: 29 additions & 6 deletions docs/source/running_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,25 @@ If you are getting this error, try to obtain type hints for the library you're u
to the library -- see our documentation on creating
:ref:`PEP 561 compliant packages <installed-packages>`.

4. Force mypy to analyze the library as best as it can (as if the library provided
a ``py.typed`` file), despite it likely missing any type annotations. In general,
the quality of type checking will be poor and mypy may have issues when
analyzing code not designed to be type checked.

You can do this via setting the
:option:`--follow-untyped-imports <mypy --follow-untyped-imports>`
command line flag or :confval:`follow_untyped_imports` config file option to True.
This option can be specified on a per-module basis as well::

# mypy.ini
[mypy-untyped_package.*]
follow_untyped_imports = True

# pyproject.toml
[[tool.mypy.overrides]]
module = ["untyped_package.*"]
follow_untyped_imports = true
Comment on lines +290 to +297
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use sphinx_inline_tabs for that. Opened #18262 for it.


If you are unable to find any existing type hints nor have time to write your
own, you can instead *suppress* the errors.

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

# mypy.ini
[mypy-foobar.*]
ignore_missing_imports = True

# pyproject.toml
[[tool.mypy.overrides]]
module = ["foobar.*"]
ignore_missing_imports = true

Note: this option is equivalent to adding a ``# type: ignore`` to every
import of ``foobar`` in your codebase. For more information, see the
documentation about configuring
Expand All @@ -311,22 +336,20 @@ not catch errors in its use.

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

# mypy.ini
[mypy]
disable_error_code = import-untyped

# pyproject.toml
[tool.mypy]
disable_error_code = ["import-untyped"]

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

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


Library stubs not installed
---------------------------
Expand Down
Loading