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

Code before:
            // without worrying about what types are present.
            if !constrain_with_equality
                || all_matching_typeddict_fields_have_literal_types(
                    self.db,
                    lhs_value_type,
                    key_literal.value(self.db),
                )
            {
                let field_name = Name::from(key_literal.value(self.db));
                let rhs_type = inference.expression_type(&comparators[0]);
                let field_type = match (ops[0], is_positive) {
                    // To avoid excluding non-`TypedDict` types, our constraints are always
                    // expressed as a negative intersection (i.e. "you're *not* this kind of
                    // `TypedDict`"). In these two cases, the whole constraint is going to be a
                    // double negative, i.e. "you're *not* a `TypedDict` *without* this literal
                    // field". As the first step of building that, we negate the right hand side.
                    (ast::CmpOp::Eq, true) | (ast::CmpOp::NotEq, false) => {
                        IntersectionBuilder::new(self.db)
                            .add_negative(rhs_type)
                            .build()
                    }
                    // The opposite, i.e. "you're *not* a `TypedDict` *with* this literal field".
                    // Don't negate the right hand side.
                    (ast::CmpOp::Eq, false) | (ast::CmpOp::NotEq, true) => rhs_type,
                    _ => unreachable!(),
                };
                // Create the synthesized `TypedDict` with that (possibly negated) field. We don't
                // want to constrain the mutability or required-ness of the field, so the most
                // compatible form is not-required and read-only.
                let field = TypedDictFieldBuilder::new(field_type)
                    .required(false)
                    .read_only(true)
                    .build();
                let schema: TypedDictSchema = std::iter::once((field_name, field)).collect();
                let synthesized_typeddict =
                    TypedDictType::Synthesized(SynthesizedTypedDictType::new(self.db, schema));
                // As mentioned above, the synthesized `TypedDict` is always negated.
                let intersection = IntersectionBuilder::new(self.db)
                    .add_negative(Type::TypedDict(synthesized_typeddict))
                    .build();
                let place = self.expect_place(&subscript_place_expr);
                constraints.insert(place, intersection);
            }
        }

        let mut last_rhs_ty: Option<Type> = None;

        for (op, (left, right)) in std::iter::zip(&**ops, comparator_tuples) {
            let lhs_ty = last_rhs_ty.unwrap_or_else(|| inference.expression_type(left));
            let rhs_ty = inference.expression_type(right);
            last_rhs_ty = Some(rhs_ty);

Review comment (suggestion):
```suggestion
                // To avoid excluding non-`TypedDict` types, our constraints are always
                // expressed as a negative intersection (i.e. "you're *not* this kind of
                // `TypedDict`"). If `constrain_with_equality` is true, the whole constraint
                // is going to be a double negative, i.e. "you're *not* a `TypedDict`
                // *without* this literal
                // field". As the first step of building that, we negate the right hand side.
                let field_type = rhs_type.negate_if(self.db, constrain_with_equality);
```

Code after:
            // However, if any of them are e.g. `int` or `str` or some random class, then we can't
            // narrow their type at all, because subclasses of those types can implement `__eq__`
            // in any perverse way they like. On the other hand, if this is an *inequality*
            // constraint, then we can go ahead and assert "you can't be this exact literal type"
            // without worrying about what other types might be present.
            let constrain_with_equality = is_positive == (ops[0] == ast::CmpOp::Eq);
            if !constrain_with_equality
                || all_matching_typeddict_fields_have_literal_types(
                    self.db,
                    lhs_value_type,
                    key_literal.value(self.db),
                )
            {
                let field_name = Name::from(key_literal.value(self.db));
                let rhs_type = inference.expression_type(&comparators[0]);
                // To avoid excluding non-`TypedDict` types, our constraints are always expressed
                // as a negative intersection (i.e. "you're *not* this kind of `TypedDict`"). If
                // `constrain_with_equality` is true, the whole constraint is going to be a double
                // negative, i.e. "you're *not* a `TypedDict` *without* this literal field". As the
                // first step of building that, we negate the right hand side.
                let field_type = rhs_type.negate_if(self.db, constrain_with_equality);
                // Create the synthesized `TypedDict` with that (possibly negated) field. We don't
                // want to constrain the mutability or required-ness of the field, so the most
                // compatible form is not-required and read-only.
                let field = TypedDictFieldBuilder::new(field_type)
                    .required(false)
                    .read_only(true)
                    .build();
                let schema = TypedDictSchema::from_iter([(field_name, field)]);
                let synthesized_typeddict =
                    TypedDictType::Synthesized(SynthesizedTypedDictType::new(self.db, schema));
                // As mentioned above, the synthesized `TypedDict` is always negated.
                let intersection = Type::TypedDict(synthesized_typeddict).negate(self.db);
                let place = self.expect_place(&subscript_place_expr);
                constraints.insert(place, intersection);
            }
        }

        let mut last_rhs_ty: Option<Type> = None;

        for (op, (left, right)) in std::iter::zip(&**ops, comparator_tuples) {
            let lhs_ty = last_rhs_ty.unwrap_or_else(|| inference.expression_type(left));
            let rhs_ty = inference.expression_type(right);
            last_rhs_ty = Some(rhs_ty);

            match left {
                ast::Expr::Name(_)
                | ast::Expr::Attribute(_)
                | ast::Expr::Subscript(_)
                | ast::Expr::Named(_) => {
                    if let Some(left) = place_expr(left)