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

Infer user-defined enum classes by checking if the class is a subtype of enum.Enum #2277

Merged

Conversation

mbyrnepr2
Copy link
Member

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

Infer user-defined enum classes by checking if the class is a subtype of enum.Enum.

Closes pylint-dev/pylint#8897

return any(
klass.name in ENUM_BASE_NAMES
and getattr(klass.root(), "name", None) == "enum"
for klass in cls.mro()
Copy link
Member Author

@mbyrnepr2 mbyrnepr2 Aug 7, 2023

Choose a reason for hiding this comment

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

cls.mro() wasn't returning the enum ancestors in the case of the example on the original issue; hence the class never made became inferred. (it only returned a single-item list containing the original class itself).

Copy link
Member

Choose a reason for hiding this comment

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

Looks like a slight performance improvement, too, based on the small sample size of profiling astroid.

@mbyrnepr2 mbyrnepr2 force-pushed the pylint_issue_8897_enum_no_member branch 2 times, most recently from bed2f14 to 7c44571 Compare August 7, 2023 18:59
@mbyrnepr2 mbyrnepr2 force-pushed the pylint_issue_8897_enum_no_member branch from 7c44571 to 2bb499d Compare August 7, 2023 19:06
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.

On top of the fix, it seems like a more sensible approach indeed, good job. Did we have tests for each hard coded subclasses from the removed constants ? (I want to make sure that intEnum are still handled correctly)

@mbyrnepr2
Copy link
Member Author

On top of the fix, it seems like a more sensible approach indeed, good job. Did we have tests for each hard coded subclasses from the removed constants ? (I want to make sure that intEnum are still handled correctly)

Yes for IntEnum.
No for IntFlag - there's tests for it in Pylint but makes sense to have one here in astroid with the others.

@jacobtylerwalls
Copy link
Member

Sorry if this test will be difficult to rewrite! Maybe an extra inference call in the setup?

@mbyrnepr2
Copy link
Member Author

@jacobtylerwalls I've tried a few things to modify the test but no luck; when the program enters the enum brain module and into the is_subtype_of function we are getting a StopIteration (if I recall correctly) and the context path doesn't behave as expected. I've also had little time lately 😬.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.7 milestone Sep 9, 2023
@jacobtylerwalls jacobtylerwalls marked this pull request as ready for review September 23, 2023 18:50
@codecov
Copy link

codecov bot commented Sep 23, 2023

Codecov Report

Merging #2277 (0dd9498) into main (ea78827) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2277      +/-   ##
==========================================
- Coverage   92.85%   92.85%   -0.01%     
==========================================
  Files          94       94              
  Lines       11058    11054       -4     
==========================================
- Hits        10268    10264       -4     
  Misses        790      790              
Flag Coverage Δ
linux 92.66% <100.00%> (-0.01%) ⬇️
pypy 90.99% <100.00%> (-0.02%) ⬇️
windows 92.44% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
astroid/brain/brain_namedtuple_enum.py 92.88% <100.00%> (-0.10%) ⬇️

@jacobtylerwalls jacobtylerwalls merged commit c5352d5 into pylint-dev:main Sep 23, 2023
18 checks passed
jacobtylerwalls pushed a commit that referenced this pull request Sep 23, 2023
… of ``enum.Enum`` (#2277)

(cherry picked from commit c5352d5)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@mbyrnepr2 mbyrnepr2 deleted the pylint_issue_8897_enum_no_member branch September 23, 2023 19:04
jacobtylerwalls added a commit that referenced this pull request Sep 23, 2023
… of ``enum.Enum`` (#2277) (#2298)

(cherry picked from commit c5352d5)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.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 this pull request may close these issues.

pylint false positive - Instance of 'str' has no 'value' member (no-member)
3 participants