Improve inference of Enum members called "name" and "value"#1020
Merged
Pierre-Sassoulas merged 4 commits intopylint-dev:masterfrom Jun 13, 2021
Merged
Conversation
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.
nelfin
added a commit
to nelfin/pylint
that referenced
this pull request
Jun 10, 2021
4 tasks
Pierre-Sassoulas
approved these changes
Jun 10, 2021
Member
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
The explanation in the comment is great, I needed that :)
Contributor
Author
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.
fff5aa0 to
b39e76b
Compare
nelfin
added a commit
to nelfin/pylint
that referenced
this pull request
Jun 13, 2021
cdce8p
pushed a commit
to pylint-dev/pylint
that referenced
this pull request
Jun 16, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve inference of Enum members called "name" and "value"
Steps
Description
The attributes .name and .value on the enum.Enum are defined as data-descriptors (but not plain
@propertymethods) 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
Related Issue
pylint-dev/pylint#1932
pylint-dev/pylint#2062