Skip to content

[ty] Avoid repeated nested substitutions in path assignments#24660

Merged
dcreager merged 2 commits into
mainfrom
dcreager/avoid-endless-expansion
Apr 15, 2026
Merged

[ty] Avoid repeated nested substitutions in path assignments#24660
dcreager merged 2 commits into
mainfrom
dcreager/avoid-endless-expansion

Conversation

@dcreager

Copy link
Copy Markdown
Member

I discovered this while looking into ecosystem timeouts on #24540. @AlexWaygood also found this while trying to bump our vendored typeshed.

When solving a constraint set that contains multiple typevars, we sometimes try to substitute a nested typevar in a constraint multiple times, leading to an endless expansion.

In #24540, we saw this with streamlit. Given constraints

Iterable[V_co] & int ≤ _T@list
ArrowStreamExportable & str ≤ _T@list
V_co = _T@list

Note that V_co is covariant, and V_co = _T@list, so we can substitute any lower bound of _T@list into Iterable in the first constraint. In particular, we can substitute V_co = ArrowStreamExportable & str, giving

Iterable[ArrowStreamExportable & str] & int ≤ _T@list

That gives us another lower bound for _T@list, which we can substitute again

Iterable[Iterable[ArrowStreamExportable & str] & int] & int ≤ _T@list

and on and on!

To catch this, we now keep track of when a new derived constraint comes from a substitution. When walking paths in a constraint set, we do not add a derived constraint if we already have some other constraint on the path that came from the same substitution. In the streamlit example, that means we will only substitute for V_co in the first constraint at most once per BDD path.

@dcreager dcreager added the ty Multi-file analysis & type inference label Apr 15, 2026
@astral-sh-bot

astral-sh-bot Bot commented Apr 15, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.94%. The percentage of expected errors that received a diagnostic held steady at 83.36%. The number of fully passing files held steady at 79/133.

@astral-sh-bot

astral-sh-bot Bot commented Apr 15, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Apr 15, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@dcreager dcreager marked this pull request as ready for review April 15, 2026 17:08
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh April 15, 2026 17:08
@dcreager dcreager merged commit b41cb9a into main Apr 15, 2026
56 checks passed
@dcreager dcreager deleted the dcreager/avoid-endless-expansion branch April 15, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants