Skip to content

Commit 01c90a7

Browse files
author
hauntsaninja
committed
edits
1 parent 44fb9f5 commit 01c90a7

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

docs/source/command_line.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ imports.
110110
passed on the command line, the ``MYPYPATH`` environment variable,
111111
and the :confval:`mypy_path` config option.
112112

113-
This flag also affects how mypy determines fully qualified module names for
114-
files, modules and packages explicitly passed on the command line. See
115-
:ref:`Mapping file paths to modules <mapping-paths-to-modules>` for details.
113+
This flag affects how mypy finds modules and packages explicitly passed on
114+
the command line. It also affects how mypy determines fully qualified module
115+
names for files passed on the command line. See :ref:`Mapping file paths to
116+
modules <mapping-paths-to-modules>` for details.
116117

117118
.. option:: --explicit-package-bases
118119

docs/source/running_mypy.rst

+39-34
Original file line numberDiff line numberDiff line change
@@ -366,40 +366,45 @@ How mypy determines fully qualified module names depends on if the options
366366
:option:`--namespace-packages <mypy --namespace-packages>` and
367367
:option:`--explicit-package-bases <mypy --explicit-package-bases>` are set.
368368

369-
First, if :option:`--namespace-packages <mypy --namespace-packages>` is off,
370-
mypy will rely solely upon the presence of ``__init__.py[i]`` files to determine
371-
the fully qualified module name. That is, mypy will crawl up the directory tree
372-
for as long as it continues to find ``__init__.py`` (or ``__init__.pyi``) files.
373-
374-
Second, if :option:`--namespace-packages <mypy --namespace-packages>` is on, but
375-
:option:`--explicit-package-bases <mypy --explicit-package-bases>` is off, mypy
376-
will allow for the possibility that directories without ``__init__.py[i]`` are
377-
packages. Specifically, mypy will look at all parent directories of the file and
378-
use the location of the highest ``__init__.py[i]`` in the directory tree to
379-
determine the top-level package.
380-
381-
For example, say your directory tree consists solely of ``pkg/__init__.py`` and
382-
``pkg/a/b/c/d/mod.py``. When determining ``mod.py``'s fully qualified module
383-
name, mypy will look at ``pkg/__init__.py`` and conclude that the associated
384-
module name is ``pkg.a.b.c.d.mod``.
385-
386-
You'll notice that that method still relies on ``__init__.py``. If you can't put
387-
an ``__init__.py`` in your top-level package, but still wish to pass paths (as
388-
opposed to packages or modules using the ``-p`` or ``-m`` flags),
389-
:option:`--explicit-package-bases <mypy --explicit-package-bases>` provides a
390-
solution.
391-
392-
Third, with :option:`--explicit-package-bases <mypy --explicit-package-bases>`,
393-
mypy will locate the nearest parent directory that is a member of the
394-
``MYPYPATH`` environment variable, the :confval:`mypy_path` config or is the
395-
current working directory. mypy will then use the relative path to determine the
396-
fully qualified module name.
397-
398-
For example, say your directory tree consists solely of
399-
``src/namespace_pkg/mod.py``. If you run the command following command, mypy
400-
will correctly associate ``mod.py`` with ``namespace_pkg.mod``::
401-
402-
$ MYPYPATH=src mypy --namespace-packages --explicit-package-bases .
369+
1. If :option:`--namespace-packages <mypy --namespace-packages>` is off,
370+
mypy will rely solely upon the presence of ``__init__.py[i]`` files to
371+
determine the fully qualified module name. That is, mypy will crawl up the
372+
directory tree for as long as it continues to find ``__init__.py`` (or
373+
``__init__.pyi``) files.
374+
375+
For example, if your directory tree consists of ``pkg/subpkg/mod.py``, mypy
376+
would require ``pkg/__init__.py`` and ``pkg/subpkg/__init__.py`` to exist in
377+
order correctly associate ``mod.py`` with ``pkg.subpkg.mod``
378+
379+
2. If :option:`--namespace-packages <mypy --namespace-packages>` is on, but
380+
:option:`--explicit-package-bases <mypy --explicit-package-bases>` is off,
381+
mypy will allow for the possibility that directories without
382+
``__init__.py[i]`` are packages. Specifically, mypy will look at all parent
383+
directories of the file and use the location of the highest
384+
``__init__.py[i]`` in the directory tree to determine the top-level package.
385+
386+
For example, say your directory tree consists solely of ``pkg/__init__.py``
387+
and ``pkg/a/b/c/d/mod.py``. When determining ``mod.py``'s fully qualified
388+
module name, mypy will look at ``pkg/__init__.py`` and conclude that the
389+
associated module name is ``pkg.a.b.c.d.mod``.
390+
391+
3. You'll notice that the above case still relies on ``__init__.py``. If
392+
you can't put an ``__init__.py`` in your top-level package, but still wish to
393+
pass paths (as opposed to packages or modules using the ``-p`` or ``-m``
394+
flags), :option:`--explicit-package-bases <mypy --explicit-package-bases>`
395+
provides a solution.
396+
397+
With :option:`--explicit-package-bases <mypy --explicit-package-bases>`, mypy
398+
will locate the nearest parent directory that is a member of the ``MYPYPATH``
399+
environment variable, the :confval:`mypy_path` config or is the current
400+
working directory. mypy will then use the relative path to determine the
401+
fully qualified module name.
402+
403+
For example, say your directory tree consists solely of
404+
``src/namespace_pkg/mod.py``. If you run the command following command, mypy
405+
will correctly associate ``mod.py`` with ``namespace_pkg.mod``::
406+
407+
$ MYPYPATH=src mypy --namespace-packages --explicit-package-bases .
403408

404409
If you pass a file not ending in ``.py[i]``, the module name assumed is
405410
``__main__`` (matching the behavior of the Python interpreter), unless

0 commit comments

Comments
 (0)