Skip to content

Document new source finding behaviour #9923

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

Merged
merged 4 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
edits
  • Loading branch information
hauntsaninja committed Jan 18, 2021
commit 01c90a77a8a7da0229a78fbe37d2ecb5998722ff
7 changes: 4 additions & 3 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ imports.
passed on the command line, the ``MYPYPATH`` environment variable,
and the :confval:`mypy_path` config option.

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

.. option:: --explicit-package-bases

Expand Down
73 changes: 39 additions & 34 deletions docs/source/running_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,40 +366,45 @@ How mypy determines fully qualified module names depends on if the options
:option:`--namespace-packages <mypy --namespace-packages>` and
:option:`--explicit-package-bases <mypy --explicit-package-bases>` are set.

First, if :option:`--namespace-packages <mypy --namespace-packages>` is off,
mypy will rely solely upon the presence of ``__init__.py[i]`` files to determine
the fully qualified module name. That is, mypy will crawl up the directory tree
for as long as it continues to find ``__init__.py`` (or ``__init__.pyi``) files.

Second, if :option:`--namespace-packages <mypy --namespace-packages>` is on, but
:option:`--explicit-package-bases <mypy --explicit-package-bases>` is off, mypy
will allow for the possibility that directories without ``__init__.py[i]`` are
packages. Specifically, mypy will look at all parent directories of the file and
use the location of the highest ``__init__.py[i]`` in the directory tree to
determine the top-level package.

For example, say your directory tree consists solely of ``pkg/__init__.py`` and
``pkg/a/b/c/d/mod.py``. When determining ``mod.py``'s fully qualified module
name, mypy will look at ``pkg/__init__.py`` and conclude that the associated
module name is ``pkg.a.b.c.d.mod``.

You'll notice that that method still relies on ``__init__.py``. If you can't put
an ``__init__.py`` in your top-level package, but still wish to pass paths (as
opposed to packages or modules using the ``-p`` or ``-m`` flags),
:option:`--explicit-package-bases <mypy --explicit-package-bases>` provides a
solution.

Third, with :option:`--explicit-package-bases <mypy --explicit-package-bases>`,
mypy will locate the nearest parent directory that is a member of the
``MYPYPATH`` environment variable, the :confval:`mypy_path` config or is the
current working directory. mypy will then use the relative path to determine the
fully qualified module name.

For example, say your directory tree consists solely of
``src/namespace_pkg/mod.py``. If you run the command following command, mypy
will correctly associate ``mod.py`` with ``namespace_pkg.mod``::

$ MYPYPATH=src mypy --namespace-packages --explicit-package-bases .
1. If :option:`--namespace-packages <mypy --namespace-packages>` is off,
mypy will rely solely upon the presence of ``__init__.py[i]`` files to
determine the fully qualified module name. That is, mypy will crawl up the
directory tree for as long as it continues to find ``__init__.py`` (or
``__init__.pyi``) files.

For example, if your directory tree consists of ``pkg/subpkg/mod.py``, mypy
would require ``pkg/__init__.py`` and ``pkg/subpkg/__init__.py`` to exist in
order correctly associate ``mod.py`` with ``pkg.subpkg.mod``

2. If :option:`--namespace-packages <mypy --namespace-packages>` is on, but
:option:`--explicit-package-bases <mypy --explicit-package-bases>` is off,
mypy will allow for the possibility that directories without
``__init__.py[i]`` are packages. Specifically, mypy will look at all parent
directories of the file and use the location of the highest
``__init__.py[i]`` in the directory tree to determine the top-level package.

For example, say your directory tree consists solely of ``pkg/__init__.py``
and ``pkg/a/b/c/d/mod.py``. When determining ``mod.py``'s fully qualified
module name, mypy will look at ``pkg/__init__.py`` and conclude that the
associated module name is ``pkg.a.b.c.d.mod``.

3. You'll notice that the above case still relies on ``__init__.py``. If
you can't put an ``__init__.py`` in your top-level package, but still wish to
pass paths (as opposed to packages or modules using the ``-p`` or ``-m``
flags), :option:`--explicit-package-bases <mypy --explicit-package-bases>`
provides a solution.

With :option:`--explicit-package-bases <mypy --explicit-package-bases>`, mypy
will locate the nearest parent directory that is a member of the ``MYPYPATH``
environment variable, the :confval:`mypy_path` config or is the current
working directory. mypy will then use the relative path to determine the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should you also mention module search path items from PEP 561 packages?

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Jan 19, 2021

Choose a reason for hiding this comment

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

No (although maybe I misunderstood the question), there isn't an interaction with PEP 561 packages when you pass files:

def get_explicit_package_bases(options: Options) -> Optional[List[str]]:

fully qualified module name.

For example, say your directory tree consists solely of
``src/namespace_pkg/mod.py``. If you run the command following command, mypy
will correctly associate ``mod.py`` with ``namespace_pkg.mod``::

$ MYPYPATH=src mypy --namespace-packages --explicit-package-bases .

If you pass a file not ending in ``.py[i]``, the module name assumed is
``__main__`` (matching the behavior of the Python interpreter), unless
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy