[ty] Eagerly evaluate types.UnionType elements as type expressions by sharkdp · Pull Request #21531 · astral-sh/ruff (original) (raw)

Summary

Eagerly evaluate the elements of a PEP 604 union in value position (e.g. IntOrStr = int | str) as type expressions and store the result (the corresponding Type::Union if all elements are valid type expressions, or the first encountered InvalidTypeExpressionError) on the UnionTypeInstance, such that the Type::Union(…) does not need to be recomputed every time the implicit type alias is used in a type annotation.

This might lead to performance improvements for large unions, but is also necessary for correctness, because the elements of the union might refer to type variables that need to be looked up in the scope of the type alias, not at the usage site.

Test Plan

New Markdown tests