Skip to content

Commit

Permalink
Add more tests; move into redefines
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 23, 2024
1 parent 43070c1 commit 21ff0c6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
24 changes: 24 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F811_30.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,27 @@ def foo(self) -> None:

def bar(self) -> None:
"""Bar."""


class B:
"""B."""
def baz(self) -> None:
"""Baz."""

baz = 1


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

bar = (foo := 1)


class D:
"""D."""
foo = 1
foo = 2
bar = (foo := 3)
bar = (foo := 4)
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ F811_30.py:12:9: F811 Redefinition of unused `bar` from line 10
13 | """Bar."""
|
= help: Remove definition: `bar`

F811_30.py:21:5: F811 Redefinition of unused `baz` from line 18
|
19 | """Baz."""
20 |
21 | baz = 1
| ^^^ F811
|
= help: Remove definition: `baz`

F811_30.py:29:12: F811 Redefinition of unused `foo` from line 26
|
27 | """Foo."""
28 |
29 | bar = (foo := 1)
| ^^^ F811
|
= help: Remove definition: `foo`
14 changes: 14 additions & 0 deletions crates/ruff_python_semantic/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ impl<'a> Binding<'a> {
| BindingKind::Builtin => {
return false;
}
// Assignment-assignment bindings are not considered redefinitions, as in:
// ```python
// x = 1
// x = 2
// ```
BindingKind::Assignment | BindingKind::NamedExprAssignment => {
if matches!(
existing.kind,
BindingKind::Assignment | BindingKind::NamedExprAssignment
) {
return false;
}
}
_ => {}
}
// Otherwise, the shadowed binding must be a class definition, function definition,
Expand All @@ -188,6 +201,7 @@ impl<'a> Binding<'a> {
| BindingKind::Import(_)
| BindingKind::FromImport(_)
| BindingKind::Assignment
| BindingKind::NamedExprAssignment
)
}

Expand Down

0 comments on commit 21ff0c6

Please sign in to comment.