ruff
20bce487 - [ty] Reuse runtime operands in eager union annotations (#29770)

Commit
19 days ago
[ty] Reuse runtime operands in eager union annotations (#29770) Checking an eager annotation such as `A | B | C` can involve looking up the same name twice. Ty needs both the type that the annotation denotes and the runtime value on which Python calls `|`. In longer annotations, it can also evaluate an inner union again while checking its parent. This change reuses the runtime values of bare names, `None`, and nested unions within the same eager annotation. A nested union computes its runtime value only when the enclosing `|` can use it under the same inference flags. Each expression still records its annotation type. For example, `bool | int` denotes `int` but evaluates to a union object, which matters for a subsequent `| None`. This addresses the eager-annotation case from #29312 on top of #29727. ## Test plan Measured CPU time for fresh, single-core `ty check` processes over 12 interleaved rounds. The comparison uses main at `18c5d27c41` and the eager operand changes from #29312 rebased onto the same commit. Times are medians in milliseconds for each whole input file. | Input | Main | Rebased #29312 | This change | | --- | ---: | ---: | ---: | | Three ordinary classes, 1,500 signatures | 58.5 | 59.3 | 56.6 | | Twelve ordinary classes, 450 signatures | 86.7 | 72.5 | 69.4 | | Twelve specialized generics, 350 signatures | 144.5 | 100.3 | 100.0 | | Direct `TypeForm` arguments | 113.9 | 98.3 | 91.7 | The source-only checks of NumPy’s `_typing` package and Pydantic showed no clear difference. Against rebased #29312, the stored 256-member `TypeForm` case used 1.0% more CPU time, and the `gradual_literal_union_equality` Criterion benchmark took 0.9% longer. Compared the inferred types of all 14 AST nodes in a pair of left- and right-nested annotations. Both alternatives and main yielded the same type at every node.
Author
Parents
Loading