Skip to content

Fix unused-variable false positive when using same name for multiple exceptions #10436

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Jun 21, 2025

Type of Changes

Type
βœ“ πŸ› Bug fix
✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

Description

Somewhat related to #4791. When investigating the issue, I noticed that the false positive is only emitted when checking for global unused variables. It is fine when the try block is nested within a function like:

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

There seems to be existing logic handling this as a special case, so I extracted it into a function and reused it for the global unused variable check.

Closes #10426

@zenlyj zenlyj added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 21, 2025
Copy link

codecov bot commented Jun 21, 2025

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.88%. Comparing base (f5aa766) to head (a9b2599).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10436   +/-   ##
=======================================
  Coverage   95.88%   95.88%           
=======================================
  Files         176      176           
  Lines       19140    19144    +4     
=======================================
+ Hits        18352    18356    +4     
  Misses        788      788           
Files with missing lines Coverage Ξ”
pylint/checkers/variables.py 97.30% <100.00%> (+<0.01%) ⬆️
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit a9b2599

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport maintenance/3.3.x False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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