pylint-dev/pylint

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

MikhailRyazanov opened this issue · 2 comments

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