mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-05 08:00:27 +00:00
![]() ## Summary The union `T | U` can be validly simplified to `U` iff: 1. `T` is a subtype of `U` OR 2. `T` is equivalent to `U` OR 3. `U` is a union and contains a type that is equivalent to `T` OR 4. `T` is an intersection and contains a type that is equivalent to `U` (In practice, the only situation in which 2, 3 or 4 would be true when (1) was not true would be if `T` or `U` is a dynamic type.) Currently we achieve these simplifications in the union builder by doing something along the lines of `t.is_subtype_of(db, u) || t.is_equivalent_to_(db, u) || t.into_intersection().is_some_and(|intersection| intersection.positive(db).contains(&u)) || u.into_union().is_some_and(|union| union.elements(db).contains(&t))`. But this is both slow and misses some cases (it doesn't simplify the union `Any | (Unknown & ~None)` to `Any`, for example). We can improve the consistency and performance of our union simplifications by adding a third type relation that sits in between `TypeRelation::Subtyping` and `TypeRelation::Assignability`: `TypeRelation::UnionSimplification`. This change leads to simpler, more user-friendly types due to the more consistent simplification. It also lead to a pretty huge performance improvement! ## Test Plan Existing tests, plus some new ones. |
||
---|---|---|
.. | ||
resources | ||
src | ||
tests | ||
build.rs | ||
Cargo.toml | ||
mdtest.py | ||
mdtest.py.lock |