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

Code before:
        );

        let comprehension_symbol_table = index.place_table(comprehension_scope_id);

        assert_eq!(names(comprehension_symbol_table), vec!["x", "y"]);

        let use_def = index.use_def_map(comprehension_scope_id);
        for name in ["x", "y"] {
            let binding = use_def
                .first_public_binding(
                    comprehension_symbol_table
                        .symbol_id(name)
                        .expect("symbol exists"),
                )
                .unwrap();
            assert!(matches!(
                binding.kind(&db),
                DefinitionKind::Comprehension(_)
            ));
        }
    }

    /// Test case to validate that comprehensions inside a method are correctly marked as attribute
    /// scopes
    #[test]
    fn comprehension_in_method_instance_attribute() {
        let TestCase { db, file } = test_case(
            "
class C:
    def __init__(self):
        [None for self.z in range(1)]
",
        );

        let index = semantic_index(&db, file);

        let [(class_scope_id, class_scope)] = index
            .child_scopes(FileScopeId::global())
            .collect::<Vec<_>>()[..]
        else {
            panic!("expected one child scope (the class)")
        };

        assert_eq!(class_scope.kind(), ScopeKind::Class);
        let class_scope = class_scope_id.to_scope_id(&db, file);

        let method_scopes: Vec<_> = index.child_scopes(class_scope_id).collect();
        assert_eq!(method_scopes.len(), 1, "expected __init__");
        let (method_scope_id, method_scope) = method_scopes[0];
        assert_eq!(method_scope.kind(), ScopeKind::Function);


Review comment (refactor):
It's not clear to me that this test and the below test couldn't be equally effective as mdtests (and/or aren't already effectively duplicated by mdtests in this PR.) But these seem harder to maintain. Do you feel like they are important?

Code after:

        assert_eq!(names(comprehension_symbol_table), vec!["x", "y"]);

        let use_def = index.use_def_map(comprehension_scope_id);
        for name in ["x", "y"] {
            let binding = use_def
                .first_public_binding(
                    comprehension_symbol_table
                        .symbol_id(name)
                        .expect("symbol exists"),
                )
                .unwrap();
            assert!(matches!(
                binding.kind(&db),
                DefinitionKind::Comprehension(_)
            ));
        }
    }

    /// Test case to validate that the `x` variable used in the comprehension is referencing the
    /// `x` variable defined by the inner generator (`for x in iter2`) and not the outer one.
    #[test]
    fn multiple_generators() {
        let TestCase { db, file } = test_case(
            "
[x for x in iter1 for x in iter2]
",
        );

        let index = semantic_index(&db, file);
        let [(comprehension_scope_id, _)] = index
            .child_scopes(FileScopeId::global())
            .collect::<Vec<_>>()[..]
        else {
            panic!("expected one child scope")
        };

        let use_def = index.use_def_map(comprehension_scope_id);

        let module = parsed_module(&db, file).load(&db);
        let syntax = module.syntax();
        let element = syntax.body[0]
            .as_expr_stmt()
            .unwrap()
            .value
            .as_list_comp_expr()
            .unwrap()
            .elt
            .as_name_expr()
            .unwrap();
        let element_use_id =