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

Not reporting W0621 redefined-outer-name when the outer name is redefined by except .. as .. #4434

Closed
chucklin opened this issue May 3, 2021 · 3 comments · Fixed by #5630
Labels
Enhancement ✨ Improvement to a component False Negative 🦋 No message is emitted but something is wrong with the code False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@chucklin
Copy link

chucklin commented May 3, 2021

Steps to reproduce

Given a file a.py:

"""Test pylint W0621"""
class CustomException(Exception):
    """Customized Exception"""

def func():
    """Override CustomException by except .. as .."""
    try:
        raise CustomException('Test')
    except IOError as CustomException:
        pass

Current behavior

Result of pylint a.py:

************* Module test
test.py:8:14: E0601: Using variable 'CustomException' before assignment (used-before-assignment)

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

Expected behavior

Expecting pylint reports W0621 redefined-outer-name on line except IOError as CustomException

I tested overriding the outer name by with .. as .., and the W0621 reported without problems.
In my opinion, W0621 should also be reported when the outer name gets redefined by except .. as .. .

pylint --version output

Result of pylint --version output:

pylint 2.8.2
astroid 2.5.6
Python 3.9.2 (default, Apr 12 2021, 14:14:14)
[Clang 12.0.0 (clang-1200.0.32.29)]
@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component False Negative 🦋 No message is emitted but something is wrong with the code False Positive 🦟 A message is emitted but nothing is wrong with the code labels May 3, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, this is a strange behavior indeed.

@jacobtylerwalls
Copy link
Member

In my opinion, W0621 should also be reported when the outer name gets redefined by except .. as .. .

This bit on line 9 (the false negative for redefined-outer-name) is fixed in #5630. I'll add a regression test.

The false positive on line 8 for used-before-assignment is weird.

@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Jan 3, 2022

The false positive on line 8 for used-before-assignment is weird.

Oooh, no -- that's just how the python interpreter works!

"""Test pylint W0621"""
class CustomException(Exception):
    """Customized Exception"""

def func():
    """Override CustomException by except .. as .."""
    try:
        raise CustomException('Test')
    except IOError as CustomException:
        pass

if __name__ == "__main__":
    func()

Gives
UnboundLocalError: local variable 'CustomException' referenced before assignment.


So I think ultimately this is a duplicate of #5370, but I can add the smart test case.

jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Jan 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Jan 3, 2022
Pierre-Sassoulas added a commit that referenced this issue Jan 3, 2022
…hadows an outer one (#5630)

* Add regression test for #4434.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component False Negative 🦋 No message is emitted but something is wrong with the code False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
3 participants