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

Missing W0612: Unused variable warning if exception name is being reused #6077

Closed
ptagl opened this issue Mar 31, 2022 · 4 comments
Closed
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@ptagl
Copy link

ptagl commented Mar 31, 2022

Bug description

Save the following code in a file like pylint_test.py.

"""
Dummy module.
"""

def my_function():
    """
    Dummy function.
    """

    try:
        my_var = 1/0
    except ZeroDivisionError as exception:
        assert exception

    try:
        my_var = 1/0
    except ZeroDivisionError as exception:
        # The following line makes "exception" unused
        #pass
        # For the second run, comment the line above and uncomment the line below
        print(exception)
        # Why "exception" at line 26 is not marked as unused?

    try:
        my_var = 1/0
    except ZeroDivisionError as exception:
        pass

    print(my_var)

my_function()

Configuration

No response

Command used

pylint pytlint_test.py

Pylint output

`pylint_bug.py:17:4: W0612: Unused variable 'exception' (unused-variable)` when line 19 is not commented and line 21 is commented, nothing when line 19 is commented and line 21 is not.

Expected behavior

I would expect to get a "unused variable" warning for line 26 at least when commenting line 19 and uncommenting line 21, like:

pylint_test.py:26:4: W0612: Unused variable 'exception' (unused-variable)

Pylint version

pylint 2.13.2

OS / Environment

Opensuse Tumbleweed

Additional dependencies

No response

@ptagl ptagl added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 31, 2022
@jacobtylerwalls
Copy link
Member

Hi @ptagl, thanks for the writeup. The variables checker warns once per variable per scope. For fishy patterns in the same scope (redefining a variable inside an except handler or nested loop) we have redefined-outer-name, but still no further messages for unused-variable.

For instance, just one warning is emitted for:

def a():
    x = 0  # [unused-variable]
    x = 0

If you have thoughts I'd be happy to continue discussing on #4391. Thanks for opening the ticket; it forced me to go back through that discussion again.

@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 2, 2022
@jacobtylerwalls
Copy link
Member

Oh, by the way, we also have #5838 open for warning about "dead stores" of reassigning "as e" and doing nothing with it. That's probably the better superseder.

@ptagl
Copy link
Author

ptagl commented Apr 5, 2022

@jacobtylerwalls thanks a lot for your answers, it's ok for me to close this issue: I'm not a Python expert, so I'm not sure what a linter should or should not report.

By the way, I just wanted to let you know that I've found that both Flake8 and Pyflakes correctly detect the unused variable in my example, not sure if such information is useful or not.

@jacobtylerwalls
Copy link
Member

Interesting. I had a look and it turns out that pyflakes is actually unsatisfied with their handling of this, so although your example works, they are issuing false positives, see PyCQA/pyflakes#378.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants