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

pylint has issues with IntEnum.name type detection #1932

Closed
jleclanche opened this issue Mar 14, 2018 · 2 comments · Fixed by #4558
Closed

pylint has issues with IntEnum.name type detection #1932

jleclanche opened this issue Mar 14, 2018 · 2 comments · Fixed by #4558
Labels

Comments

@jleclanche
Copy link

# test.py
from enum import IntEnum


class MyEnum(IntEnum):
    FOO = 1

    def whats_my_name(self):
        return self.name.lower()


print(MyEnum.FOO.whats_my_name())

Steps to reproduce

  1. pylint -dC test.py with the file above

Output:

E:  8,15: Method 'name' has no 'lower' member (no-member)

pylint --version output

pylint 1.8.2,
astroid 1.6.1
Python 3.6.4 (default, Jan 5 2018, 02:35:40)
[GCC 7.2.1 20171224]

@PCManticore
Copy link
Contributor

Thanks for the report @jleclanche It still amazes me how much weird stuff goes around in enum.

@geckon
Copy link

geckon commented May 4, 2018

Reproducible for a plain Enum as well: #2062

nelfin added a commit to nelfin/astroid that referenced this issue Jun 10, 2021
Ref pylint-dev/pylint#1932. Ref pylint-dev/pylint#2062. The enum.Enum class itself
defines two @DynamicClassAttribute data-descriptors "name" and "value"
which behave differently when looked up on an instance or on the class.
When dealing with inference of an arbitrary instance of the enum class,
e.g. in a method defined in the class body like:

    class SomeEnum(enum.Enum):
        def method(self):
            self.name  # <- here

we should assume that "self.name" is the string name of some enum
member, unless the enum itself defines a "name" member.
nelfin added a commit to nelfin/astroid that referenced this issue Jun 10, 2021
Ref pylint-dev/pylint#1932. Ref pylint-dev/pylint#2062. The enum.Enum class itself
defines two @DynamicClassAttribute data-descriptors "name" and "value"
which behave differently when looked up on an instance or on the class.
When dealing with inference of an arbitrary instance of the enum class,
e.g. in a method defined in the class body like:

    class SomeEnum(enum.Enum):
        def method(self):
            self.name  # <- here

we should assume that "self.name" is the string name of some enum
member, unless the enum itself defines a "name" member.
Pierre-Sassoulas added a commit to pylint-dev/astroid that referenced this issue Jun 13, 2021
* Add DynamicClassAttribute to list of properties

DynamicClassAttribute is a descriptor defined in Python's Lib/types.py
which changes the behaviour of an attribute depending on if it is looked
up on the class or on an instance.

* Add fake "name" property to enum.Enum subclasses

Ref pylint-dev/pylint#1932. Ref pylint-dev/pylint#2062. The enum.Enum class itself
defines two @DynamicClassAttribute data-descriptors "name" and "value"
which behave differently when looked up on an instance or on the class.
When dealing with inference of an arbitrary instance of the enum class,
e.g. in a method defined in the class body like:

    class SomeEnum(enum.Enum):
        def method(self):
            self.name  # <- here

we should assume that "self.name" is the string name of some enum
member, unless the enum itself defines a "name" member.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
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.

3 participants