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

Weird redefined-variable-type warning for IntEnum #8955

Open
EugeneZelenko opened this issue Aug 14, 2023 · 3 comments
Open

Weird redefined-variable-type warning for IntEnum #8955

EugeneZelenko opened this issue Aug 14, 2023 · 3 comments
Labels
Bug 🪲 Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc.

Comments

@EugeneZelenko
Copy link

EugeneZelenko commented Aug 14, 2023

Bug description

redefined-variable-type shows weir warning for enum.IntEnum:

import enum

class Transition(enum.IntEnum):
    ONE_ONE     = 1
    ONE_TWO     = 2
    ONE_THREE   = 3
    TWO_TWO     = 4
    TWO_THREE   = 5
    THREE_THREE = 6

class State(enum.IntEnum):
    ONE   = 1
    TWO   = 2
    THREE = 3

state1 = State.ONE
state2 = State.ONE
transition = None
if state1 == State.ONE:
    if state2 == State.ONE:
        transition = Transition.ONE_ONE
    elif state2 == State.TWO:
        transition = Transition.ONE_TWO
    elif state2 == State.THREE:
        transition = Transition.ONE_THREE
    else:
        pass
elif state1 == State.TWO:
    if state2 == State.ONE:
        transition = Transition.ONE_TWO  # [redefined-variable-type]
    elif state2 == State.TWO:
        transition = Transition.TWO_TWO
    elif state2 == State.THREE:
        transition = Transition.TWO_THREE
    else:
        pass

Command used

pylint --load-plugins=pylint.extensions.redefined_variable_type --enable=all pylint_test.py

Pylint output

************* Module pylint_test
pylint_test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
pylint_test.py:3:0: C0115: Missing class docstring (missing-class-docstring)
pylint_test.py:11:0: C0115: Missing class docstring (missing-class-docstring)
pylint_test.py:18:0: C0103: Constant name "transition" doesn't conform to UPPER_CASE naming style (invalid-name)
pylint_test.py:30:8: R0204: Redefinition of transition type from test.Transition.ONE_THREE to pylint_test.Transition.ONE_TWO (redefined-variable-type)

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

Expected behavior

If check meant redefinition from None to Transition, such warnings should be shown for every assignment. If not, warning should not be there.

Pylint version

pylint 2.17.4
astroid 2.15.5
Python 3.11.4 (main, Jun 26 2023, 16:57:25) [GCC 7.5.0]
@EugeneZelenko EugeneZelenko added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 14, 2023
@Pierre-Sassoulas Pierre-Sassoulas added Bug 🪲 Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 15, 2023
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, it's still happening on main.

@hofrob
Copy link
Contributor

hofrob commented Nov 2, 2023

Another example using StrEnum:

import enum


class Foo(enum.StrEnum):
    ABC = enum.auto()
    DEF = enum.auto()


bar = Foo.ABC
bar = Foo.DEF

results in:

R0204: Redefinition of bar type from Foo.ABC to Foo.DEF (redefined-variable-type)

Looks like pylint assumes that the type is Foo.ABC instead of just Foo. Sometimes it will assume str instead of the StrEnum type. Annotations don't help here.

import enum


class Foo(enum.StrEnum):
    ABC = enum.auto()
    DEF = enum.auto()


def return_enum():
    return Foo("DEF")


bar = Foo.ABC
if True:
    bar = return_enum()

results in:

R0204: Redefinition of bar type from Foo.ABC to str (redefined-variable-type)

@hasselmm
Copy link

hasselmm commented Dec 7, 2023

Thank you for reporting this, I am also running into this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc.
Projects
None yet
Development

No branches or pull requests

4 participants