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

False Positive with subclassed Enums #2626

Closed
jonapich opened this issue Nov 30, 2018 · 10 comments · Fixed by pylint-dev/astroid#1121
Closed

False Positive with subclassed Enums #2626

jonapich opened this issue Nov 30, 2018 · 10 comments · Fixed by pylint-dev/astroid#1121
Labels

Comments

@jonapich
Copy link

Adding custom methods to an Enum subclass and then implementing it: pylint can't see the new methods.

from enum import Enum, auto


class TestBase(Enum):
    """ Adds a special method to enums. """

    def hello_pylint(self) -> str:
        """ False positive. """
        return self.name


class TestEnum(TestBase):
    """ Tests the false positive for enums. """
    a = auto()
    b = auto()


test_enum = TestEnum.a
assert test_enum.hello_pylint() == test_enum.name

[no-member] Instance of 'auto' has no 'hello_pylint' member
[no-member] Instance of 'auto' has no 'name' member

Similar to #1932 and #2062

@PCManticore
Copy link
Contributor

Thanks for the report!

@belm0
Copy link
Contributor

belm0 commented Oct 1, 2019

I believe this problem happens regardless of subclassing.

What will it take for pylint/astroid to be aware of Enum's patching of class members?

@bhargavrpatel
Copy link

bhargavrpatel commented May 12, 2020

Just adding more examples on this as it may help close this issue.

import typing
from enum import Enum


class MyEnum(Enum):
    def to_pb2(self) -> int:
        return typing.cast(int, self.value)

    def __str__(self) -> str:
        return self.name.upper()  # pylint: disable=no-member


class SomeEnum(MyEnum):
    A = "A"


class AnotherEnum(Enum):
    B = "B"


SomeEnum.__members__.items()
AnotherEnum.__members__.items()

This will report an issue for SomeEnum.__members__

nelfin added a commit to nelfin/astroid that referenced this issue May 13, 2021
Ref pylint-dev/pylint#2626. Ref pylint-dev/pylint#3535. Ref pylint-dev/pylint#4358.
This updates the namedtuple/enum brain to add a dictionary for
__members__
nelfin added a commit to nelfin/astroid that referenced this issue May 13, 2021
Ref pylint-dev/pylint#2626. Ref pylint-dev/pylint#3535. Ref pylint-dev/pylint#4358.
This updates the namedtuple/enum brain to add a dictionary for
__members__
@belm0
Copy link
Contributor

belm0 commented Jun 8, 2021

@nelfin do you have a PR forthcoming? nelfin/astroid@afd0c2b

It seems as astroid gets smarter about propagating inference, I hit this problem more and more in my codebase.

@nelfin
Copy link
Contributor

nelfin commented Jun 8, 2021

@belm0, the fix you reference made it into the main astroid branch as pylint-dev/astroid@8181dc9 (see pylint-dev/astroid#941). It should be available in astroid 2.5.7 and later

@Pierre-Sassoulas
Copy link
Member

Will be available in pylint 2.9.0 (see milestone for release planning), or you can have it with pylint 2.8.2 if you set the astroid version to 2.5.8 yourself.

@belm0
Copy link
Contributor

belm0 commented Jun 8, 2021

@nelfin I'm actually still seeing this problem with subclassed enums on the latest astroid (2.5.8) and pylint pre-release. Has anyone confirmed otherwise?

@nelfin
Copy link
Contributor

nelfin commented Jun 8, 2021

Ah, now I understand. The commits we've been talking about actually only address the issue in the comment by bhargavrpatel above (#2626 (comment)), which talks about Enum.__members__. I'd misread the issue when I was first looking for duplicates about false-positives on Enum.__members__. I don't know if any work has been done to address incorrect inference of attributes on enum.Enum subclasses.

@belm0
Copy link
Contributor

belm0 commented Jun 9, 2021

If there's any guidance you can offer on what this problem might be or what parts of the implementation to poke at, I can try looking at it.

@nelfin
Copy link
Contributor

nelfin commented Jun 9, 2021

@belm0, I think astroid/brain/brain_namedtuple_enum.py:infer_enum_class would probably be the most likely place to start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants