Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe6fd1a

Browse files
authored
Namespaces refactor (python#5686)
In preparation of working on PEP 420 namespaces, here's some initial work. IT DOES NOT IMPLEMENT NAMESPACES YET. What it does do: - Move nearly 400 lines from build.py to a new file, `modulefinder.py`. This includes `SearchPaths`, `BuildSource`, `FindModuleCache`, `mypy-path`, `default_lib_path`, `get_site_packages` (now without underscore!), and `compute_search_paths`. - Slight refactor to `FindModuleCache` so that the `search_paths` are passed to the constructor instead of to `find_module`. - Removed `search_paths` and `python_executable` from the signature of `find_module` and `find_modules_recursive`, and also from the cache key (**this may be the most controversial change** -- it's not just a refactor). - Add a (non-functional) `--namespace-packages` flag to `main.py` and add new global config option `namespace_packages`. (These are not used yet.) I'm presenting this as a separate PR because it's a significant refactor without change of functionality -- everything I plan to do after this will mostly tweak the code in `modulefinder.py`. It seems fairer to reviewers to be able to review this massive code move without having to worry too much about "what else changed". (Note that this is part of my general plan to move functionality out of `build.py` -- that file is (still) way too bulky.)
1 parent 2e2fe30 commit fe6fd1a

17 files changed

Lines changed: 436 additions & 414 deletions

mypy/build.py

Lines changed: 6 additions & 391 deletions
Large diffs are not rendered by default.

mypy/find_sources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from typing import List, Sequence, Set, Tuple, Optional, Dict
66

7-
from mypy.build import BuildSource, PYTHON_EXTENSIONS
7+
from mypy.modulefinder import BuildSource, PYTHON_EXTENSIONS
88
from mypy.fscache import FileSystemCache
99
from mypy.options import Options
1010

mypy/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from mypy import defaults
1616
from mypy import experiments
1717
from mypy import util
18-
from mypy.build import BuildSource, BuildResult, SearchPaths
18+
from mypy.build import BuildResult
19+
from mypy.modulefinder import BuildSource, FindModuleCache, mypy_path, SearchPaths
1920
from mypy.find_sources import create_source_list, InvalidSourceList
2021
from mypy.fscache import FileSystemCache
2122
from mypy.errors import CompileError
@@ -453,6 +454,10 @@ def add_invertible_flag(flag: str,
453454
imports_group.add_argument(
454455
'--no-silence-site-packages', action='store_true',
455456
help="Do not silence errors in PEP 561 compliant installed packages")
457+
add_invertible_flag(
458+
'--namespace-packages', default=False,
459+
help="Support namespace packages (PEP 420, __init__.py-less)",
460+
group=imports_group)
456461

457462
platform_group = parser.add_argument_group(
458463
title='Platform configuration',
@@ -880,14 +885,14 @@ def add_invertible_flag(flag: str,
880885
# Set target.
881886
if special_opts.modules + special_opts.packages:
882887
options.build_type = BuildType.MODULE
883-
search_paths = SearchPaths((os.getcwd(),), tuple(build.mypy_path()), (), ())
888+
search_paths = SearchPaths((os.getcwd(),), tuple(mypy_path()), (), ())
884889
targets = []
885890
# TODO: use the same cache that the BuildManager will
886-
cache = build.FindModuleCache(fscache)
891+
cache = FindModuleCache(search_paths, fscache)
887892
for p in special_opts.packages:
888893
if os.sep in p or os.altsep and os.altsep in p:
889894
fail("Package name '{}' cannot have a slash in it.".format(p))
890-
p_targets = cache.find_modules_recursive(p, search_paths, options.python_executable)
895+
p_targets = cache.find_modules_recursive(p)
891896
if not p_targets:
892897
fail("Can't find package '{}'".format(p))
893898
targets.extend(p_targets)

mypy/modulefinder.py

Lines changed: 397 additions & 0 deletions
Large diffs are not rendered by default.

mypy/options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def __init__(self) -> None:
8080
self.follow_imports = 'normal' # normal|silent|skip|error
8181
# Whether to respect the follow_imports setting even for stub files.
8282
# Intended to be used for disabling specific stubs.
83-
self.follow_imports_for_stubs = False # type: bool
83+
self.follow_imports_for_stubs = False
84+
# PEP 420 namespace packages
85+
self.namespace_packages = False
8486

8587
# disallow_any options
8688
self.disallow_any_generics = False

mypy/stubgen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import mypy.traverser
5858
import mypy.util
5959
from mypy import defaults
60+
from mypy.modulefinder import FindModuleCache, SearchPaths
6061
from mypy.nodes import (
6162
Expression, IntExpr, UnaryExpr, StrExpr, BytesExpr, NameExpr, FloatExpr, MemberExpr, TupleExpr,
6263
ListExpr, ComparisonExpr, CallExpr, IndexExpr, EllipsisExpr,
@@ -165,9 +166,8 @@ def find_module_path_and_all(module: str, pyversion: Tuple[int, int],
165166
module_all = getattr(mod, '__all__', None)
166167
else:
167168
# Find module by going through search path.
168-
search_paths = mypy.build.SearchPaths(('.',) + tuple(search_path), (), (), ())
169-
module_path = mypy.build.FindModuleCache().find_module(module, search_paths,
170-
interpreter)
169+
search_paths = SearchPaths(('.',) + tuple(search_path), (), (), ())
170+
module_path = FindModuleCache(search_paths).find_module(module)
171171
if not module_path:
172172
raise SystemExit(
173173
"Can't find module '{}' (consider using --search-path)".format(module))

mypy/test/testcheck.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from typing import Dict, List, Set, Tuple
88

99
from mypy import build
10-
from mypy.build import BuildSource, Graph, SearchPaths
10+
from mypy.build import Graph
11+
from mypy.modulefinder import BuildSource, SearchPaths
1112
from mypy.test.config import test_temp_dir, test_data_prefix
1213
from mypy.test.data import DataDrivenTestCase, DataSuite, FileOperation, UpdateFile
1314
from mypy.test.helpers import (
@@ -286,8 +287,7 @@ def parse_module(self,
286287
out = []
287288
search_paths = SearchPaths((test_temp_dir,), (), (), ())
288289
for module_name in module_names.split(' '):
289-
path = build.FindModuleCache().find_module(module_name, search_paths,
290-
sys.executable)
290+
path = build.FindModuleCache(search_paths).find_module(module_name)
291291
assert path is not None, "Can't find ad hoc case file"
292292
with open(path) as f:
293293
program_text = f.read()

mypy/test/testdeps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import defaultdict
99

1010
from mypy import build, defaults
11-
from mypy.build import BuildSource
11+
from mypy.modulefinder import BuildSource
1212
from mypy.errors import CompileError
1313
from mypy.nodes import MypyFile, Expression
1414
from mypy.options import Options

mypy/test/testdiff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import List, Tuple, Dict, Optional
55

66
from mypy import build
7-
from mypy.build import BuildSource
7+
from mypy.modulefinder import BuildSource
88
from mypy.defaults import PYTHON3_VERSION
99
from mypy.errors import CompileError
1010
from mypy.nodes import MypyFile

mypy/test/testerrorstream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mypy import build
55
from mypy.test.helpers import assert_string_arrays_equal
66
from mypy.test.data import DataDrivenTestCase, DataSuite
7-
from mypy.build import BuildSource
7+
from mypy.modulefinder import BuildSource
88
from mypy.errors import CompileError
99
from mypy.options import Options
1010

0 commit comments

Comments
 (0)