Skip to content

False positive W0612 / 'unused variable' when using same name for exception in except block #10426

Open
@DervishD

Description

@DervishD

Bug description

With the minimal code below a `W0612` warning is produced for the second except, but it goes if a different variable name is used instead of `exc`. For some reason, using the same variable name in both except blocks confuses the linter and causes the false positive.


try:
    pass
except TypeError as exc:
    print(exc)
except ValueError as exc:
    print(exc)

Configuration

[MASTER]
jobs=0
unsafe-load-any-extension=yes

[MESSAGES CONTROL]
enable=all
disable=I,too-many-lines

[REPORTS]
#reports=yes
msg-template='{path}:{line}:{column}: {msg_id} ({symbol}) {msg}'

[BASIC]
good-names=i,j,c,_
include-naming-hint=yes

[FORMAT]
max-line-length=120

[VARIABLES]
allow-global-unused-variables=no

Command used

I'm using Visual Studio Code Pylance, so the command is like:
`python.exe -m pylint --reports=n --output-format=json --clear-cache-post-run=y --from-stdin minimal.py`

Pylint output

{
        "type": "warning",
        "module": "erase",
        "obj": "",
        "line": 5,
        "column": 0,
        "endLine": 6,
        "endColumn": 14,
        "path": "C:\\Users\\rauln\\Desktop\\sysbin\\erase.py",
        "symbol": "unused-variable",
        "message": "Unused variable 'exc'",
        "message-id": "W0612"
    }

Expected behavior

No W0612 warning issued for that code.

Pylint version

pylint 3.3.4
astroid 3.3.8
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb  4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)]

OS / Environment

Windows 11 Home 24H2
Visual Studio Code 1.101.0
Pylance 2025.6.1

Additional dependencies

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions