Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Avoid oscillating collection-use constraints#26031

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/fix-dict-get-cycle-3778
Jun 16, 2026
Merged

[ty] Avoid oscillating collection-use constraints#26031
charliermarsh merged 1 commit into
mainfrom
charlie/fix-dict-get-cycle-3778

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 16, 2026

Copy link
Copy Markdown
Member

Summary

Full-scope collection inference can introduce a Salsa cycle when multiple unannotated collections are constrained by the same container literal:

from typing import Any

def run(cond: bool, d: dict[Any, Any]) -> list[Any]:
    a = {}
    b = {}
    if cond:
        b = d
    return [a.get("x", 0), b.get("x", 0)]

To infer a = {} and b = {}, we collect constraints from later uses such as a.get(...) and b.get(...). This creates a cycle because inferring the literals requires inferring the return statement, while inferring the calls requires resolving the literal definitions.

The conditional assignment makes b's collection-use constraint unstable across fixed-point iterations. Without that constraint, b remains dict[Unknown, Unknown], which exposes a single class specialization at b.get(...) and records the constraint on the next iteration. Once recorded, the literal refines to dict[Unknown, int | Unknown]; joining it with d produces dict[Unknown, int | Unknown] | dict[Any, Any], which no longer exposes a single class specialization, so the constraint disappears again. The constraint map therefore alternates between {a} and {a, b} even after the expression and binding types converge, eventually reaching Salsa's cycle-iteration limit and panicking. The existing cycle-initial values let evaluation begin but do not make this metadata monotonic.

Once the initial tainted iterations have passed, this preserves collection-use constraints from previous iterations across scope, definition, expression, and statement inference. The constraint map now grows monotonically and reaches a fixed point, while definition inference expands compact metadata only when needed and preserves its other retained fields. The regression coverage includes both direct returns and annotated assignments.

Closes astral-sh/ty#3778.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 16, 2026
@charliermarsh charliermarsh added the bug Something isn't working label Jun 16, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 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 94.37%. The percentage of expected errors that received a diagnostic held steady at 89.00%. The number of fully passing files held steady at 94/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 16, 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)

@charliermarsh charliermarsh marked this pull request as ready for review June 16, 2026 02:02
@charliermarsh charliermarsh requested a review from a team as a code owner June 16, 2026 02:02
@charliermarsh charliermarsh merged commit 966bc5e into main Jun 16, 2026
60 checks passed
@astral-sh-bot astral-sh-bot Bot requested a review from dcreager June 16, 2026 02:03
@charliermarsh charliermarsh deleted the charlie/fix-dict-get-cycle-3778 branch June 16, 2026 02:03
@ibraheemdev

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[panic] too many cycle iterations on dict.get(k, default) over a {} | dict union

2 participants