astral-sh
ruff
BlogDocsChangelog

[ty] Fix narrowing for transparent enums (StrEnum, IntEnum)

#22690
Comparing
bxff:fix-enum-custom-eq-narrowing
(
4e503b2
) with
main
(
7c98033
)
CodSpeed Performance Gauge
-94%
Regressions
1
Untouched
22
Skipped
30

Benchmarks

Skipped (30)

Failed

ty_micro[many_enum_members_2]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_many_enum_members_2
Regression
CodSpeed Performance Gauge
-94%
154.4 ms2,402.8 ms

Passed

DateType
crates/ruff_benchmark/benches/ty.rs::project::datetype::project
CodSpeed Performance Gauge
+1%
240.1 ms236.6 ms
attrs
crates/ruff_benchmark/benches/ty.rs::project::attrs::project
CodSpeed Performance Gauge
+1%
438.4 ms434 ms
ty_micro[many_tuple_assignments]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_tuple_implicit_instance_attributes
CodSpeed Performance Gauge
+1%
68.7 ms68 ms
ty_micro[complex_constrained_attributes_2]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_complex_constrained_attributes_2
CodSpeed Performance Gauge
+1%
68.6 ms68 ms
ty_micro[complex_constrained_attributes_1]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_complex_constrained_attributes_1
CodSpeed Performance Gauge
+1%
69 ms68.4 ms
ty_micro[complex_constrained_attributes_3]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_complex_constrained_attributes_3
CodSpeed Performance Gauge
+1%
72.1 ms71.4 ms
ty_micro[many_tuple_assignments]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_many_tuple_assignments
CodSpeed Performance Gauge
+1%
66.9 ms66.3 ms
ty_micro[many_string_assignments]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_many_string_assignments
CodSpeed Performance Gauge
+1%
78 ms77.4 ms
hydra-zen
crates/ruff_benchmark/benches/ty.rs::project::hydra::project
CodSpeed Performance Gauge
+1%
1.2 s1.2 s
ty_check_file[cold]
crates/ruff_benchmark/benches/ty.rs::check_file::benchmark_cold
CodSpeed Performance Gauge
+1%
131.6 ms130.9 ms
ty_micro[many_enum_members]
crates/ruff_benchmark/benches/ty.rs::micro::benchmark_many_enum_members
CodSpeed Performance Gauge
+1%
123.7 ms123 ms
anyio
crates/ruff_benchmark/benches/ty.rs::project::anyio::project
CodSpeed Performance Gauge
0%
1.2 s1.2 s
ty_check_file[incremental]
crates/ruff_benchmark/benches/ty.rs::check_file::benchmark_incremental
CodSpeed Performance Gauge
0%
6.1 ms6.1 ms
colour_science
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
0%
89.1 s89.5 s
sympy
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
51 s51.4 s
pydantic
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
8.2 s8.2 s
tanjun
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
2.5 s2.5 s
pandas
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
63.8 s64.4 s
multithreaded
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
1.1 s1.1 s
altair
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
4.5 s4.6 s
static_frame
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
21.4 s21.7 s
freqtrade
crates/ruff_benchmark/benches/ty_walltime.rs
CodSpeed Performance Gauge
-1%
7.9 s8 s

Commits

Click on a commit to change the comparison range
Base
main
7c98033
-93.57%
[ty] Fix narrowing for transparent enums (StrEnum, IntEnum) ## Summary This PR fixes incorrect type narrowing to Never for enums with transparent equality semantics, such as StrEnum and IntEnum. These enums compare equal to their underlying primitive values at runtime, but the type checker previously treated them as disjoint. The fix introduces a has_transparent_equality helper to identify enums inheriting from primitive types (str, int, or bytes). It then updates disjointness checks and intersection logic to recognize that these enum members are not disjoint from matching primitive literals. This ensures that narrowing in match statements and comparisons correctly preserves the appropriate types instead of collapsing to Never. This directly resolves ty#1454 by improving the core type relation and builder logic. By explicitly detecting transparent enums, this solution is more resilient than relying on overrides_equality which is currently affected by pre-existing lookup policy bugs. Closes https://github.com/astral-sh/ty/issues/1454. ## Test Plan I added a new test file match_enums.md with regression cases for: - StrEnum narrowing in match statements. - Handling of enums with custom __eq__ overrides (narrowing disabled). - Tagged union narrowing with StrEnum in TypedDict fields. I also verified that all existing enum and narrowing tests pass and that there are no regressions in the semantic type checker's unit tests.
4e503b2
3 hours ago
by bxff
© 2026 CodSpeed Technology
Home Terms Privacy Docs