Skip to content

Commit e455ecc

Browse files
committed
Namespace implementation
1 parent cdbc7db commit e455ecc

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

mypy/build.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ def verify_module(id: str, path: str) -> bool:
857857
"""Check that all packages containing id have a __init__ file."""
858858
if path.endswith(('__init__.py', '__init__.pyi')):
859859
path = dirname(path)
860+
return True
861+
860862
for i in range(id.count('.')):
861863
path = dirname(path)
862864
if not any(os.path.isfile(os.path.join(path, '__init__{}'.format(extension)))
@@ -1457,13 +1459,15 @@ def __init__(self,
14571459
self.order = State.order_counter
14581460
self.caller_state = caller_state
14591461
self.caller_line = caller_line
1462+
self.namespace = False
14601463
if caller_state:
14611464
self.import_context = caller_state.import_context[:]
14621465
self.import_context.append((caller_state.xpath, caller_line))
14631466
else:
14641467
self.import_context = []
14651468
self.id = id or '__main__'
14661469
self.options = manager.options.clone_for_module(self.id)
1470+
14671471
if not path and source is None:
14681472
assert id is not None
14691473
file_id = id
@@ -1516,6 +1520,13 @@ def __init__(self,
15161520
manager.missing_modules.add(id)
15171521
raise ModuleNotFound
15181522
else:
1523+
if ancestor_for:
1524+
self.child_modules = set()
1525+
self.ancestors = []
1526+
self.dependencies = []
1527+
self.suppressed = []
1528+
self.namespace = True
1529+
return
15191530
# If we can't find a root source it's always fatal.
15201531
# TODO: This might hide non-fatal errors from
15211532
# root sources processed earlier.
@@ -1725,6 +1736,9 @@ def parse_file(self) -> None:
17251736
# The file was already parsed (in __init__()).
17261737
return
17271738

1739+
if self.namespace:
1740+
return
1741+
17281742
manager = self.manager
17291743
modules = manager.modules
17301744
manager.log("Parsing %s (%s)" % (self.xpath, self.id))
@@ -1743,9 +1757,13 @@ def parse_file(self) -> None:
17431757
except (UnicodeDecodeError, DecodeError) as decodeerr:
17441758
raise CompileError([
17451759
"mypy: can't decode file '{}': {}".format(self.path, str(decodeerr))])
1746-
assert source is not None
1747-
self.tree = manager.parse_file(self.id, self.xpath, source,
1748-
self.ignore_all or self.options.ignore_errors)
1760+
1761+
if not self.namespace:
1762+
assert source is not None
1763+
self.tree = manager.parse_file(self.id, self.xpath, source,
1764+
self.ignore_all or self.options.ignore_errors)
1765+
else:
1766+
self.tree = MypyFile([], [])
17491767

17501768
modules[self.id] = self.tree
17511769

@@ -1807,13 +1825,20 @@ def parse_file(self) -> None:
18071825
self.check_blockers()
18081826

18091827
def semantic_analysis(self) -> None:
1828+
if self.namespace:
1829+
return
1830+
18101831
assert self.tree is not None, "Internal error: method must be called on parsed file only"
18111832
patches = [] # type: List[Callable[[], None]]
18121833
with self.wrap_context():
18131834
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
18141835
self.patches = patches
18151836

18161837
def semantic_analysis_pass_three(self) -> None:
1838+
if self.namespace:
1839+
self.patches = []
1840+
return
1841+
18171842
assert self.tree is not None, "Internal error: method must be called on parsed file only"
18181843
patches = [] # type: List[Callable[[], None]]
18191844
with self.wrap_context():
@@ -1828,6 +1853,9 @@ def semantic_analysis_apply_patches(self) -> None:
18281853
patch_func()
18291854

18301855
def type_check_first_pass(self) -> None:
1856+
if self.namespace:
1857+
return
1858+
18311859
assert self.tree is not None, "Internal error: method must be called on parsed file only"
18321860
manager = self.manager
18331861
if self.options.semantic_analysis_only:
@@ -1838,12 +1866,18 @@ def type_check_first_pass(self) -> None:
18381866
self.type_checker.check_first_pass()
18391867

18401868
def type_check_second_pass(self) -> bool:
1869+
if self.namespace:
1870+
return False
1871+
18411872
if self.options.semantic_analysis_only:
18421873
return False
18431874
with self.wrap_context():
18441875
return self.type_checker.check_second_pass()
18451876

18461877
def finish_passes(self) -> None:
1878+
if self.namespace:
1879+
return
1880+
18471881
assert self.tree is not None, "Internal error: method must be called on parsed file only"
18481882
manager = self.manager
18491883
if self.options.semantic_analysis_only:
@@ -1892,6 +1926,9 @@ def valid_references(self) -> Set[str]:
18921926
return valid_refs
18931927

18941928
def write_cache(self) -> None:
1929+
if self.namespace:
1930+
return
1931+
18951932
assert self.tree is not None, "Internal error: method must be called on parsed file only"
18961933
if not self.path or self.options.cache_dir == os.devnull:
18971934
return

0 commit comments

Comments
 (0)