-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-40084: Enum.__dir__ listing includes entries from instance dict #19219
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
bpo-40084: Enum.__dir__ listing includes entries from instance dict #19219
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests including the for http example. Also add a news/entry.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
@@ -614,7 +614,7 @@ def __dir__(self): | |||
for cls in self.__class__.mro() | |||
for m in cls.__dict__ | |||
if m[0] != '_' and m not in self._member_map_ | |||
] | |||
] + [m for m in self.__dict__ if m[0] != '_'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd prefer if not m.startswith("_")
because it's more Pythonic and resilient in case someone smuggles an empty string in.
But, perhaps, m[0]
is more performant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance isn't really an issue for a function like dir()
(except in extreme cases, of course). In this case we'll stick with m[0]
to match what's already being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that m[0]
is more performant and I also wanted to keep the consistency with the previous comparison m[0] != '_'
I don't know why it more performant though, I've found the following infos about what is performed:
m[0]
usesCOMPARE_OP
which seems to use opcode prediction, paired withPOP_JUMP_IF_FALSE
andPOP_JUMP_IF_TRUE
startswith
implies a call of the builtin function
Thanks for asking, I've learned more on the opcode dispatch while investigating!
aceecf3
to
9b2cf1a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Change the News
entry, add yourself to Misc/ACKS
if you're not already there, and we should be good to go.
@@ -0,0 +1 @@ | |||
Enum.__dir__ listing includes entries from instance dict |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dir(Enum.member)
now includes attributes as well as methods.
@@ -614,7 +614,7 @@ def __dir__(self): | |||
for cls in self.__class__.mro() | |||
for m in cls.__dict__ | |||
if m[0] != '_' and m not in self._member_map_ | |||
] | |||
] + [m for m in self.__dict__ if m[0] != '_'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance isn't really an issue for a function like dir()
(except in extreme cases, of course). In this case we'll stick with m[0]
to match what's already being used.
13b3621
to
71ed596
Compare
I have made the requested changes; please review again.
thanks for your review :) |
Lib/test/test_enum.py
Outdated
@@ -173,6 +173,9 @@ class Holiday(date, Enum): | |||
IDES_OF_MARCH = 2013, 3, 15 | |||
self.Holiday = Holiday | |||
|
|||
from http import HTTPStatus | |||
self.HTTPStatus = HTTPStatus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should be in the test for the http module. Also, the test is too brittle and would immediately break if a new code is added or if the API for ints were to be expanded.
Consider something like this:
self.assertTrue( {'value', 'name', 'description', 'phrase'} <= set(dir(HTTPStatus(404))))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed my tests would break in that case, thanks for your review :)
I've made the following changes:
- The HTTPStatus test is now in test_httplib.py, is it the right place?
- two implemented tests are checking that dir() listing contains attributes
I have made the requested changes; please review again.
71ed596
to
03d064b
Compare
I have made the requested changes; please review again |
Thanks for making the requested changes! @rhettinger, @ethanfurman: please review the changes made to this pull request. |
GH-22338 is a backport of this pull request to the 3.8 branch. |
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
GH-22339 is a backport of this pull request to the 3.9 branch. |
Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
GH-22853 is a backport of this pull request to the 3.8 branch. |
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
GH-22854 is a backport of this pull request to the 3.9 branch. |
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9. |
GH-23746 is a backport of this pull request to the 3.9 branch. |
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <[email protected]>
https://bugs.python.org/issue40084