Skip to content

[ty] Intersection simplifications with gradual generic specializations#26365

Draft
sharkdp wants to merge 2 commits into
mainfrom
david/more-gradual-generics-simplifications
Draft

[ty] Intersection simplifications with gradual generic specializations#26365
sharkdp wants to merge 2 commits into
mainfrom
david/more-gradual-generics-simplifications

Conversation

@sharkdp

@sharkdp sharkdp commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

In isinstance(..., C) and TypeIs[..] narrowing, we currently intersect with the top materialization Top[C[Unknown]] of a generic type C. If we want to relax that to an intersection with C[Unknown] directly, then we will not just see intersections like C[P] & C[Unknown] in the if branch, we'll also see intersections like C[P] & ~C[Unknown] in the else branch. If we don't simplify/transform these intersections further, we are currently unable to infer that the bottom intersection of something like list[str] & ~list[Unknown] is Never. So here, we implement the following intersection transformation:

# For C a co-, contra-, or invariant generic class:
C[P] & ~C[Unknown] = C[P] & ~Bottom[C[Unknown]] & Unknown

In this presentation, due to the intersection with a plain Unknown, it is now obvious to ty that the gradual type extends all the way down to Never.

In addition, we also implement these transformations that we have derived before:

Co[P] & Co[Unknown] = Co[P & Unknown]
Contra[P] & Contra[Unknown] = Contra[P | Unknown]

Test Plan

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Jun 25, 2026
@astral-sh-bot

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

@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@sharkdp sharkdp force-pushed the david/more-gradual-generics-simplifications branch from a4de703 to 8dbad3a Compare June 25, 2026 17:59
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.

1 participant