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

Improve inference of Enum members called "name" and "value" #1020

Merged

Conversation

nelfin
Copy link
Contributor

@nelfin nelfin commented Jun 10, 2021

Improve inference of Enum members called "name" and "value"

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

The attributes .name and .value on the enum.Enum are defined as data-descriptors (but not plain @property methods) that behave differently when the attribute access is on the class or an instance. This change updates the enum brain to provide better guesses for values (and appropriate changes if the enum happens to define a member named "name" or "value").

Type of Changes

Type
🐛 Bug fix

Related Issue

pylint-dev/pylint#1932
pylint-dev/pylint#2062

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.
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation in the comment is great, I needed that :)

@nelfin
Copy link
Contributor Author

nelfin commented Jun 10, 2021

The explanation in the comment is great, I needed that :)

Thanks. I was thinking that it'd probably need explaining, and instead of sticking it in the PR, I'll just stick it in a comment straight away.

Re: the test failure on 3.10, it looks like the enum module has undergone a fairly extensive re-write. I'll update the tests since I don't think we really care what the value of the ".value" attribute is in a class context

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 nelfin force-pushed the fix/pylint1932+2026-enum-name-member branch from fff5aa0 to b39e76b Compare June 10, 2021 13:21
@Pierre-Sassoulas Pierre-Sassoulas added Brain 🧠 Needs a brain tip Enhancement ✨ Improvement to a component labels Jun 13, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.6.0 milestone Jun 13, 2021
@Pierre-Sassoulas Pierre-Sassoulas merged commit 4c9b9b5 into pylint-dev:master Jun 13, 2021
@nelfin nelfin mentioned this pull request Jun 13, 2021
2 tasks
cdce8p pushed a commit to pylint-dev/pylint that referenced this pull request Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Brain 🧠 Needs a brain tip Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants