update.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. """Update build by processing changes using fine-grained dependencies.
  2. Use fine-grained dependencies to update targets in other modules that
  3. may be affected by externally-visible changes in the changed modules.
  4. This forms the core of the fine-grained incremental daemon mode. This
  5. module is not used at all by the 'classic' (non-daemon) incremental
  6. mode.
  7. Here is some motivation for this mode:
  8. * By keeping program state in memory between incremental runs, we
  9. only have to process changed modules, not their dependencies. The
  10. classic incremental mode has to deserialize the symbol tables of
  11. all dependencies of changed modules, which can be slow for large
  12. programs.
  13. * Fine-grained dependencies allow processing only the relevant parts
  14. of modules indirectly affected by a change. Say, if only one function
  15. in a large module is affected by a change in another module, only this
  16. function is processed. The classic incremental mode always processes
  17. an entire file as a unit, which is typically much slower.
  18. * It's possible to independently process individual modules within an
  19. import cycle (SCC). Small incremental changes can be fast independent
  20. of the size of the related SCC. In classic incremental mode, any change
  21. within a SCC requires the entire SCC to be processed, which can slow
  22. things down considerably.
  23. Some terms:
  24. * A *target* is a function/method definition or the top level of a module.
  25. We refer to targets using their fully qualified name (e.g.
  26. 'mod.Cls.method'). Targets are the smallest units of processing during
  27. fine-grained incremental checking.
  28. * A *trigger* represents the properties of a part of a program, and it
  29. gets triggered/fired when these properties change. For example,
  30. '<mod.func>' refers to a module-level function. It gets triggered if
  31. the signature of the function changes, or if the function is removed,
  32. for example.
  33. Some program state is maintained across multiple build increments in
  34. memory:
  35. * The full ASTs of all modules are stored in memory all the time (this
  36. includes the type map).
  37. * A fine-grained dependency map is maintained, which maps triggers to
  38. affected program locations (these can be targets, triggers, or
  39. classes). The latter determine what other parts of a program need to
  40. be processed again due to a fired trigger.
  41. Here's a summary of how a fine-grained incremental program update happens:
  42. * Determine which modules have changes in their source code since the
  43. previous update.
  44. * Process changed modules one at a time. Perform a separate full update
  45. for each changed module, but only report the errors after all modules
  46. have been processed, since the intermediate states can generate bogus
  47. errors due to only seeing a partial set of changes.
  48. * Each changed module is processed in full. We parse the module, and
  49. run semantic analysis to create a new AST and symbol table for the
  50. module. Reuse the existing ASTs and symbol tables of modules that
  51. have no changes in their source code. At the end of this stage, we have
  52. two ASTs and symbol tables for the changed module (the old and the new
  53. versions). The latter AST has not yet been type checked.
  54. * Take a snapshot of the old symbol table. This is used later to determine
  55. which properties of the module have changed and which triggers to fire.
  56. * Merge the old AST with the new AST, preserving the identities of
  57. externally visible AST nodes for which we can find a corresponding node
  58. in the new AST. (Look at mypy.server.astmerge for the details.) This
  59. way all external references to AST nodes in the changed module will
  60. continue to point to the right nodes (assuming they still have a valid
  61. target).
  62. * Type check the new module.
  63. * Take another snapshot of the symbol table of the changed module.
  64. Look at the differences between the old and new snapshots to determine
  65. which parts of the changed modules have changed. The result is a set of
  66. fired triggers.
  67. * Using the dependency map and the fired triggers, decide which other
  68. targets have become stale and need to be reprocessed.
  69. * Create new fine-grained dependencies for the changed module. We don't
  70. garbage collect old dependencies, since extra dependencies are relatively
  71. harmless (they take some memory and can theoretically slow things down
  72. a bit by causing redundant work). This is implemented in
  73. mypy.server.deps.
  74. * Strip the stale AST nodes that we found above. This returns them to a
  75. state resembling the end of semantic analysis pass 1. We'll run semantic
  76. analysis again on the existing AST nodes, and since semantic analysis
  77. is not idempotent, we need to revert some changes made during semantic
  78. analysis. This is implemented in mypy.server.aststrip.
  79. * Run semantic analyzer passes 2 and 3 on the stale AST nodes, and type
  80. check them. We also need to do the symbol table snapshot comparison
  81. dance to find any changes, and we need to merge ASTs to preserve AST node
  82. identities.
  83. * If some triggers haven been fired, continue processing and repeat the
  84. previous steps until no triggers are fired.
  85. This is module is tested using end-to-end fine-grained incremental mode
  86. test cases (test-data/unit/fine-grained*.test).
  87. """
  88. from __future__ import annotations
  89. import os
  90. import re
  91. import sys
  92. import time
  93. from typing import Callable, Final, NamedTuple, Sequence, Union
  94. from typing_extensions import TypeAlias as _TypeAlias
  95. from mypy.build import (
  96. DEBUG_FINE_GRAINED,
  97. FAKE_ROOT_MODULE,
  98. BuildManager,
  99. BuildResult,
  100. Graph,
  101. State,
  102. load_graph,
  103. process_fresh_modules,
  104. )
  105. from mypy.checker import FineGrainedDeferredNode
  106. from mypy.errors import CompileError
  107. from mypy.fscache import FileSystemCache
  108. from mypy.modulefinder import BuildSource
  109. from mypy.nodes import (
  110. Decorator,
  111. FuncDef,
  112. ImportFrom,
  113. MypyFile,
  114. OverloadedFuncDef,
  115. SymbolNode,
  116. SymbolTable,
  117. TypeInfo,
  118. )
  119. from mypy.options import Options
  120. from mypy.semanal_main import (
  121. core_modules,
  122. semantic_analysis_for_scc,
  123. semantic_analysis_for_targets,
  124. )
  125. from mypy.server.astdiff import (
  126. SymbolSnapshot,
  127. compare_symbol_table_snapshots,
  128. snapshot_symbol_table,
  129. )
  130. from mypy.server.astmerge import merge_asts
  131. from mypy.server.aststrip import SavedAttributes, strip_target
  132. from mypy.server.deps import get_dependencies_of_target, merge_dependencies
  133. from mypy.server.target import trigger_to_target
  134. from mypy.server.trigger import WILDCARD_TAG, make_trigger
  135. from mypy.typestate import type_state
  136. from mypy.util import module_prefix, split_target
  137. MAX_ITER: Final = 1000
  138. SENSITIVE_INTERNAL_MODULES = tuple(core_modules) + ("mypy_extensions", "typing_extensions")
  139. class FineGrainedBuildManager:
  140. def __init__(self, result: BuildResult) -> None:
  141. """Initialize fine-grained build based on a batch build.
  142. Args:
  143. result: Result from the initialized build.
  144. The manager and graph will be taken over by this class.
  145. manager: State of the build (mutated by this class)
  146. graph: Additional state of the build (mutated by this class)
  147. """
  148. manager = result.manager
  149. self.manager = manager
  150. self.graph = result.graph
  151. self.previous_modules = get_module_to_path_map(self.graph)
  152. self.deps = manager.fg_deps
  153. # Merge in any root dependencies that may not have been loaded
  154. merge_dependencies(manager.load_fine_grained_deps(FAKE_ROOT_MODULE), self.deps)
  155. self.previous_targets_with_errors = manager.errors.targets()
  156. self.previous_messages: list[str] = result.errors.copy()
  157. # Module, if any, that had blocking errors in the last run as (id, path) tuple.
  158. self.blocking_error: tuple[str, str] | None = None
  159. # Module that we haven't processed yet but that are known to be stale.
  160. self.stale: list[tuple[str, str]] = []
  161. # Disable the cache so that load_graph doesn't try going back to disk
  162. # for the cache.
  163. self.manager.cache_enabled = False
  164. # Some hints to the test suite about what is going on:
  165. # Active triggers during the last update
  166. self.triggered: list[str] = []
  167. # Modules passed to update during the last update
  168. self.changed_modules: list[tuple[str, str]] = []
  169. # Modules processed during the last update
  170. self.updated_modules: list[str] = []
  171. # Targets processed during last update (for testing only).
  172. self.processed_targets: list[str] = []
  173. def update(
  174. self,
  175. changed_modules: list[tuple[str, str]],
  176. removed_modules: list[tuple[str, str]],
  177. followed: bool = False,
  178. ) -> list[str]:
  179. """Update previous build result by processing changed modules.
  180. Also propagate changes to other modules as needed, but only process
  181. those parts of other modules that are affected by the changes. Retain
  182. the existing ASTs and symbol tables of unaffected modules.
  183. Reuses original BuildManager and Graph.
  184. Args:
  185. changed_modules: Modules changed since the previous update/build; each is
  186. a (module id, path) tuple. Includes modified and added modules.
  187. Assume this is correct; it's not validated here.
  188. removed_modules: Modules that have been deleted since the previous update
  189. or removed from the build.
  190. followed: If True, the modules were found through following imports
  191. Returns:
  192. A list of errors.
  193. """
  194. self.processed_targets.clear()
  195. changed_modules = changed_modules + removed_modules
  196. removed_set = {module for module, _ in removed_modules}
  197. self.changed_modules = changed_modules
  198. if not changed_modules:
  199. return self.previous_messages
  200. # Reset find_module's caches for the new build.
  201. self.manager.find_module_cache.clear()
  202. self.triggered = []
  203. self.updated_modules = []
  204. changed_modules = dedupe_modules(changed_modules + self.stale)
  205. initial_set = {id for id, _ in changed_modules}
  206. self.manager.log_fine_grained(
  207. "==== update %s ====" % ", ".join(repr(id) for id, _ in changed_modules)
  208. )
  209. if self.previous_targets_with_errors and is_verbose(self.manager):
  210. self.manager.log_fine_grained(
  211. "previous targets with errors: %s" % sorted(self.previous_targets_with_errors)
  212. )
  213. blocking_error = None
  214. if self.blocking_error:
  215. # Handle blocking errors first. We'll exit as soon as we find a
  216. # module that still has blocking errors.
  217. self.manager.log_fine_grained(f"existing blocker: {self.blocking_error[0]}")
  218. changed_modules = dedupe_modules([self.blocking_error] + changed_modules)
  219. blocking_error = self.blocking_error[0]
  220. self.blocking_error = None
  221. while True:
  222. result = self.update_one(
  223. changed_modules, initial_set, removed_set, blocking_error, followed
  224. )
  225. changed_modules, (next_id, next_path), blocker_messages = result
  226. if blocker_messages is not None:
  227. self.blocking_error = (next_id, next_path)
  228. self.stale = changed_modules
  229. messages = blocker_messages
  230. break
  231. # It looks like we are done processing everything, so now
  232. # reprocess all targets with errors. We are careful to
  233. # support the possibility that reprocessing an errored module
  234. # might trigger loading of a module, but I am not sure
  235. # if this can really happen.
  236. if not changed_modules:
  237. # N.B: We just checked next_id, so manager.errors contains
  238. # the errors from it. Thus we consider next_id up to date
  239. # when propagating changes from the errored targets,
  240. # which prevents us from reprocessing errors in it.
  241. changed_modules = propagate_changes_using_dependencies(
  242. self.manager,
  243. self.graph,
  244. self.deps,
  245. set(),
  246. {next_id},
  247. self.previous_targets_with_errors,
  248. self.processed_targets,
  249. )
  250. changed_modules = dedupe_modules(changed_modules)
  251. if not changed_modules:
  252. # Preserve state needed for the next update.
  253. self.previous_targets_with_errors = self.manager.errors.targets()
  254. messages = self.manager.errors.new_messages()
  255. break
  256. messages = sort_messages_preserving_file_order(messages, self.previous_messages)
  257. self.previous_messages = messages.copy()
  258. return messages
  259. def trigger(self, target: str) -> list[str]:
  260. """Trigger a specific target explicitly.
  261. This is intended for use by the suggestions engine.
  262. """
  263. self.manager.errors.reset()
  264. changed_modules = propagate_changes_using_dependencies(
  265. self.manager,
  266. self.graph,
  267. self.deps,
  268. set(),
  269. set(),
  270. self.previous_targets_with_errors | {target},
  271. [],
  272. )
  273. # Preserve state needed for the next update.
  274. self.previous_targets_with_errors = self.manager.errors.targets()
  275. self.previous_messages = self.manager.errors.new_messages().copy()
  276. return self.update(changed_modules, [])
  277. def flush_cache(self) -> None:
  278. """Flush AST cache.
  279. This needs to be called after each increment, or file changes won't
  280. be detected reliably.
  281. """
  282. self.manager.ast_cache.clear()
  283. def update_one(
  284. self,
  285. changed_modules: list[tuple[str, str]],
  286. initial_set: set[str],
  287. removed_set: set[str],
  288. blocking_error: str | None,
  289. followed: bool,
  290. ) -> tuple[list[tuple[str, str]], tuple[str, str], list[str] | None]:
  291. """Process a module from the list of changed modules.
  292. Returns:
  293. Tuple with these items:
  294. - Updated list of pending changed modules as (module id, path) tuples
  295. - Module which was actually processed as (id, path) tuple
  296. - If there was a blocking error, the error messages from it
  297. """
  298. t0 = time.time()
  299. next_id, next_path = changed_modules.pop(0)
  300. # If we have a module with a blocking error that is no longer
  301. # in the import graph, we must skip it as otherwise we'll be
  302. # stuck with the blocking error.
  303. if (
  304. next_id == blocking_error
  305. and next_id not in self.previous_modules
  306. and next_id not in initial_set
  307. ):
  308. self.manager.log_fine_grained(
  309. f"skip {next_id!r} (module with blocking error not in import graph)"
  310. )
  311. return changed_modules, (next_id, next_path), None
  312. result = self.update_module(next_id, next_path, next_id in removed_set, followed)
  313. remaining, (next_id, next_path), blocker_messages = result
  314. changed_modules = [(id, path) for id, path in changed_modules if id != next_id]
  315. changed_modules = dedupe_modules(remaining + changed_modules)
  316. t1 = time.time()
  317. self.manager.log_fine_grained(
  318. f"update once: {next_id} in {t1 - t0:.3f}s - {len(changed_modules)} left"
  319. )
  320. return changed_modules, (next_id, next_path), blocker_messages
  321. def update_module(
  322. self, module: str, path: str, force_removed: bool, followed: bool
  323. ) -> tuple[list[tuple[str, str]], tuple[str, str], list[str] | None]:
  324. """Update a single modified module.
  325. If the module contains imports of previously unseen modules, only process one of
  326. the new modules and return the remaining work to be done.
  327. Args:
  328. module: Id of the module
  329. path: File system path of the module
  330. force_removed: If True, consider module removed from the build even if path
  331. exists (used for removing an existing file from the build)
  332. followed: Was this found via import following?
  333. Returns:
  334. Tuple with these items:
  335. - Remaining modules to process as (module id, path) tuples
  336. - Module which was actually processed as (id, path) tuple
  337. - If there was a blocking error, the error messages from it
  338. """
  339. self.manager.log_fine_grained(f"--- update single {module!r} ---")
  340. self.updated_modules.append(module)
  341. # builtins and friends could potentially get triggered because
  342. # of protocol stuff, but nothing good could possibly come from
  343. # actually updating them.
  344. if module in SENSITIVE_INTERNAL_MODULES:
  345. return [], (module, path), None
  346. manager = self.manager
  347. previous_modules = self.previous_modules
  348. graph = self.graph
  349. ensure_deps_loaded(module, self.deps, graph)
  350. # If this is an already existing module, make sure that we have
  351. # its tree loaded so that we can snapshot it for comparison.
  352. ensure_trees_loaded(manager, graph, [module])
  353. t0 = time.time()
  354. # Record symbol table snapshot of old version the changed module.
  355. old_snapshots: dict[str, dict[str, SymbolSnapshot]] = {}
  356. if module in manager.modules:
  357. snapshot = snapshot_symbol_table(module, manager.modules[module].names)
  358. old_snapshots[module] = snapshot
  359. manager.errors.reset()
  360. self.processed_targets.append(module)
  361. result = update_module_isolated(
  362. module, path, manager, previous_modules, graph, force_removed, followed
  363. )
  364. if isinstance(result, BlockedUpdate):
  365. # Blocking error -- just give up
  366. module, path, remaining, errors = result
  367. self.previous_modules = get_module_to_path_map(graph)
  368. return remaining, (module, path), errors
  369. assert isinstance(result, NormalUpdate) # Work around #4124
  370. module, path, remaining, tree = result
  371. # TODO: What to do with stale dependencies?
  372. t1 = time.time()
  373. triggered = calculate_active_triggers(manager, old_snapshots, {module: tree})
  374. if is_verbose(self.manager):
  375. filtered = [trigger for trigger in triggered if not trigger.endswith("__>")]
  376. self.manager.log_fine_grained(f"triggered: {sorted(filtered)!r}")
  377. self.triggered.extend(triggered | self.previous_targets_with_errors)
  378. if module in graph:
  379. graph[module].update_fine_grained_deps(self.deps)
  380. graph[module].free_state()
  381. remaining += propagate_changes_using_dependencies(
  382. manager,
  383. graph,
  384. self.deps,
  385. triggered,
  386. {module},
  387. targets_with_errors=set(),
  388. processed_targets=self.processed_targets,
  389. )
  390. t2 = time.time()
  391. manager.add_stats(update_isolated_time=t1 - t0, propagate_time=t2 - t1)
  392. # Preserve state needed for the next update.
  393. self.previous_targets_with_errors.update(manager.errors.targets())
  394. self.previous_modules = get_module_to_path_map(graph)
  395. return remaining, (module, path), None
  396. def find_unloaded_deps(
  397. manager: BuildManager, graph: dict[str, State], initial: Sequence[str]
  398. ) -> list[str]:
  399. """Find all the deps of the nodes in initial that haven't had their tree loaded.
  400. The key invariant here is that if a module is loaded, so are all
  401. of their dependencies. This means that when we encounter a loaded
  402. module, we don't need to explore its dependencies. (This
  403. invariant is slightly violated when dependencies are added, which
  404. can be handled by calling find_unloaded_deps directly on the new
  405. dependencies.)
  406. """
  407. worklist = list(initial)
  408. seen: set[str] = set()
  409. unloaded = []
  410. while worklist:
  411. node = worklist.pop()
  412. if node in seen or node not in graph:
  413. continue
  414. seen.add(node)
  415. if node not in manager.modules:
  416. ancestors = graph[node].ancestors or []
  417. worklist.extend(graph[node].dependencies + ancestors)
  418. unloaded.append(node)
  419. return unloaded
  420. def ensure_deps_loaded(module: str, deps: dict[str, set[str]], graph: dict[str, State]) -> None:
  421. """Ensure that the dependencies on a module are loaded.
  422. Dependencies are loaded into the 'deps' dictionary.
  423. This also requires loading dependencies from any parent modules,
  424. since dependencies will get stored with parent modules when a module
  425. doesn't exist.
  426. """
  427. if module in graph and graph[module].fine_grained_deps_loaded:
  428. return
  429. parts = module.split(".")
  430. for i in range(len(parts)):
  431. base = ".".join(parts[: i + 1])
  432. if base in graph and not graph[base].fine_grained_deps_loaded:
  433. merge_dependencies(graph[base].load_fine_grained_deps(), deps)
  434. graph[base].fine_grained_deps_loaded = True
  435. def ensure_trees_loaded(
  436. manager: BuildManager, graph: dict[str, State], initial: Sequence[str]
  437. ) -> None:
  438. """Ensure that the modules in initial and their deps have loaded trees."""
  439. to_process = find_unloaded_deps(manager, graph, initial)
  440. if to_process:
  441. if is_verbose(manager):
  442. manager.log_fine_grained(
  443. "Calling process_fresh_modules on set of size {} ({})".format(
  444. len(to_process), sorted(to_process)
  445. )
  446. )
  447. process_fresh_modules(graph, to_process, manager)
  448. # The result of update_module_isolated when no blockers, with these items:
  449. #
  450. # - Id of the changed module (can be different from the module argument)
  451. # - Path of the changed module
  452. # - New AST for the changed module (None if module was deleted)
  453. # - Remaining changed modules that are not processed yet as (module id, path)
  454. # tuples (non-empty if the original changed module imported other new
  455. # modules)
  456. class NormalUpdate(NamedTuple):
  457. module: str
  458. path: str
  459. remaining: list[tuple[str, str]]
  460. tree: MypyFile | None
  461. # The result of update_module_isolated when there is a blocking error. Items
  462. # are similar to NormalUpdate (but there are fewer).
  463. class BlockedUpdate(NamedTuple):
  464. module: str
  465. path: str
  466. remaining: list[tuple[str, str]]
  467. messages: list[str]
  468. UpdateResult: _TypeAlias = Union[NormalUpdate, BlockedUpdate]
  469. def update_module_isolated(
  470. module: str,
  471. path: str,
  472. manager: BuildManager,
  473. previous_modules: dict[str, str],
  474. graph: Graph,
  475. force_removed: bool,
  476. followed: bool,
  477. ) -> UpdateResult:
  478. """Build a new version of one changed module only.
  479. Don't propagate changes to elsewhere in the program. Raise CompileError on
  480. encountering a blocking error.
  481. Args:
  482. module: Changed module (modified, created or deleted)
  483. path: Path of the changed module
  484. manager: Build manager
  485. graph: Build graph
  486. force_removed: If True, consider the module removed from the build even it the
  487. file exists
  488. Returns a named tuple describing the result (see above for details).
  489. """
  490. if module not in graph:
  491. manager.log_fine_grained(f"new module {module!r}")
  492. if not manager.fscache.isfile(path) or force_removed:
  493. delete_module(module, path, graph, manager)
  494. return NormalUpdate(module, path, [], None)
  495. sources = get_sources(manager.fscache, previous_modules, [(module, path)], followed)
  496. if module in manager.missing_modules:
  497. manager.missing_modules.remove(module)
  498. orig_module = module
  499. orig_state = graph.get(module)
  500. orig_tree = manager.modules.get(module)
  501. def restore(ids: list[str]) -> None:
  502. # For each of the modules in ids, restore that id's old
  503. # manager.modules and graphs entries. (Except for the original
  504. # module, this means deleting them.)
  505. for id in ids:
  506. if id == orig_module and orig_tree:
  507. manager.modules[id] = orig_tree
  508. elif id in manager.modules:
  509. del manager.modules[id]
  510. if id == orig_module and orig_state:
  511. graph[id] = orig_state
  512. elif id in graph:
  513. del graph[id]
  514. new_modules: list[State] = []
  515. try:
  516. if module in graph:
  517. del graph[module]
  518. load_graph(sources, manager, graph, new_modules)
  519. except CompileError as err:
  520. # Parse error somewhere in the program -- a blocker
  521. assert err.module_with_blocker
  522. restore([module] + [st.id for st in new_modules])
  523. return BlockedUpdate(err.module_with_blocker, path, [], err.messages)
  524. # Reparsing the file may have brought in dependencies that we
  525. # didn't have before. Make sure that they are loaded to restore
  526. # the invariant that a module having a loaded tree implies that
  527. # its dependencies do as well.
  528. ensure_trees_loaded(manager, graph, graph[module].dependencies)
  529. # Find any other modules brought in by imports.
  530. changed_modules = [(st.id, st.xpath) for st in new_modules]
  531. # If there are multiple modules to process, only process one of them and return
  532. # the remaining ones to the caller.
  533. if len(changed_modules) > 1:
  534. # As an optimization, look for a module that imports no other changed modules.
  535. module, path = find_relative_leaf_module(changed_modules, graph)
  536. changed_modules.remove((module, path))
  537. remaining_modules = changed_modules
  538. # The remaining modules haven't been processed yet so drop them.
  539. restore([id for id, _ in remaining_modules])
  540. manager.log_fine_grained(f"--> {module!r} (newly imported)")
  541. else:
  542. remaining_modules = []
  543. state = graph[module]
  544. # Process the changed file.
  545. state.parse_file()
  546. assert state.tree is not None, "file must be at least parsed"
  547. t0 = time.time()
  548. try:
  549. semantic_analysis_for_scc(graph, [state.id], manager.errors)
  550. except CompileError as err:
  551. # There was a blocking error, so module AST is incomplete. Restore old modules.
  552. restore([module])
  553. return BlockedUpdate(module, path, remaining_modules, err.messages)
  554. # Merge old and new ASTs.
  555. new_modules_dict: dict[str, MypyFile | None] = {module: state.tree}
  556. replace_modules_with_new_variants(manager, graph, {orig_module: orig_tree}, new_modules_dict)
  557. t1 = time.time()
  558. # Perform type checking.
  559. state.type_checker().reset()
  560. state.type_check_first_pass()
  561. state.type_check_second_pass()
  562. state.detect_possibly_undefined_vars()
  563. t2 = time.time()
  564. state.finish_passes()
  565. t3 = time.time()
  566. manager.add_stats(semanal_time=t1 - t0, typecheck_time=t2 - t1, finish_passes_time=t3 - t2)
  567. graph[module] = state
  568. return NormalUpdate(module, path, remaining_modules, state.tree)
  569. def find_relative_leaf_module(modules: list[tuple[str, str]], graph: Graph) -> tuple[str, str]:
  570. """Find a module in a list that directly imports no other module in the list.
  571. If no such module exists, return the lexicographically first module from the list.
  572. Always return one of the items in the modules list.
  573. NOTE: If both 'abc' and 'typing' have changed, an effect of the above rule is that
  574. we prefer 'abc', even if both are in the same SCC. This works around a false
  575. positive in 'typing', at least in tests.
  576. Args:
  577. modules: List of (module, path) tuples (non-empty)
  578. graph: Program import graph that contains all modules in the module list
  579. """
  580. assert modules
  581. # Sort for repeatable results.
  582. modules = sorted(modules)
  583. module_set = {module for module, _ in modules}
  584. for module, path in modules:
  585. state = graph[module]
  586. if len(set(state.dependencies) & module_set) == 0:
  587. # Found it!
  588. return module, path
  589. # Could not find any. Just return the first module (by lexicographic order).
  590. return modules[0]
  591. def delete_module(module_id: str, path: str, graph: Graph, manager: BuildManager) -> None:
  592. manager.log_fine_grained(f"delete module {module_id!r}")
  593. # TODO: Remove deps for the module (this only affects memory use, not correctness)
  594. if module_id in graph:
  595. del graph[module_id]
  596. if module_id in manager.modules:
  597. del manager.modules[module_id]
  598. components = module_id.split(".")
  599. if len(components) > 1:
  600. # Delete reference to module in parent module.
  601. parent_id = ".".join(components[:-1])
  602. # If parent module is ignored, it won't be included in the modules dictionary.
  603. if parent_id in manager.modules:
  604. parent = manager.modules[parent_id]
  605. if components[-1] in parent.names:
  606. del parent.names[components[-1]]
  607. # If the module is removed from the build but still exists, then
  608. # we mark it as missing so that it will get picked up by import from still.
  609. if manager.fscache.isfile(path):
  610. manager.missing_modules.add(module_id)
  611. def dedupe_modules(modules: list[tuple[str, str]]) -> list[tuple[str, str]]:
  612. seen: set[str] = set()
  613. result = []
  614. for id, path in modules:
  615. if id not in seen:
  616. seen.add(id)
  617. result.append((id, path))
  618. return result
  619. def get_module_to_path_map(graph: Graph) -> dict[str, str]:
  620. return {module: node.xpath for module, node in graph.items()}
  621. def get_sources(
  622. fscache: FileSystemCache,
  623. modules: dict[str, str],
  624. changed_modules: list[tuple[str, str]],
  625. followed: bool,
  626. ) -> list[BuildSource]:
  627. sources = []
  628. for id, path in changed_modules:
  629. if fscache.isfile(path):
  630. sources.append(BuildSource(path, id, None, followed=followed))
  631. return sources
  632. def calculate_active_triggers(
  633. manager: BuildManager,
  634. old_snapshots: dict[str, dict[str, SymbolSnapshot]],
  635. new_modules: dict[str, MypyFile | None],
  636. ) -> set[str]:
  637. """Determine activated triggers by comparing old and new symbol tables.
  638. For example, if only the signature of function m.f is different in the new
  639. symbol table, return {'<m.f>'}.
  640. """
  641. names: set[str] = set()
  642. for id in new_modules:
  643. snapshot1 = old_snapshots.get(id)
  644. if snapshot1 is None:
  645. names.add(id)
  646. snapshot1 = {}
  647. new = new_modules[id]
  648. if new is None:
  649. snapshot2 = snapshot_symbol_table(id, SymbolTable())
  650. names.add(id)
  651. else:
  652. snapshot2 = snapshot_symbol_table(id, new.names)
  653. diff = compare_symbol_table_snapshots(id, snapshot1, snapshot2)
  654. package_nesting_level = id.count(".")
  655. for item in diff.copy():
  656. if item.count(".") <= package_nesting_level + 1 and item.split(".")[-1] not in (
  657. "__builtins__",
  658. "__file__",
  659. "__name__",
  660. "__package__",
  661. "__doc__",
  662. ):
  663. # Activate catch-all wildcard trigger for top-level module changes (used for
  664. # "from m import *"). This also gets triggered by changes to module-private
  665. # entries, but as these unneeded dependencies only result in extra processing,
  666. # it's a minor problem.
  667. #
  668. # TODO: Some __* names cause mistriggers. Fix the underlying issue instead of
  669. # special casing them here.
  670. diff.add(id + WILDCARD_TAG)
  671. if item.count(".") > package_nesting_level + 1:
  672. # These are for changes within classes, used by protocols.
  673. diff.add(item.rsplit(".", 1)[0] + WILDCARD_TAG)
  674. names |= diff
  675. return {make_trigger(name) for name in names}
  676. def replace_modules_with_new_variants(
  677. manager: BuildManager,
  678. graph: dict[str, State],
  679. old_modules: dict[str, MypyFile | None],
  680. new_modules: dict[str, MypyFile | None],
  681. ) -> None:
  682. """Replace modules with newly builds versions.
  683. Retain the identities of externally visible AST nodes in the
  684. old ASTs so that references to the affected modules from other
  685. modules will still be valid (unless something was deleted or
  686. replaced with an incompatible definition, in which case there
  687. will be dangling references that will be handled by
  688. propagate_changes_using_dependencies).
  689. """
  690. for id in new_modules:
  691. preserved_module = old_modules.get(id)
  692. new_module = new_modules[id]
  693. if preserved_module and new_module is not None:
  694. merge_asts(preserved_module, preserved_module.names, new_module, new_module.names)
  695. manager.modules[id] = preserved_module
  696. graph[id].tree = preserved_module
  697. def propagate_changes_using_dependencies(
  698. manager: BuildManager,
  699. graph: dict[str, State],
  700. deps: dict[str, set[str]],
  701. triggered: set[str],
  702. up_to_date_modules: set[str],
  703. targets_with_errors: set[str],
  704. processed_targets: list[str],
  705. ) -> list[tuple[str, str]]:
  706. """Transitively rechecks targets based on triggers and the dependency map.
  707. Returns a list (module id, path) tuples representing modules that contain
  708. a target that needs to be reprocessed but that has not been parsed yet.
  709. Processed targets should be appended to processed_targets (used in tests only,
  710. to test the order of processing targets).
  711. """
  712. num_iter = 0
  713. remaining_modules: list[tuple[str, str]] = []
  714. # Propagate changes until nothing visible has changed during the last
  715. # iteration.
  716. while triggered or targets_with_errors:
  717. num_iter += 1
  718. if num_iter > MAX_ITER:
  719. raise RuntimeError("Max number of iterations (%d) reached (endless loop?)" % MAX_ITER)
  720. todo, unloaded, stale_protos = find_targets_recursive(
  721. manager, graph, triggered, deps, up_to_date_modules
  722. )
  723. # TODO: we sort to make it deterministic, but this is *incredibly* ad hoc
  724. remaining_modules.extend((id, graph[id].xpath) for id in sorted(unloaded))
  725. # Also process targets that used to have errors, as otherwise some
  726. # errors might be lost.
  727. for target in targets_with_errors:
  728. id = module_prefix(graph, target)
  729. if id is not None and id not in up_to_date_modules:
  730. if id not in todo:
  731. todo[id] = set()
  732. manager.log_fine_grained(f"process target with error: {target}")
  733. more_nodes, _ = lookup_target(manager, target)
  734. todo[id].update(more_nodes)
  735. triggered = set()
  736. # First invalidate subtype caches in all stale protocols.
  737. # We need to do this to avoid false negatives if the protocol itself is
  738. # unchanged, but was marked stale because its sub- (or super-) type changed.
  739. for info in stale_protos:
  740. type_state.reset_subtype_caches_for(info)
  741. # Then fully reprocess all targets.
  742. # TODO: Preserve order (set is not optimal)
  743. for id, nodes in sorted(todo.items(), key=lambda x: x[0]):
  744. assert id not in up_to_date_modules
  745. triggered |= reprocess_nodes(manager, graph, id, nodes, deps, processed_targets)
  746. # Changes elsewhere may require us to reprocess modules that were
  747. # previously considered up to date. For example, there may be a
  748. # dependency loop that loops back to an originally processed module.
  749. up_to_date_modules = set()
  750. targets_with_errors = set()
  751. if is_verbose(manager):
  752. manager.log_fine_grained(f"triggered: {list(triggered)!r}")
  753. return remaining_modules
  754. def find_targets_recursive(
  755. manager: BuildManager,
  756. graph: Graph,
  757. triggers: set[str],
  758. deps: dict[str, set[str]],
  759. up_to_date_modules: set[str],
  760. ) -> tuple[dict[str, set[FineGrainedDeferredNode]], set[str], set[TypeInfo]]:
  761. """Find names of all targets that need to reprocessed, given some triggers.
  762. Returns: A tuple containing a:
  763. * Dictionary from module id to a set of stale targets.
  764. * A set of module ids for unparsed modules with stale targets.
  765. """
  766. result: dict[str, set[FineGrainedDeferredNode]] = {}
  767. worklist = triggers
  768. processed: set[str] = set()
  769. stale_protos: set[TypeInfo] = set()
  770. unloaded_files: set[str] = set()
  771. # Find AST nodes corresponding to each target.
  772. #
  773. # TODO: Don't rely on a set, since the items are in an unpredictable order.
  774. while worklist:
  775. processed |= worklist
  776. current = worklist
  777. worklist = set()
  778. for target in current:
  779. if target.startswith("<"):
  780. module_id = module_prefix(graph, trigger_to_target(target))
  781. if module_id:
  782. ensure_deps_loaded(module_id, deps, graph)
  783. worklist |= deps.get(target, set()) - processed
  784. else:
  785. module_id = module_prefix(graph, target)
  786. if module_id is None:
  787. # Deleted module.
  788. continue
  789. if module_id in up_to_date_modules:
  790. # Already processed.
  791. continue
  792. if (
  793. module_id not in manager.modules
  794. or manager.modules[module_id].is_cache_skeleton
  795. ):
  796. # We haven't actually parsed and checked the module, so we don't have
  797. # access to the actual nodes.
  798. # Add it to the queue of files that need to be processed fully.
  799. unloaded_files.add(module_id)
  800. continue
  801. if module_id not in result:
  802. result[module_id] = set()
  803. manager.log_fine_grained(f"process: {target}")
  804. deferred, stale_proto = lookup_target(manager, target)
  805. if stale_proto:
  806. stale_protos.add(stale_proto)
  807. result[module_id].update(deferred)
  808. return result, unloaded_files, stale_protos
  809. def reprocess_nodes(
  810. manager: BuildManager,
  811. graph: dict[str, State],
  812. module_id: str,
  813. nodeset: set[FineGrainedDeferredNode],
  814. deps: dict[str, set[str]],
  815. processed_targets: list[str],
  816. ) -> set[str]:
  817. """Reprocess a set of nodes within a single module.
  818. Return fired triggers.
  819. """
  820. if module_id not in graph:
  821. manager.log_fine_grained("%s not in graph (blocking errors or deleted?)" % module_id)
  822. return set()
  823. file_node = manager.modules[module_id]
  824. old_symbols = find_symbol_tables_recursive(file_node.fullname, file_node.names)
  825. old_symbols = {name: names.copy() for name, names in old_symbols.items()}
  826. old_symbols_snapshot = snapshot_symbol_table(file_node.fullname, file_node.names)
  827. def key(node: FineGrainedDeferredNode) -> int:
  828. # Unlike modules which are sorted by name within SCC,
  829. # nodes within the same module are sorted by line number, because
  830. # this is how they are processed in normal mode.
  831. return node.node.line
  832. nodes = sorted(nodeset, key=key)
  833. state = graph[module_id]
  834. options = state.options
  835. manager.errors.set_file_ignored_lines(
  836. file_node.path, file_node.ignored_lines, options.ignore_errors or state.ignore_all
  837. )
  838. manager.errors.set_skipped_lines(file_node.path, file_node.skipped_lines)
  839. targets = set()
  840. for node in nodes:
  841. target = target_from_node(module_id, node.node)
  842. if target is not None:
  843. targets.add(target)
  844. manager.errors.clear_errors_in_targets(file_node.path, targets)
  845. # If one of the nodes is the module itself, emit any errors that
  846. # happened before semantic analysis.
  847. for target in targets:
  848. if target == module_id:
  849. for info in graph[module_id].early_errors:
  850. manager.errors.add_error_info(info)
  851. # Strip semantic analysis information.
  852. saved_attrs: SavedAttributes = {}
  853. for deferred in nodes:
  854. processed_targets.append(deferred.node.fullname)
  855. strip_target(deferred.node, saved_attrs)
  856. semantic_analysis_for_targets(graph[module_id], nodes, graph, saved_attrs)
  857. # Merge symbol tables to preserve identities of AST nodes. The file node will remain
  858. # the same, but other nodes may have been recreated with different identities, such as
  859. # NamedTuples defined using assignment statements.
  860. new_symbols = find_symbol_tables_recursive(file_node.fullname, file_node.names)
  861. for name in old_symbols:
  862. if name in new_symbols:
  863. merge_asts(file_node, old_symbols[name], file_node, new_symbols[name])
  864. # Type check.
  865. checker = graph[module_id].type_checker()
  866. checker.reset()
  867. # We seem to need additional passes in fine-grained incremental mode.
  868. checker.pass_num = 0
  869. checker.last_pass = 3
  870. more = checker.check_second_pass(nodes)
  871. while more:
  872. more = False
  873. if graph[module_id].type_checker().check_second_pass():
  874. more = True
  875. if manager.options.export_types:
  876. manager.all_types.update(graph[module_id].type_map())
  877. new_symbols_snapshot = snapshot_symbol_table(file_node.fullname, file_node.names)
  878. # Check if any attribute types were changed and need to be propagated further.
  879. changed = compare_symbol_table_snapshots(
  880. file_node.fullname, old_symbols_snapshot, new_symbols_snapshot
  881. )
  882. new_triggered = {make_trigger(name) for name in changed}
  883. # Dependencies may have changed.
  884. update_deps(module_id, nodes, graph, deps, options)
  885. # Report missing imports.
  886. graph[module_id].verify_dependencies()
  887. graph[module_id].free_state()
  888. return new_triggered
  889. def find_symbol_tables_recursive(prefix: str, symbols: SymbolTable) -> dict[str, SymbolTable]:
  890. """Find all nested symbol tables.
  891. Args:
  892. prefix: Full name prefix (used for return value keys and to filter result so that
  893. cross references to other modules aren't included)
  894. symbols: Root symbol table
  895. Returns a dictionary from full name to corresponding symbol table.
  896. """
  897. result = {}
  898. result[prefix] = symbols
  899. for name, node in symbols.items():
  900. if isinstance(node.node, TypeInfo) and node.node.fullname.startswith(prefix + "."):
  901. more = find_symbol_tables_recursive(prefix + "." + name, node.node.names)
  902. result.update(more)
  903. return result
  904. def update_deps(
  905. module_id: str,
  906. nodes: list[FineGrainedDeferredNode],
  907. graph: dict[str, State],
  908. deps: dict[str, set[str]],
  909. options: Options,
  910. ) -> None:
  911. for deferred in nodes:
  912. node = deferred.node
  913. type_map = graph[module_id].type_map()
  914. tree = graph[module_id].tree
  915. assert tree is not None, "Tree must be processed at this stage"
  916. new_deps = get_dependencies_of_target(
  917. module_id, tree, node, type_map, options.python_version
  918. )
  919. for trigger, targets in new_deps.items():
  920. deps.setdefault(trigger, set()).update(targets)
  921. # Merge also the newly added protocol deps (if any).
  922. type_state.update_protocol_deps(deps)
  923. def lookup_target(
  924. manager: BuildManager, target: str
  925. ) -> tuple[list[FineGrainedDeferredNode], TypeInfo | None]:
  926. """Look up a target by fully-qualified name.
  927. The first item in the return tuple is a list of deferred nodes that
  928. needs to be reprocessed. If the target represents a TypeInfo corresponding
  929. to a protocol, return it as a second item in the return tuple, otherwise None.
  930. """
  931. def not_found() -> None:
  932. manager.log_fine_grained(f"Can't find matching target for {target} (stale dependency?)")
  933. modules = manager.modules
  934. items = split_target(modules, target)
  935. if items is None:
  936. not_found() # Stale dependency
  937. return [], None
  938. module, rest = items
  939. if rest:
  940. components = rest.split(".")
  941. else:
  942. components = []
  943. node: SymbolNode | None = modules[module]
  944. file: MypyFile | None = None
  945. active_class = None
  946. for c in components:
  947. if isinstance(node, TypeInfo):
  948. active_class = node
  949. if isinstance(node, MypyFile):
  950. file = node
  951. if not isinstance(node, (MypyFile, TypeInfo)) or c not in node.names:
  952. not_found() # Stale dependency
  953. return [], None
  954. # Don't reprocess plugin generated targets. They should get
  955. # stripped and regenerated when the containing target is
  956. # reprocessed.
  957. if node.names[c].plugin_generated:
  958. return [], None
  959. node = node.names[c].node
  960. if isinstance(node, TypeInfo):
  961. # A ClassDef target covers the body of the class and everything defined
  962. # within it. To get the body we include the entire surrounding target,
  963. # typically a module top-level, since we don't support processing class
  964. # bodies as separate entities for simplicity.
  965. assert file is not None
  966. if node.fullname != target:
  967. # This is a reference to a different TypeInfo, likely due to a stale dependency.
  968. # Processing them would spell trouble -- for example, we could be refreshing
  969. # a deserialized TypeInfo with missing attributes.
  970. not_found()
  971. return [], None
  972. result = [FineGrainedDeferredNode(file, None)]
  973. stale_info: TypeInfo | None = None
  974. if node.is_protocol:
  975. stale_info = node
  976. for name, symnode in node.names.items():
  977. node = symnode.node
  978. if isinstance(node, FuncDef):
  979. method, _ = lookup_target(manager, target + "." + name)
  980. result.extend(method)
  981. return result, stale_info
  982. if isinstance(node, Decorator):
  983. # Decorator targets actually refer to the function definition only.
  984. node = node.func
  985. if not isinstance(node, (FuncDef, MypyFile, OverloadedFuncDef)):
  986. # The target can't be refreshed. It's possible that the target was
  987. # changed to another type and we have a stale dependency pointing to it.
  988. not_found()
  989. return [], None
  990. if node.fullname != target:
  991. # Stale reference points to something unexpected. We shouldn't process since the
  992. # context will be wrong and it could be a partially initialized deserialized node.
  993. not_found()
  994. return [], None
  995. return [FineGrainedDeferredNode(node, active_class)], None
  996. def is_verbose(manager: BuildManager) -> bool:
  997. return manager.options.verbosity >= 1 or DEBUG_FINE_GRAINED
  998. def target_from_node(module: str, node: FuncDef | MypyFile | OverloadedFuncDef) -> str | None:
  999. """Return the target name corresponding to a deferred node.
  1000. Args:
  1001. module: Must be module id of the module that defines 'node'
  1002. Returns the target name, or None if the node is not a valid target in the given
  1003. module (for example, if it's actually defined in another module).
  1004. """
  1005. if isinstance(node, MypyFile):
  1006. if module != node.fullname:
  1007. # Actually a reference to another module -- likely a stale dependency.
  1008. return None
  1009. return module
  1010. else: # OverloadedFuncDef or FuncDef
  1011. if node.info:
  1012. return f"{node.info.fullname}.{node.name}"
  1013. else:
  1014. return f"{module}.{node.name}"
  1015. if sys.platform != "win32":
  1016. INIT_SUFFIXES: Final = ("/__init__.py", "/__init__.pyi")
  1017. else:
  1018. INIT_SUFFIXES: Final = (
  1019. os.sep + "__init__.py",
  1020. os.sep + "__init__.pyi",
  1021. os.altsep + "__init__.py",
  1022. os.altsep + "__init__.pyi",
  1023. )
  1024. def refresh_suppressed_submodules(
  1025. module: str,
  1026. path: str | None,
  1027. deps: dict[str, set[str]],
  1028. graph: Graph,
  1029. fscache: FileSystemCache,
  1030. refresh_file: Callable[[str, str], list[str]],
  1031. ) -> list[str] | None:
  1032. """Look for submodules that are now suppressed in target package.
  1033. If a submodule a.b gets added, we need to mark it as suppressed
  1034. in modules that contain "from a import b". Previously we assumed
  1035. that 'a.b' is not a module but a regular name.
  1036. This is only relevant when following imports normally.
  1037. Args:
  1038. module: target package in which to look for submodules
  1039. path: path of the module
  1040. refresh_file: function that reads the AST of a module (returns error messages)
  1041. Return a list of errors from refresh_file() if it was called. If the
  1042. return value is None, we didn't call refresh_file().
  1043. """
  1044. messages = None
  1045. if path is None or not path.endswith(INIT_SUFFIXES):
  1046. # Only packages have submodules.
  1047. return None
  1048. # Find any submodules present in the directory.
  1049. pkgdir = os.path.dirname(path)
  1050. try:
  1051. entries = fscache.listdir(pkgdir)
  1052. except FileNotFoundError:
  1053. entries = []
  1054. for fnam in entries:
  1055. if (
  1056. not fnam.endswith((".py", ".pyi"))
  1057. or fnam.startswith("__init__.")
  1058. or fnam.count(".") != 1
  1059. ):
  1060. continue
  1061. shortname = fnam.split(".")[0]
  1062. submodule = module + "." + shortname
  1063. trigger = make_trigger(submodule)
  1064. # We may be missing the required fine-grained deps.
  1065. ensure_deps_loaded(module, deps, graph)
  1066. if trigger in deps:
  1067. for dep in deps[trigger]:
  1068. # We can ignore <...> deps since a submodule can't trigger any.
  1069. state = graph.get(dep)
  1070. if not state:
  1071. # Maybe it's a non-top-level target. We only care about the module.
  1072. dep_module = module_prefix(graph, dep)
  1073. if dep_module is not None:
  1074. state = graph.get(dep_module)
  1075. if state:
  1076. # Is the file may missing an AST in case it's read from cache?
  1077. if state.tree is None:
  1078. # Create AST for the file. This may produce some new errors
  1079. # that we need to propagate.
  1080. assert state.path is not None
  1081. messages = refresh_file(state.id, state.path)
  1082. tree = state.tree
  1083. assert tree # Will be fine, due to refresh_file() above
  1084. for imp in tree.imports:
  1085. if isinstance(imp, ImportFrom):
  1086. if (
  1087. imp.id == module
  1088. and any(name == shortname for name, _ in imp.names)
  1089. and submodule not in state.suppressed_set
  1090. ):
  1091. state.suppressed.append(submodule)
  1092. state.suppressed_set.add(submodule)
  1093. return messages
  1094. def extract_fnam_from_message(message: str) -> str | None:
  1095. m = re.match(r"([^:]+):[0-9]+: (error|note): ", message)
  1096. if m:
  1097. return m.group(1)
  1098. return None
  1099. def extract_possible_fnam_from_message(message: str) -> str:
  1100. # This may return non-path things if there is some random colon on the line
  1101. return message.split(":", 1)[0]
  1102. def sort_messages_preserving_file_order(
  1103. messages: list[str], prev_messages: list[str]
  1104. ) -> list[str]:
  1105. """Sort messages so that the order of files is preserved.
  1106. An update generates messages so that the files can be in a fairly
  1107. arbitrary order. Preserve the order of files to avoid messages
  1108. getting reshuffled continuously. If there are messages in
  1109. additional files, sort them towards the end.
  1110. """
  1111. # Calculate file order from the previous messages
  1112. n = 0
  1113. order = {}
  1114. for msg in prev_messages:
  1115. fnam = extract_fnam_from_message(msg)
  1116. if fnam and fnam not in order:
  1117. order[fnam] = n
  1118. n += 1
  1119. # Related messages must be sorted as a group of successive lines
  1120. groups = []
  1121. i = 0
  1122. while i < len(messages):
  1123. msg = messages[i]
  1124. maybe_fnam = extract_possible_fnam_from_message(msg)
  1125. group = [msg]
  1126. if maybe_fnam in order:
  1127. # This looks like a file name. Collect all lines related to this message.
  1128. while (
  1129. i + 1 < len(messages)
  1130. and extract_possible_fnam_from_message(messages[i + 1]) not in order
  1131. and extract_fnam_from_message(messages[i + 1]) is None
  1132. and not messages[i + 1].startswith("mypy: ")
  1133. ):
  1134. i += 1
  1135. group.append(messages[i])
  1136. groups.append((order.get(maybe_fnam, n), group))
  1137. i += 1
  1138. groups = sorted(groups, key=lambda g: g[0])
  1139. result = []
  1140. for key, group in groups:
  1141. result.extend(group)
  1142. return result