Commit 03c7a5a
TypeForm: Early-reject Var-with-concrete-Instance-type in try_parse_as_type_expression()
Add a fast-rejection filter to SemanticAnalyzer.try_parse_as_type_expression():
a string literal that is an identifier naming a Var whose declared type is a
concrete Instance (and is not a typing special form) is a value -- a local,
parameter, or module-level constant -- never a type expression. Reject it
before the expensive full-parse block (expr_to_analyzed_type +
isolated_error_analysis).
On the mypy self-check this filter rejects 157 of the 381 identifier-string
literals that currently reach the full-parse block (e.g. "__doc__", "__name__",
enum/constant members like "ROUND_DOWN", "GEN_CREATED"), all of which were
failing full parses -- pure wasted work.
Insertion point chosen empirically. The filter is placed AFTER the existing
PlaceholderNode and unbound-tvar checks rather than before them. Its only
expensive conjunct (get_proper_type(node.type)) runs solely for Var nodes, and
all Var nodes already survive both earlier checks, so position cannot change how
often the expensive part runs -- only how often the cheap isinstance(node, Var)
conjunct is evaluated. Because 951 of the identifier-strings reaching this block
are unbound type variables, evaluating the filter before the tvar check would
force an extra isinstance onto ~951 nodes it can never catch. perf_compare.py
(--metric cpu --workers1 --num-runs 100) confirms: paired median vs baseline was
-15.6ms +/-4.6 here, vs -12.9ms (before placeholder) and -9.3ms (before tvar) --
matching the eval-count model's predicted ordering.
Also add typing.Self / typing_extensions.Self to var_is_typing_special_form().
Self is a _SpecialForm-typed Var, so without this guard the new filter would
wrongly reject a stringified "Self" type annotation (regressing
testSelfRecognizedInOtherSyntacticLocations). This guard is a correctness
prerequisite of the filter and is committed together with it.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent c2ae176 commit 03c7a5a
1 file changed
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8116 | 8116 | | |
8117 | 8117 | | |
8118 | 8118 | | |
| 8119 | + | |
| 8120 | + | |
| 8121 | + | |
| 8122 | + | |
| 8123 | + | |
| 8124 | + | |
| 8125 | + | |
| 8126 | + | |
| 8127 | + | |
| 8128 | + | |
8119 | 8129 | | |
8120 | 8130 | | |
8121 | 8131 | | |
| |||
8208 | 8218 | | |
8209 | 8219 | | |
8210 | 8220 | | |
| 8221 | + | |
| 8222 | + | |
8211 | 8223 | | |
8212 | 8224 | | |
8213 | 8225 | | |
| |||
0 commit comments