Skip to content

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

@MikhailRyazanov

Description

@MikhailRyazanov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions