Skip to content

Commit 8afda22

Browse files
authored
New semantic analyzer: Document high-level logic (#6245)
1 parent 9fc3229 commit 8afda22

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mypy/newsemanal/semanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
module-level variable (Var) node. The second assignment would also
1414
be analyzed, and the type of 'y' marked as being inferred.
1515
16-
Semantic analysis of types is implemented in module mypy.typeanal.
16+
Semantic analysis of types is implemented in typeanal.py.
17+
18+
See semanal_main.py for the top-level logic.
1719
"""
1820

1921
from contextlib import contextmanager

mypy/newsemanal/semanal_main.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
"""Top-level logic for the new semantic analyzer."""
1+
"""Top-level logic for the new semantic analyzer.
2+
3+
The semantic analyzer binds names, resolves imports, detects various
4+
special constructs that don't have dedicated AST nodes after parse
5+
(such as 'cast' which looks like a call), and performs various simple
6+
consistency checks.
7+
8+
Semantic analysis of each SCC (strongly connected component; import
9+
cycle) is performed in one unit. Each module is analyzed as multiple
10+
separate *targets*; the module top level is one target and each function
11+
is a target. Nested functions are not separate targets, however. This is
12+
mostly identical to targets used by mypy daemon (but classes aren't
13+
targets in semantic analysis).
14+
15+
We first analyze each module top level in an SCC. If we encounter some
16+
names that we can't bind because the target of the name may not have
17+
been processed yet, we *defer* the current target for further
18+
processing. Deferred targets will be analyzed additional times until
19+
everything can be bound, or we reach a maximum number of iterations.
20+
21+
We keep track of a set of incomplete namespaces, i.e. namespaces that we
22+
haven't finished populating yet. References to these namespaces cause a
23+
deferral if they can't be satisfied. Initially every module in the SCC
24+
will be incomplete.
25+
"""
226

327
from typing import List, Tuple, Optional, Union
428

0 commit comments

Comments
 (0)