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

Emit unused-variable for unused exceptions in exception handlers #2223

Closed
sushobhit27 opened this issue Jun 25, 2018 · 7 comments
Closed

Emit unused-variable for unused exceptions in exception handlers #2223

sushobhit27 opened this issue Jun 25, 2018 · 7 comments
Labels
Invalid Not a bug, already exists or already fixed

Comments

@sushobhit27
Copy link
Contributor

sushobhit27 commented Jun 25, 2018

Steps to reproduce

  1. cat sample.py
try:
    a = 1/0
except ArithmeticError as e:
    pass
  1. pylint sample.py

Current behavior


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

Expected behavior

pylint should give a warning that only pass statement is in the except block, as the exception is simply consumed without doing meaningful operation on it.
This issue occurs occasionally, as I personally write skeleton try/except block with pass and later on add either logging or re raising exception code in except block after completing the main code in try block. However sometimes I just miss some except blocks and this message should highlight this issue.

Note: Although this is sometimes expected behavior, but if not, then can lead to subtle bugs.

for key in some_seq:
    try:
        print(d[key])
    except KeyError as ke:
        pass

pylint --version output

pylint 2.0.0.dev1
astroid 2.0.0.dev3
Python 3.6.4 (default, Jan 7 2018, 15:53:53)
[GCC 6.4.0]

Another similar issue but somewhat minor than above one is:

Steps to reproduce

  1. cat sample.py
try:
    a = 1/0
except ArithmeticError as e:
    print('ArithmeticError occurred')
  1. pylint sample.py

Current behavior


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

Expected behavior

pylint should give a warning that variable 'e' is not used in the except block.

@PCManticore
Copy link
Contributor

This is intended. We're only emitting in some conditions for except: and except Exception: but for general exception handling we're not doing anything special as we cannot know the intention of the user. Just imagine if we'd introduce a warning for except ...: pass, we'll suddenly introduce hundreds of violations in projects for code that might not be buggy at all.

What should we fix though is the second example, we should emit that e is unused.

@PCManticore PCManticore changed the title Empty except block | only pass statement in except Emit unused-variable for unused exceptions in exception handlers Jun 25, 2018
@PCManticore PCManticore added the Enhancement ✨ Improvement to a component label Jun 25, 2018
@sushobhit27
Copy link
Contributor Author

sushobhit27 commented Jun 25, 2018

Just imagine if we'd introduce a warning for except ...: pass, we'll suddenly introduce hundreds of violations in projects for code that might not be buggy at all.

hmm that's true, that's why I also mentioned the valid case.

@sushobhit27
Copy link
Contributor Author

@PCManticore is there any function like is_name_used_in_scope in checkers/utils.py?

@jacobtylerwalls
Copy link
Member

Duplicate of #626, which was fixed. Suggest closing.

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Nov 24, 2021

@jacobtylerwalls it looks like it's not exactly the same thing, we still do not emit warning for the snippet provided in the issue:

$ cat a.py 
try:
    a = 1/0
except ArithmeticError as e:
    print('ArithmeticError occurred')
$ pylint a.py --enable=all --enable-all-extensions
************* Module a
a.py:1:0: C0114: Missing module docstring (missing-module-docstring)

------------------------------------------------------------------
Your code has been rated at 7.50/10 (previous run: 7.50/10, +0.00)

@jacobtylerwalls
Copy link
Member

True, thank you. Closely related to #4391.

@jacobtylerwalls
Copy link
Member

These examples were only presented at the module-level. These work with --allow-global-unused-variables=False in your config.

try:
    a = 1/0
except ArithmeticError as e:
    print('ArithmeticError occurred')
% pylint a.py --allow-global-unused-variables=False
************* Module a
a.py:2:4: W0612: Unused variable 'a' (unused-variable)
a.py:3:0: W0612: Unused variable 'e' (unused-variable)

For function examples, see discussion at #4391 about whether we need more handling.

@jacobtylerwalls jacobtylerwalls added Invalid Not a bug, already exists or already fixed and removed Enhancement ✨ Improvement to a component High effort 🏋 Difficult solution or problem to solve labels Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, already exists or already fixed
Projects
None yet
Development

No branches or pull requests

4 participants