Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/narrow.rs (Rust)

Code before:
        // Importantly, `my_typeddict_union["tag"]` isn't the place we're going to constraint.
        // Instead, we're going to constrain `my_typeddict_union` itself.
        if matches!(&**ops, [ast::CmpOp::Eq | ast::CmpOp::NotEq]) {
            // For `==`, we use equality semantics on the `if` branch (is_positive=true).
            // For `!=`, we use equality semantics on the `else` branch (is_positive=false).
            let constrain_with_equality = is_positive == (ops[0] == ast::CmpOp::Eq);

            // Check if left is a subscript.
            if let ast::Expr::Subscript(subscript) = &**left {
                if let Some((place, constraint)) = self.narrow_typeddict_subscript(
                    inference.expression_type(&*subscript.value),
                    &subscript.value,
                    inference.expression_type(&*subscript.slice),
                    inference.expression_type(&comparators[0]),
                    constrain_with_equality,
                ) {
                    constraints
                        .entry(place)
                        .and_modify(|existing| {
                            *existing = existing.merge_constraint_and(constraint.clone(), self.db);
                        })
                        .or_insert(constraint);
                }

                // Narrow tagged unions of tuples with `Literal` elements.
                if let Some((place, constraint)) = self.narrow_tuple_subscript(
                    inference.expression_type(&*subscript.value),
                    &subscript.value,
                    inference.expression_type(&*subscript.slice),
                    inference.expression_type(&comparators[0]),
                    constrain_with_equality,
                ) {
                    constraints
                        .entry(place)
                        .and_modify(|existing| {
                            *existing = existing.merge_constraint_and(constraint.clone(), self.db);
                        })
                        .or_insert(constraint);
                }
            }

            // Check if right (comparator) is a subscript.
            if let ast::Expr::Subscript(subscript) = &comparators[0] {
                if let Some((place, constraint)) = self.narrow_typeddict_subscript(
                    inference.expression_type(&*subscript.value),
                    &subscript.value,
                    inference.expression_type(&*subscript.slice),
                    inference.expression_type(&**left),
                    constrain_with_equality,
                ) {
                    constraints

Review comment (suggestion):
should this be


```suggestion
                else if let Some((place, constraint)) = self.narrow_tuple_subscript(
```

since I don't think we ever want to apply `TypedDict`-tagged-union narrowing _and_ tuple-tagged-union-narrowing simultaneously 😄

Code after:
        // Importantly, `my_typeddict_union["tag"]` isn't the place we're going to constraint.
        // Instead, we're going to constrain `my_typeddict_union` itself.
        if matches!(&**ops, [ast::CmpOp::Eq | ast::CmpOp::NotEq]) {
            // For `==`, we use equality semantics on the `if` branch (is_positive=true).
            // For `!=`, we use equality semantics on the `else` branch (is_positive=false).
            let constrain_with_equality = is_positive == (ops[0] == ast::CmpOp::Eq);

            let mut narrow_subscript = |subscript: &ast::ExprSubscript, other_type: Type<'db>| {
                let value_type = inference.expression_type(&*subscript.value);
                let slice_type = inference.expression_type(&*subscript.slice);

                if let Some((place, constraint)) = self.narrow_typeddict_subscript(
                    value_type,
                    &subscript.value,
                    slice_type,
                    other_type,
                    constrain_with_equality,
                ) {
                    constraints
                        .entry(place)
                        .and_modify(|existing| {
                            *existing = existing.merge_constraint_and(constraint.clone(), self.db);
                        })
                        .or_insert(constraint);
                } else if let Some((place, constraint)) = self.narrow_tuple_subscript(
                    value_type,
                    &subscript.value,
                    slice_type,
                    other_type,
                    constrain_with_equality,
                ) {
                    constraints
                        .entry(place)
                        .and_modify(|existing| {
                            *existing = existing.merge_constraint_and(constraint.clone(), self.db);
                        })
                        .or_insert(constraint);
                }
            };

            if let ast::Expr::Subscript(subscript) = &**left {
                narrow_subscript(subscript, inference.expression_type(&comparators[0]));
            }

            if let ast::Expr::Subscript(subscript) = &comparators[0] {
                narrow_subscript(subscript, inference.expression_type(&**left));
            }
        }

        // Narrow unions and intersections of `TypedDict` in cases where required keys are
        // excluded: