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

Code before:
                        .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
                        .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(&**left),
                    constrain_with_equality,
                ) {
                    constraints
                        .entry(place)
                        .and_modify(|existing| {

Review comment (refactor):
is this branch under `if let ast::Expr::Subscript(subscript) = &comparators[0] {` not exactly the same code as the branch under `if let ast::Expr::Subscript(subscript) = &**left {` above? Is there no way of factoring out a helper function or closure here?

Code after:
                    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:
        //
        // class Foo(TypedDict):
        //     foo: int
        // class Bar(TypedDict):
        //     bar: int
        //
        // def _(u: Foo | Bar):
        //     if "foo" not in u:
        //         reveal_type(u)  # revealed: Bar
        if matches!(&**ops, [ast::CmpOp::In | ast::CmpOp::NotIn])
            && let Type::StringLiteral(key) = inference.expression_type(&**left)
            && let Some(rhs_place_expr) = PlaceExpr::try_from_expr(&comparators[0])
            && let rhs_type = inference.expression_type(&comparators[0])
            && is_or_contains_typeddict(self.db, rhs_type)
        {
            let is_negative_check = is_positive == (ops[0] == ast::CmpOp::NotIn);
            if is_negative_check {
                let requires_key = |td: TypedDictType<'db>| -> bool {