Skip to content

Commit

Permalink
fix(F811): assignment can also be shadowed binding
Browse files Browse the repository at this point in the history
  • Loading branch information
ukyen8 committed Jun 21, 2024
1 parent a26bd01 commit f034623
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
13 changes: 13 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F811_30.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Regression test for: https://github.com/astral-sh/ruff/issues/11828"""


class A:
"""A."""

def foo(self) -> None:
"""Foo."""

bar = foo

def bar(self) -> None:
"""Bar."""
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
continue;
};

// Ignore non-import redefinitions
if matches!(shadowed.kind, BindingKind::Assignment)
&& matches!(binding.kind, BindingKind::Assignment)
{
continue;
}

// If this is an overloaded function, abort.
if shadowed.kind.is_function_definition() {
if checker
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mod tests {
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_27.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_28.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_29.pyi"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_30.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_0.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_1.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_2.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_30.py:12:9: F811 Redefinition of unused `bar` from line 10
|
10 | bar = foo
11 |
12 | def bar(self) -> None:
| ^^^ F811
13 | """Bar."""
|
= help: Remove definition: `bar`
5 changes: 3 additions & 2 deletions crates/ruff_python_semantic/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,15 @@ impl<'a> Binding<'a> {
}
_ => {}
}
// Otherwise, the shadowed binding must be a class definition, function definition, or
// import to be considered a redefinition.
// Otherwise, the shadowed binding must be a class definition, function definition,
// import, or assignment to be considered a redefinition.
matches!(
existing.kind,
BindingKind::ClassDefinition(_)
| BindingKind::FunctionDefinition(_)
| BindingKind::Import(_)
| BindingKind::FromImport(_)
| BindingKind::Assignment
)
}

Expand Down

0 comments on commit f034623

Please sign in to comment.