Skip to content

Commit 3a27fa2

Browse files
committed
A bunch of cleanup
1 parent 870e8c9 commit 3a27fa2

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

mypy/build.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class BuildResult:
8080
manager: The build manager.
8181
files: Dictionary from module name to related AST node.
8282
types: Dictionary from parse tree node to its inferred type.
83+
used_cache: Whether the build took advantage of a cache
8384
errors: List of error messages.
8485
"""
8586

@@ -88,6 +89,7 @@ def __init__(self, manager: 'BuildManager', graph: Graph) -> None:
8889
self.graph = graph
8990
self.files = manager.modules
9091
self.types = manager.all_types # Non-empty for tests only or if dumping deps
92+
self.used_cache = manager.cache_enabled
9193
self.errors = [] # type: List[str] # Filled in by build if desired
9294

9395

@@ -569,6 +571,7 @@ class BuildManager:
569571
flush_errors: A function for processing errors after each SCC
570572
saved_cache: Dict with saved cache state for coarse-grained dmypy
571573
(read-write!)
574+
cache_enabled: Whether cache usage is enabled
572575
stats: Dict with various instrumentation numbers
573576
"""
574577

@@ -606,9 +609,13 @@ def __init__(self, data_dir: str,
606609
self.rechecked_modules = set() # type: Set[str]
607610
self.plugin = plugin
608611
self.flush_errors = flush_errors
612+
self.cache_enabled = options.incremental and options.cache_dir != os.devnull
609613
self.saved_cache = saved_cache if saved_cache is not None else {} # type: SavedCache
610614
self.stats = {} # type: Dict[str, Any] # Values are ints or floats
611615

616+
def use_fine_grained_cache(self) -> bool:
617+
return self.cache_enabled and self.options.use_fine_grained_cache
618+
612619
def maybe_swap_for_shadow_path(self, path: str) -> str:
613620
if (self.options.shadow_file and
614621
os.path.samefile(self.options.shadow_file[0], path)):
@@ -1156,7 +1163,7 @@ def validate_meta(meta: Optional[CacheMeta], id: str, path: Optional[str],
11561163
# changed since the cache was generated. We *don't* want to do a
11571164
# coarse-grained incremental rebuild, so we accept the cache
11581165
# metadata even if it doesn't match the source file.
1159-
if manager.options.use_fine_grained_cache:
1166+
if manager.use_fine_grained_cache():
11601167
manager.log('Using potentially stale metadata for {}'.format(id))
11611168
return meta
11621169

@@ -1654,7 +1661,7 @@ def __init__(self,
16541661
self.path = path
16551662
self.xpath = path or '<string>'
16561663
self.source = source
1657-
if path and source is None and self.options.incremental:
1664+
if path and source is None and self.manager.cache_enabled:
16581665
self.meta = find_cache_meta(self.id, path, manager)
16591666
# TODO: Get mtime if not cached.
16601667
if self.meta is not None:
@@ -1677,7 +1684,7 @@ def __init__(self,
16771684
# When doing a fine-grained cache load, pretend we only
16781685
# know about modules that have cache information and defer
16791686
# handling new modules until the fine-grained update.
1680-
if manager.options.use_fine_grained_cache:
1687+
if manager.use_fine_grained_cache():
16811688
manager.log("Deferring module to fine-grained update %s (%s)" % (path, id))
16821689
raise ModuleNotFound
16831690

@@ -1798,7 +1805,7 @@ def fix_cross_refs(self) -> None:
17981805
# cache load because we need to gracefully handle missing modules.
17991806
fixup_module_pass_one(self.tree, self.manager.modules,
18001807
self.manager.options.quick_and_dirty or
1801-
self.manager.options.use_fine_grained_cache)
1808+
self.manager.use_fine_grained_cache())
18021809

18031810
def calculate_mros(self) -> None:
18041811
assert self.tree is not None, "Internal error: method must be called on parsed file only"
@@ -2059,7 +2066,7 @@ def valid_references(self) -> Set[str]:
20592066

20602067
def write_cache(self) -> None:
20612068
assert self.tree is not None, "Internal error: method must be called on parsed file only"
2062-
if not self.path or self.options.cache_dir == os.devnull:
2069+
if not self.path or not self.manager.cache_enabled:
20632070
return
20642071
if self.manager.options.quick_and_dirty:
20652072
is_errors = self.manager.errors.is_errors_for_file(self.path)
@@ -2109,10 +2116,9 @@ def dispatch(sources: List[BuildSource], manager: BuildManager) -> Graph:
21092116
# In this case, we just turn the cache off entirely, so we don't need
21102117
# to worry about some files being loaded and some from cache and so
21112118
# that fine-grained mode never *writes* to the cache.
2112-
if manager.options.use_fine_grained_cache and len(graph) < 0.50 * len(sources):
2119+
if manager.use_fine_grained_cache() and len(graph) < 0.50 * len(sources):
21132120
manager.log("Redoing load_graph without cache because too much was missing")
2114-
manager.options.use_fine_grained_cache = False
2115-
manager.options.cache_dir = os.devnull
2121+
manager.cache_enabled = False
21162122
graph = load_graph(sources, manager)
21172123

21182124
t1 = time.time()
@@ -2133,7 +2139,10 @@ def dispatch(sources: List[BuildSource], manager: BuildManager) -> Graph:
21332139
if manager.options.dump_graph:
21342140
dump_graph(graph)
21352141
return graph
2136-
if manager.options.use_fine_grained_cache:
2142+
# If we are loading a fine-grained incremental mode cache, we
2143+
# don't want to do a real incremental reprocess of the graph---we
2144+
# just want to load in all of the cache information.
2145+
if manager.use_fine_grained_cache():
21372146
process_fine_grained_cache_graph(graph, manager)
21382147
else:
21392148
process_graph(graph, manager)

mypy/dmypy_server.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,6 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
280280
result = mypy.build.build(sources=sources,
281281
options=self.options,
282282
alt_lib_path=self.alt_lib_path)
283-
# build will clear use_fine_grained_cache if it needs to give up
284-
# on doing a cache load.
285-
cache_load_succeeded = self.options.use_fine_grained_cache
286283
except mypy.errors.CompileError as e:
287284
output = ''.join(s + '\n' for s in e.messages)
288285
if e.use_stdout:
@@ -300,7 +297,7 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
300297
# If we are using the fine-grained cache, build hasn't actually done
301298
# the typechecking on the updated files yet.
302299
# Run a fine-grained update starting from the cached data
303-
if cache_load_succeeded:
300+
if result.used_cache:
304301
# Pull times and hashes out of the saved_cache and stick them into
305302
# the fswatcher, so we pick up the changes.
306303
for state in self.fine_grained_manager.graph.values():

mypy/server/update.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,8 @@ def __init__(self,
171171
# Module that we haven't processed yet but that are known to be stale.
172172
self.stale = [] # type: List[Tuple[str, str]]
173173
# Disable the cache so that load_graph doesn't try going back to disk
174-
# for the cache. This is kind of a hack and it might be better to have
175-
# this directly reflected in load_graph's interface.
176-
self.options.cache_dir = os.devnull
177-
self.options.use_fine_grained_cache = False
174+
# for the cache.
175+
self.manager.cache_enabled = False
178176
manager.saved_cache = {}
179177
# Active triggers during the last update
180178
self.triggered = [] # type: List[str]

0 commit comments

Comments
 (0)