semanal_main.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. """Top-level logic for the semantic analyzer.
  2. The semantic analyzer binds names, resolves imports, detects various
  3. special constructs that don't have dedicated AST nodes after parse
  4. (such as 'cast' which looks like a call), populates symbol tables, and
  5. performs various simple consistency checks.
  6. Semantic analysis of each SCC (strongly connected component; import
  7. cycle) is performed in one unit. Each module is analyzed as multiple
  8. separate *targets*; the module top level is one target and each function
  9. is a target. Nested functions are not separate targets, however. This is
  10. mostly identical to targets used by mypy daemon (but classes aren't
  11. targets in semantic analysis).
  12. We first analyze each module top level in an SCC. If we encounter some
  13. names that we can't bind because the target of the name may not have
  14. been processed yet, we *defer* the current target for further
  15. processing. Deferred targets will be analyzed additional times until
  16. everything can be bound, or we reach a maximum number of iterations.
  17. We keep track of a set of incomplete namespaces, i.e. namespaces that we
  18. haven't finished populating yet. References to these namespaces cause a
  19. deferral if they can't be satisfied. Initially every module in the SCC
  20. will be incomplete.
  21. """
  22. from __future__ import annotations
  23. from contextlib import nullcontext
  24. from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union
  25. from typing_extensions import Final, TypeAlias as _TypeAlias
  26. import mypy.build
  27. import mypy.state
  28. from mypy.checker import FineGrainedDeferredNode
  29. from mypy.errors import Errors
  30. from mypy.nodes import Decorator, FuncDef, MypyFile, OverloadedFuncDef, TypeInfo, Var
  31. from mypy.options import Options
  32. from mypy.plugin import ClassDefContext
  33. from mypy.plugins import dataclasses as dataclasses_plugin
  34. from mypy.semanal import (
  35. SemanticAnalyzer,
  36. apply_semantic_analyzer_patches,
  37. remove_imported_names_from_symtable,
  38. )
  39. from mypy.semanal_classprop import (
  40. add_type_promotion,
  41. calculate_class_abstract_status,
  42. calculate_class_vars,
  43. check_protocol_status,
  44. )
  45. from mypy.semanal_infer import infer_decorator_signature_if_simple
  46. from mypy.semanal_shared import find_dataclass_transform_spec
  47. from mypy.semanal_typeargs import TypeArgumentAnalyzer
  48. from mypy.server.aststrip import SavedAttributes
  49. from mypy.util import is_typeshed_file
  50. if TYPE_CHECKING:
  51. from mypy.build import Graph, State
  52. Patches: _TypeAlias = List[Tuple[int, Callable[[], None]]]
  53. # If we perform this many iterations, raise an exception since we are likely stuck.
  54. MAX_ITERATIONS: Final = 20
  55. # Number of passes over core modules before going on to the rest of the builtin SCC.
  56. CORE_WARMUP: Final = 2
  57. core_modules: Final = [
  58. "typing",
  59. "_collections_abc",
  60. "builtins",
  61. "abc",
  62. "collections",
  63. "collections.abc",
  64. ]
  65. def semantic_analysis_for_scc(graph: Graph, scc: list[str], errors: Errors) -> None:
  66. """Perform semantic analysis for all modules in a SCC (import cycle).
  67. Assume that reachability analysis has already been performed.
  68. The scc will be processed roughly in the order the modules are included
  69. in the list.
  70. """
  71. patches: Patches = []
  72. # Note that functions can't define new module-level attributes
  73. # using 'global x', since module top levels are fully processed
  74. # before functions. This limitation is unlikely to go away soon.
  75. process_top_levels(graph, scc, patches)
  76. process_functions(graph, scc, patches)
  77. # We use patch callbacks to fix up things when we expect relatively few
  78. # callbacks to be required.
  79. apply_semantic_analyzer_patches(patches)
  80. # Run class decorator hooks (they requite complete MROs and no placeholders).
  81. apply_class_plugin_hooks(graph, scc, errors)
  82. # This pass might need fallbacks calculated above and the results of hooks.
  83. check_type_arguments(graph, scc, errors)
  84. calculate_class_properties(graph, scc, errors)
  85. check_blockers(graph, scc)
  86. # Clean-up builtins, so that TypeVar etc. are not accessible without importing.
  87. if "builtins" in scc:
  88. cleanup_builtin_scc(graph["builtins"])
  89. def cleanup_builtin_scc(state: State) -> None:
  90. """Remove imported names from builtins namespace.
  91. This way names imported from typing in builtins.pyi aren't available
  92. by default (without importing them). We can only do this after processing
  93. the whole SCC is finished, when the imported names aren't needed for
  94. processing builtins.pyi itself.
  95. """
  96. assert state.tree is not None
  97. remove_imported_names_from_symtable(state.tree.names, "builtins")
  98. def semantic_analysis_for_targets(
  99. state: State, nodes: list[FineGrainedDeferredNode], graph: Graph, saved_attrs: SavedAttributes
  100. ) -> None:
  101. """Semantically analyze only selected nodes in a given module.
  102. This essentially mirrors the logic of semantic_analysis_for_scc()
  103. except that we process only some targets. This is used in fine grained
  104. incremental mode, when propagating an update.
  105. The saved_attrs are implicitly declared instance attributes (attributes
  106. defined on self) removed by AST stripper that may need to be reintroduced
  107. here. They must be added before any methods are analyzed.
  108. """
  109. patches: Patches = []
  110. if any(isinstance(n.node, MypyFile) for n in nodes):
  111. # Process module top level first (if needed).
  112. process_top_levels(graph, [state.id], patches)
  113. restore_saved_attrs(saved_attrs)
  114. analyzer = state.manager.semantic_analyzer
  115. for n in nodes:
  116. if isinstance(n.node, MypyFile):
  117. # Already done above.
  118. continue
  119. process_top_level_function(
  120. analyzer, state, state.id, n.node.fullname, n.node, n.active_typeinfo, patches
  121. )
  122. apply_semantic_analyzer_patches(patches)
  123. apply_class_plugin_hooks(graph, [state.id], state.manager.errors)
  124. check_type_arguments_in_targets(nodes, state, state.manager.errors)
  125. calculate_class_properties(graph, [state.id], state.manager.errors)
  126. def restore_saved_attrs(saved_attrs: SavedAttributes) -> None:
  127. """Restore instance variables removed during AST strip that haven't been added yet."""
  128. for (cdef, name), sym in saved_attrs.items():
  129. info = cdef.info
  130. existing = info.get(name)
  131. defined_in_this_class = name in info.names
  132. assert isinstance(sym.node, Var)
  133. # This needs to mimic the logic in SemanticAnalyzer.analyze_member_lvalue()
  134. # regarding the existing variable in class body or in a superclass:
  135. # If the attribute of self is not defined in superclasses, create a new Var.
  136. if (
  137. existing is None
  138. or
  139. # (An abstract Var is considered as not defined.)
  140. (isinstance(existing.node, Var) and existing.node.is_abstract_var)
  141. or
  142. # Also an explicit declaration on self creates a new Var unless
  143. # there is already one defined in the class body.
  144. sym.node.explicit_self_type
  145. and not defined_in_this_class
  146. ):
  147. info.names[name] = sym
  148. def process_top_levels(graph: Graph, scc: list[str], patches: Patches) -> None:
  149. # Process top levels until everything has been bound.
  150. # Reverse order of the scc so the first modules in the original list will be
  151. # be processed first. This helps with performance.
  152. scc = list(reversed(scc))
  153. # Initialize ASTs and symbol tables.
  154. for id in scc:
  155. state = graph[id]
  156. assert state.tree is not None
  157. state.manager.semantic_analyzer.prepare_file(state.tree)
  158. # Initially all namespaces in the SCC are incomplete (well they are empty).
  159. state.manager.incomplete_namespaces.update(scc)
  160. worklist = scc.copy()
  161. # HACK: process core stuff first. This is mostly needed to support defining
  162. # named tuples in builtin SCC.
  163. if all(m in worklist for m in core_modules):
  164. worklist += list(reversed(core_modules)) * CORE_WARMUP
  165. final_iteration = False
  166. iteration = 0
  167. analyzer = state.manager.semantic_analyzer
  168. analyzer.deferral_debug_context.clear()
  169. while worklist:
  170. iteration += 1
  171. if iteration > MAX_ITERATIONS:
  172. # Just pick some module inside the current SCC for error context.
  173. assert state.tree is not None
  174. with analyzer.file_context(state.tree, state.options):
  175. analyzer.report_hang()
  176. break
  177. if final_iteration:
  178. # Give up. It's impossible to bind all names.
  179. state.manager.incomplete_namespaces.clear()
  180. all_deferred: list[str] = []
  181. any_progress = False
  182. while worklist:
  183. next_id = worklist.pop()
  184. state = graph[next_id]
  185. assert state.tree is not None
  186. deferred, incomplete, progress = semantic_analyze_target(
  187. next_id, next_id, state, state.tree, None, final_iteration, patches
  188. )
  189. all_deferred += deferred
  190. any_progress = any_progress or progress
  191. if not incomplete:
  192. state.manager.incomplete_namespaces.discard(next_id)
  193. if final_iteration:
  194. assert not all_deferred, "Must not defer during final iteration"
  195. # Reverse to process the targets in the same order on every iteration. This avoids
  196. # processing the same target twice in a row, which is inefficient.
  197. worklist = list(reversed(all_deferred))
  198. final_iteration = not any_progress
  199. def process_functions(graph: Graph, scc: list[str], patches: Patches) -> None:
  200. # Process functions.
  201. for module in scc:
  202. tree = graph[module].tree
  203. assert tree is not None
  204. analyzer = graph[module].manager.semantic_analyzer
  205. # In principle, functions can be processed in arbitrary order,
  206. # but _methods_ must be processed in the order they are defined,
  207. # because some features (most notably partial types) depend on
  208. # order of definitions on self.
  209. #
  210. # There can be multiple generated methods per line. Use target
  211. # name as the second sort key to get a repeatable sort order on
  212. # Python 3.5, which doesn't preserve dictionary order.
  213. targets = sorted(get_all_leaf_targets(tree), key=lambda x: (x[1].line, x[0]))
  214. for target, node, active_type in targets:
  215. assert isinstance(node, (FuncDef, OverloadedFuncDef, Decorator))
  216. process_top_level_function(
  217. analyzer, graph[module], module, target, node, active_type, patches
  218. )
  219. def process_top_level_function(
  220. analyzer: SemanticAnalyzer,
  221. state: State,
  222. module: str,
  223. target: str,
  224. node: FuncDef | OverloadedFuncDef | Decorator,
  225. active_type: TypeInfo | None,
  226. patches: Patches,
  227. ) -> None:
  228. """Analyze single top-level function or method.
  229. Process the body of the function (including nested functions) again and again,
  230. until all names have been resolved (or iteration limit reached).
  231. """
  232. # We need one more iteration after incomplete is False (e.g. to report errors, if any).
  233. final_iteration = False
  234. incomplete = True
  235. # Start in the incomplete state (no missing names will be reported on first pass).
  236. # Note that we use module name, since functions don't create qualified names.
  237. deferred = [module]
  238. analyzer.deferral_debug_context.clear()
  239. analyzer.incomplete_namespaces.add(module)
  240. iteration = 0
  241. while deferred:
  242. iteration += 1
  243. if iteration == MAX_ITERATIONS:
  244. # Just pick some module inside the current SCC for error context.
  245. assert state.tree is not None
  246. with analyzer.file_context(state.tree, state.options):
  247. analyzer.report_hang()
  248. break
  249. if not (deferred or incomplete) or final_iteration:
  250. # OK, this is one last pass, now missing names will be reported.
  251. analyzer.incomplete_namespaces.discard(module)
  252. deferred, incomplete, progress = semantic_analyze_target(
  253. target, module, state, node, active_type, final_iteration, patches
  254. )
  255. if final_iteration:
  256. assert not deferred, "Must not defer during final iteration"
  257. if not progress:
  258. final_iteration = True
  259. analyzer.incomplete_namespaces.discard(module)
  260. # After semantic analysis is done, discard local namespaces
  261. # to avoid memory hoarding.
  262. analyzer.saved_locals.clear()
  263. TargetInfo: _TypeAlias = Tuple[
  264. str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]
  265. ]
  266. def get_all_leaf_targets(file: MypyFile) -> list[TargetInfo]:
  267. """Return all leaf targets in a symbol table (module-level and methods)."""
  268. result: list[TargetInfo] = []
  269. for fullname, node, active_type in file.local_definitions():
  270. if isinstance(node.node, (FuncDef, OverloadedFuncDef, Decorator)):
  271. result.append((fullname, node.node, active_type))
  272. return result
  273. def semantic_analyze_target(
  274. target: str,
  275. module: str,
  276. state: State,
  277. node: MypyFile | FuncDef | OverloadedFuncDef | Decorator,
  278. active_type: TypeInfo | None,
  279. final_iteration: bool,
  280. patches: Patches,
  281. ) -> tuple[list[str], bool, bool]:
  282. """Semantically analyze a single target.
  283. Return tuple with these items:
  284. - list of deferred targets
  285. - was some definition incomplete (need to run another pass)
  286. - were any new names defined (or placeholders replaced)
  287. """
  288. state.manager.processed_targets.append((module, target))
  289. tree = state.tree
  290. assert tree is not None
  291. analyzer = state.manager.semantic_analyzer
  292. # TODO: Move initialization to somewhere else
  293. analyzer.global_decls = [set()]
  294. analyzer.nonlocal_decls = [set()]
  295. analyzer.globals = tree.names
  296. analyzer.progress = False
  297. with state.wrap_context(check_blockers=False):
  298. refresh_node = node
  299. if isinstance(refresh_node, Decorator):
  300. # Decorator expressions will be processed as part of the module top level.
  301. refresh_node = refresh_node.func
  302. analyzer.refresh_partial(
  303. refresh_node,
  304. patches,
  305. final_iteration,
  306. file_node=tree,
  307. options=state.options,
  308. active_type=active_type,
  309. )
  310. if isinstance(node, Decorator):
  311. infer_decorator_signature_if_simple(node, analyzer)
  312. for dep in analyzer.imports:
  313. state.add_dependency(dep)
  314. priority = mypy.build.PRI_LOW
  315. if priority <= state.priorities.get(dep, priority):
  316. state.priorities[dep] = priority
  317. # Clear out some stale data to avoid memory leaks and astmerge
  318. # validity check confusion
  319. analyzer.statement = None
  320. del analyzer.cur_mod_node
  321. if analyzer.deferred:
  322. return [target], analyzer.incomplete, analyzer.progress
  323. else:
  324. return [], analyzer.incomplete, analyzer.progress
  325. def check_type_arguments(graph: Graph, scc: list[str], errors: Errors) -> None:
  326. for module in scc:
  327. state = graph[module]
  328. assert state.tree
  329. analyzer = TypeArgumentAnalyzer(
  330. errors,
  331. state.options,
  332. is_typeshed_file(state.options.abs_custom_typeshed_dir, state.path or ""),
  333. )
  334. with state.wrap_context():
  335. with mypy.state.state.strict_optional_set(state.options.strict_optional):
  336. state.tree.accept(analyzer)
  337. def check_type_arguments_in_targets(
  338. targets: list[FineGrainedDeferredNode], state: State, errors: Errors
  339. ) -> None:
  340. """Check type arguments against type variable bounds and restrictions.
  341. This mirrors the logic in check_type_arguments() except that we process only
  342. some targets. This is used in fine grained incremental mode.
  343. """
  344. analyzer = TypeArgumentAnalyzer(
  345. errors,
  346. state.options,
  347. is_typeshed_file(state.options.abs_custom_typeshed_dir, state.path or ""),
  348. )
  349. with state.wrap_context():
  350. with mypy.state.state.strict_optional_set(state.options.strict_optional):
  351. for target in targets:
  352. func: FuncDef | OverloadedFuncDef | None = None
  353. if isinstance(target.node, (FuncDef, OverloadedFuncDef)):
  354. func = target.node
  355. saved = (state.id, target.active_typeinfo, func) # module, class, function
  356. with errors.scope.saved_scope(saved) if errors.scope else nullcontext():
  357. analyzer.recurse_into_functions = func is not None
  358. target.node.accept(analyzer)
  359. def apply_class_plugin_hooks(graph: Graph, scc: list[str], errors: Errors) -> None:
  360. """Apply class plugin hooks within a SCC.
  361. We run these after to the main semantic analysis so that the hooks
  362. don't need to deal with incomplete definitions such as placeholder
  363. types.
  364. Note that some hooks incorrectly run during the main semantic
  365. analysis pass, for historical reasons.
  366. """
  367. num_passes = 0
  368. incomplete = True
  369. # If we encounter a base class that has not been processed, we'll run another
  370. # pass. This should eventually reach a fixed point.
  371. while incomplete:
  372. assert num_passes < 10, "Internal error: too many class plugin hook passes"
  373. num_passes += 1
  374. incomplete = False
  375. for module in scc:
  376. state = graph[module]
  377. tree = state.tree
  378. assert tree
  379. for _, node, _ in tree.local_definitions():
  380. if isinstance(node.node, TypeInfo):
  381. if not apply_hooks_to_class(
  382. state.manager.semantic_analyzer,
  383. module,
  384. node.node,
  385. state.options,
  386. tree,
  387. errors,
  388. ):
  389. incomplete = True
  390. def apply_hooks_to_class(
  391. self: SemanticAnalyzer,
  392. module: str,
  393. info: TypeInfo,
  394. options: Options,
  395. file_node: MypyFile,
  396. errors: Errors,
  397. ) -> bool:
  398. # TODO: Move more class-related hooks here?
  399. defn = info.defn
  400. ok = True
  401. for decorator in defn.decorators:
  402. with self.file_context(file_node, options, info):
  403. hook = None
  404. decorator_name = self.get_fullname_for_hook(decorator)
  405. if decorator_name:
  406. hook = self.plugin.get_class_decorator_hook_2(decorator_name)
  407. # Special case: if the decorator is itself decorated with
  408. # typing.dataclass_transform, apply the hook for the dataclasses plugin
  409. # TODO: remove special casing here
  410. if hook is None and find_dataclass_transform_spec(decorator):
  411. hook = dataclasses_plugin.dataclass_class_maker_callback
  412. if hook:
  413. ok = ok and hook(ClassDefContext(defn, decorator, self))
  414. # Check if the class definition itself triggers a dataclass transform (via a parent class/
  415. # metaclass)
  416. spec = find_dataclass_transform_spec(info)
  417. if spec is not None:
  418. with self.file_context(file_node, options, info):
  419. # We can't use the normal hook because reason = defn, and ClassDefContext only accepts
  420. # an Expression for reason
  421. ok = ok and dataclasses_plugin.DataclassTransformer(defn, defn, spec, self).transform()
  422. return ok
  423. def calculate_class_properties(graph: Graph, scc: list[str], errors: Errors) -> None:
  424. builtins = graph["builtins"].tree
  425. assert builtins
  426. for module in scc:
  427. state = graph[module]
  428. tree = state.tree
  429. assert tree
  430. for _, node, _ in tree.local_definitions():
  431. if isinstance(node.node, TypeInfo):
  432. with state.manager.semantic_analyzer.file_context(tree, state.options, node.node):
  433. calculate_class_abstract_status(node.node, tree.is_stub, errors)
  434. check_protocol_status(node.node, errors)
  435. calculate_class_vars(node.node)
  436. add_type_promotion(
  437. node.node, tree.names, graph[module].options, builtins.names
  438. )
  439. def check_blockers(graph: Graph, scc: list[str]) -> None:
  440. for module in scc:
  441. graph[module].check_blockers()