Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive redefined-outer-name for variables bound to the exception in except clauses #9671

Closed
MikhailRyazanov opened this issue May 23, 2024 · 2 comments · Fixed by #9678 or #9702
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@MikhailRyazanov
Copy link

Bug description

# pylint: disable=missing-module-docstring, missing-function-docstring
def f():
    try:
        raise ValueError('inner')
    except ValueError as e:
        print(e)

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

# print(e)  # NameError: name 'e' is not defined

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:5:4: W0621: Redefining name 'e' from outer scope (line 10) (redefined-outer-name)

Expected behavior

Pylint must not complain about reusing variables bound to the exception in except clauses because they are in fact not defined outside the corresponding except clause (as can be seen by uncommenting the last line in the example).

Pylint version

pylint 3.2.2
astroid 3.2.2
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0]

OS / Environment

No response

Additional dependencies

No response

@MikhailRyazanov MikhailRyazanov added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 23, 2024
@MikhailRyazanov MikhailRyazanov changed the title False W0621 for the variable bound to the exception in the except clause False W0621 for variables bound to the exception in except clauses May 23, 2024
@mbyrnepr2 mbyrnepr2 added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 23, 2024
@mbyrnepr2 mbyrnepr2 changed the title False W0621 for variables bound to the exception in except clauses False positive redefined-outer-name for variables bound to the exception in except clauses May 23, 2024
@mbyrnepr2 mbyrnepr2 added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label May 27, 2024
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue May 27, 2024
…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
@MikhailRyazanov
Copy link
Author

@mbyrnepr2, this attempt:

if line > stmt.lineno:
continue

doesn't make much sense because the same behavior is observed when the order is reversed:

# pylint: disable=missing-module-docstring, missing-function-docstring
try:
    raise ValueError('outer')
except ValueError as e:
    print(e)

def f():
    try:
        raise ValueError('inner')
    except ValueError as e:
        print(e)
a.py:10:4: W0621: Redefining name 'e' from outer scope (line 4) (redefined-outer-name)

Pylint should really understand that the variable from except ... as ...: doesn't exist outside the except block.
If it would help, this is what Python documentation says:

When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

except E as N:
   foo

was translated to

except E as N:
   try:
       foo
   finally:
       del N

As a side note, Pylint apparently doesn't understand explicit del either (I'm not saying that using it to redefine variables can be considered “good”, but if Pylint had working logic for del, it could be used for correcting this except problem).

@mbyrnepr2
Copy link
Member

Thank you @MikhailRyazanov. That indeed makes plenty of sense!
I've taken an alternative approach and perhaps this can be a workable solution for this case.
FYI since you mentioned del there is some reading you can check out here on that.

Pierre-Sassoulas pushed a commit that referenced this issue Jun 4, 2024
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. Check if the outer node is in the 
scope of an exception assignment and do not emit ``redefined-outer-name`` if that is the case.

Closes #9671
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.2.3 milestone Jun 4, 2024
github-actions bot pushed a commit that referenced this issue Jun 4, 2024
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. Check if the outer node is in the
scope of an exception assignment and do not emit ``redefined-outer-name`` if that is the case.

Closes #9671

(cherry picked from commit 57ae027)
Pierre-Sassoulas pushed a commit that referenced this issue Jun 4, 2024
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. Check if the outer node is in the
scope of an exception assignment and do not emit ``redefined-outer-name`` if that is the case.

Closes #9671

(cherry picked from commit 57ae027)

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
3 participants