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

unused-variable false negative on a caught exception in the latest 3.x alpha release #8595

Closed
yilei opened this issue Apr 19, 2023 · 6 comments · Fixed by #8684
Closed

unused-variable false negative on a caught exception in the latest 3.x alpha release #8595

yilei opened this issue Apr 19, 2023 · 6 comments · Fixed by #8684
Labels
C: unused-variable False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation Regression Unreleased
Milestone

Comments

@yilei
Copy link
Contributor

yilei commented Apr 19, 2023

Bug description

import logging

def func(module):
  try:
    module.compute()
    error = False
  except module.TimeoutError as e:
    logging.error('Timed out: %s', e)
    error = True

  if error:
    # Try again with larger timeout.
    try:
      module.compute(timeout=1200)
    except module.Error as e:
      pass

Configuration

No response

Command used

pylint a.py

Pylint output

-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 9.23/10, +0.77)

Expected behavior

It should raise unused-variable for the second e:

************* Module unused
t.py:15:4: W0612: Unused variable 'e' (unused-variable)


Your code has been rated at 9.23/10 (previous run: 10.00/10, -0.77)

Pylint version

This is a regression in the latest alpha:

pylint 3.0.0a6
astroid 2.15.3
Python 3.11.1 (main, Jan 10 2023, 15:08:01) [Clang 14.0.0 (clang-1400.0.29.202)]

And working as expected in 2.17.2

OS / Environment

No response

Additional dependencies

No response

@yilei yilei added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 19, 2023
@yilei
Copy link
Contributor Author

yilei commented Apr 19, 2023

My git bisect suggests this might have been a regression caused by #8441

@jacobtylerwalls
Copy link
Member

Hi @yilei, thanks for the report and bisect.

I was surprised: I would have expected we weren't warning about this case. Generally, when you define a variable like e a second time, as here, pylint doesn't report unused-variable. (See #5838). So IMO this is an improvement, at least until we address #5838.

I'll leave this open for a bit to see what others think.

@jacobtylerwalls jacobtylerwalls added C: undefined-variable Issues related to 'undefined-variable' check Needs decision 🔒 Needs a decision before implemention or rejection C: unused-variable and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling C: undefined-variable Issues related to 'undefined-variable' check labels Apr 23, 2023
@yilei
Copy link
Contributor Author

yilei commented Apr 23, 2023

Ah, this was not very obvious in the exception catching cases. Feel free to close this then. I can send a PR to add a test case if you think it would benefit?

@jacobtylerwalls
Copy link
Member

Yes, please do, thank you!

@jacobtylerwalls jacobtylerwalls added this to the 3.0.0b1 milestone Apr 23, 2023
@jacobtylerwalls jacobtylerwalls added tests Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs decision 🔒 Needs a decision before implemention or rejection labels Apr 23, 2023
@yilei
Copy link
Contributor Author

yilei commented Apr 24, 2023

@jacobtylerwalls Hmm, actually, it does raise unused-variable if I omit the if error: line so this is inconsistent and needs to be resolved one way or the other:

# pylint: disable=missing-module-docstring,missing-function-docstring

import logging


def func1(module):
    try:
        module.compute()
    except module.TimeoutError as exc:
        logging.error('Timed out: %s', exc)

    try:
        module.compute(timeout=1200)
    except module.Error as exc:
        pass


def func2(module):
    try:
        module.compute()
        error = False
    except module.TimeoutError as exc:
        logging.error('Timed out: %s', exc)
        error = True

    if error:
        # Try again with larger timeout.
        try:
            module.compute(timeout=1200)
        except module.Error as exc:
            pass

func1 raises, but not func2

(Unassigning myself for now since this needs a decision and isn't just a test case anymore.)

@yilei yilei removed their assignment Apr 24, 2023
@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Apr 24, 2023

Thanks for the test cases. I guess we can continue special-casing exception handlers in this instance. I'm not sure exactly when that was introduced, but #4791 looks relevant.

Thanks for the report.

@jacobtylerwalls jacobtylerwalls added Regression False Negative 🦋 No message is emitted but something is wrong with the code and removed tests labels Apr 24, 2023
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.0.0b1, 3.0.0a7 May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unused-variable False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation Regression Unreleased
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants