Latest Results
[ty] Add first-class support for enum complements (#24961)
## Summary
Previously, narrowing an enum by excluding one or more members eagerly
expanded the enum into a union of all remaining enum literals. That's
fine for small enums, but can cause large enums to blow up during
repeated equality or membership narrowing. And large enums are not
uncommon...
This PR keeps those narrowed types in a compact form, like `Color &
~Literal[Color.RED]`, instead of immediately expanding them to
`Literal[Color.GREEN, Color.BLUE, ...]`. The type is expanded only when
we need to inspect the remaining enum members individually (like for
overload matching or attribute lookup). The net effect is that we retain
the existing behavior while avoiding a brutal performance penalty for
large enums.
Note that we do vary some behavior by enum size. For example, with more
than 7 elements, we use (as you can see in the ecosystem diff) the
compact representation (`SupportedBlockchain &
~Literal[SupportedBlockchain.ETHEREUM_BEACONCHAIN`).
For the benchmark in the linked issue, `main` doesn't finish within 30s,
but this PR completes in 128ms.
Closes https://github.com/astral-sh/ty/issues/1588.
---------
Co-authored-by: Codex <noreply@openai.com> Latest Branches
0%
anishgirianish:ruf071-incorrect-decorator-order 0%
InvalidPathException:main 0%
© 2026 CodSpeed Technology