Skip to content

Commit

Permalink
Ignore annotation-only assignment statements in redundant-assignment …
Browse files Browse the repository at this point in the history
…check
  • Loading branch information
david-yz-liu committed Jan 16, 2025
1 parent 31b9659 commit 726b7ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### 🐛 Bug fixes

- Ignore annotation-only assignment statements in `redundant-assignment` check

### 🔧 Internal changes

## [2.9.1] - 2024-12-09
Expand Down
6 changes: 4 additions & 2 deletions python_ta/checkers/redundant_assignment_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def visit_augassign(self, node: nodes.AugAssign) -> None:
@only_required_for_messages("redundant-assignment")
def visit_annassign(self, node: nodes.AnnAssign) -> None:
"""Visit the annotated assign node"""
if node in self._redundant_assignment:
if node.value is not None and node in self._redundant_assignment:
self.add_message("redundant-assignment", node=node, args=node.target.name)

def visit_module(self, node: nodes.Module) -> None:
Expand Down Expand Up @@ -151,7 +151,9 @@ def _transfer(self, block: CFGBlock, out_facts: set[str]) -> set[str]:
parent = (
node.parent.parent if isinstance(node.parent, nodes.Tuple) else node.parent
)
if node.name in gen.difference(kill):
if isinstance(parent, nodes.AnnAssign) and parent.value is None:
continue
elif node.name in gen.difference(kill):
# add redundant assignment
if parent not in self._redundant_assignment:
self._redundant_assignment[parent] = []
Expand Down
15 changes: 15 additions & 0 deletions tests/test_custom_checkers/test_redundant_assignment_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,21 @@ def test_multiple_target_assign_one_variable_redundant(self):
):
self.checker.visit_assign(assign_node)

def test_ignore_annotation_only_annassigns(self):
src = """
x: int
x = 10
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())

self.checker.visit_module(mod)
with self.assertNoMessages():
for node in mod.nodes_of_class(nodes.Assign):
self.checker.visit_assign(node)
for node in mod.nodes_of_class(nodes.AnnAssign):
self.checker.visit_augassign(node)


class TestRedundantAssignmentCheckerZ3Option(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = RedundantAssignmentChecker
Expand Down

0 comments on commit 726b7ae

Please sign in to comment.