From a03d70069dfd479d8b4d82129e228ff5702f0311 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 30 Jul 2023 18:07:16 -0400 Subject: [PATCH] Avoid falsely marking non-submodules as submodule aliases --- crates/ruff/resources/test/fixtures/pyflakes/F823.py | 7 +++++++ crates/ruff/src/checkers/ast/mod.rs | 9 +++++++++ crates/ruff/src/rules/pyflakes/rules/undefined_local.rs | 2 ++ crates/ruff_python_semantic/src/model.rs | 7 ++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F823.py b/crates/ruff/resources/test/fixtures/pyflakes/F823.py index a17d91f0c735bf..343150143e4425 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F823.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F823.py @@ -63,3 +63,10 @@ def main(): for sys in range(5): pass + + +import requests_mock as rm + + +def requests_mock(requests_mock: rm.Mocker): + print(rm.ANY) diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 442000a3f2c3b6..fc3735a6202ca0 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1539,6 +1539,15 @@ impl<'a> Checker<'a> { return binding_id; } + println!( + "same scope shadowed_id: {:?}", + &self.semantic.bindings[shadowed_id] + ); + println!( + "same scope binding_id: {:?}", + &self.semantic.bindings[binding_id] + ); + // Avoid shadowing builtins. let shadowed = &self.semantic.bindings[shadowed_id]; if !matches!( diff --git a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs index 43c4eda5950ba1..7a3293e35e8caa 100644 --- a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs +++ b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs @@ -65,6 +65,8 @@ pub(crate) fn undefined_local( None } }) { + println!("shadowed: {:?}", shadowed); + println!("binding: {:?}", checker.semantic().binding(binding_id)); // Then it's probably an error. diagnostics.push(Diagnostic::new( UndefinedLocal { diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index 75810ef142bc1b..fc66f668de8cf5 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -594,7 +594,12 @@ impl<'a> SemanticModel<'a> { return None; } - self.scopes[scope_id].get(qualified_name) + let binding_id = self.scopes[scope_id].get(qualified_name)?; + if !self.bindings[binding_id].kind.is_submodule_import() { + return None; + } + + Some(binding_id) } /// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported