Skip to content

Commit

Permalink
Fix a false positive for redefined-outer-name when there is a nam…
Browse files Browse the repository at this point in the history
…e defined in an exception-handling block which shares the same name as a local variable that has been defined in a function body.

Closes pylint-dev#9671
  • Loading branch information
mbyrnepr2 committed May 27, 2024
1 parent 6888fde commit 67b7791
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9671.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a false positive for ``redefined-outer-name`` when there is a name defined in an exception-handling block which shares the same name as a local variable that has been defined in a function body.

Closes #9671
2 changes: 2 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,8 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
continue

line = definition.fromlineno
if line > stmt.lineno:
continue
if not self._is_name_ignored(stmt, name):
self.add_message(
"redefined-outer-name", args=(name, line), node=stmt
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/r/redefined/redefined_except_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ def func():
# pylint:disable-next=invalid-name, unused-variable
except IOError as CustomException: # [redefined-outer-name]
pass


def function_before():
"""https://github.com/pylint-dev/pylint/issues/9671
The local variable `e` should not trigger `redefined-outer-name`
when `e` is also defined in the subsequent exception handling block.
"""
e = 42
return e

try:
raise ValueError('outer')
except ValueError as e:
print(e)

0 comments on commit 67b7791

Please sign in to comment.