diff --git a/docs/source/running_mypy.rst b/docs/source/running_mypy.rst index d4061fcdc103..603264b8b6d9 100644 --- a/docs/source/running_mypy.rst +++ b/docs/source/running_mypy.rst @@ -443,9 +443,9 @@ How mypy determines fully qualified module names depends on if the options With :option:`--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. + environment variable, the :confval:`mypy_path` config or, if neither of those + configuration options are specified, then 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 diff --git a/mypy/find_sources.py b/mypy/find_sources.py index a44648f261ed..80ec2b1deaca 100644 --- a/mypy/find_sources.py +++ b/mypy/find_sources.py @@ -73,7 +73,7 @@ def normalise_package_base(root: str) -> str: def get_explicit_package_bases(options: Options) -> Optional[List[str]]: """Returns explicit package bases to use if the option is enabled, or None if disabled. - We currently use MYPYPATH and the current directory as the package bases. In the future, + We currently use MYPYPATH or the current directory as the package bases. In the future, when --namespace-packages is the default could also use the values passed with the --package-root flag, see #9632. @@ -82,7 +82,7 @@ def get_explicit_package_bases(options: Options) -> Optional[List[str]]: """ if not options.explicit_package_bases: return None - roots = mypy_path() + options.mypy_path + [os.getcwd()] + roots = mypy_path() + options.mypy_path or [os.getcwd()] return [normalise_package_base(root) for root in roots] diff --git a/mypy/main.py b/mypy/main.py index 90134de9c438..9797784553c8 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -162,7 +162,8 @@ def flush_errors(new_messages: List[str], serious: bool) -> None: try: # Keep a dummy reference (res) for memory profiling afterwards, as otherwise # the result could be freed. - res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr) + alt_lib_path = "" if options.explicit_package_bases else None + res = build.build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr) except CompileError as e: blockers = True if not e.use_stdout: @@ -862,7 +863,7 @@ def add_invertible_flag(flag: str, "mypy.readthedocs.io/en/stable/running_mypy.html#running-mypy") add_invertible_flag( '--explicit-package-bases', default=False, - help="Use current directory and MYPYPATH to determine module names of files passed", + help="Use current directory or MYPYPATH to determine module names of files passed", group=code_group) code_group.add_argument( "--exclude", diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index df5c556409a3..fe3017ff4a92 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -698,7 +698,7 @@ def compute_search_paths(sources: List[BuildSource], # alt_lib_path is used by some tests to bypass the normal lib_path mechanics. # If we don't have one, grab directories of source files. python_path: List[str] = [] - if not alt_lib_path: + if alt_lib_path is None: for source in sources: # Include directory of the program file in the module search path. if source.base_dir: diff --git a/mypy/options.py b/mypy/options.py index 310fff5dbfe5..904779625246 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -93,7 +93,7 @@ def __init__(self) -> None: # multiple directories. This flag affects both import discovery and the association of # input files/modules/packages to the relevant file and fully qualified module name. self.namespace_packages = False - # Use current directory and MYPYPATH to determine fully qualified module names of files + # Use current directory or MYPYPATH to determine fully qualified module names of files # passed by automatically considering their subdirectories as packages. This is only # relevant if namespace packages are enabled, since otherwise examining __init__.py's is # sufficient to determine module names for files. As a possible alternative, add a single