Skip to content

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

Merged

Conversation

lem2clide
Copy link
Contributor

@lem2clide lem2clide commented Mar 29, 2020

@lem2clide lem2clide requested a review from ethanfurman as a code owner March 29, 2020 20:56
@the-knights-who-say-ni
Copy link

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 Missing

Our records indicate the following people have not signed the CLA:

@lem2clide

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
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

Copy link
Contributor

@rhettinger rhettinger left a 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.

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@@ -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] != '_']
Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Contributor Author

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] uses COMPARE_OP which seems to use opcode prediction, paired with POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE
  • startswith implies a call of the builtin function

Thanks for asking, I've learned more on the opcode dispatch while investigating!

Copy link
Member

@ethanfurman ethanfurman left a 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
Copy link
Member

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] != '_']
Copy link
Member

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.

@lem2clide lem2clide force-pushed the bpo-40084-dir-attributes-listing branch 2 times, most recently from 13b3621 to 71ed596 Compare March 30, 2020 20:56
@lem2clide
Copy link
Contributor Author

lem2clide commented Mar 30, 2020

Looking good! Change the News entry, add yourself to Misc/ACKS if you're not already there, and we should be good to go.

I have made the requested changes; please review again.

  • fixed News entry
  • added myself to Misc/ACKS

thanks for your review :)

@ethanfurman ethanfurman requested a review from rhettinger March 30, 2020 23:53
@@ -173,6 +173,9 @@ class Holiday(date, Enum):
IDES_OF_MARCH = 2013, 3, 15
self.Holiday = Holiday

from http import HTTPStatus
self.HTTPStatus = HTTPStatus
Copy link
Contributor

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))))

Copy link
Contributor Author

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.

@lem2clide lem2clide force-pushed the bpo-40084-dir-attributes-listing branch from 71ed596 to 03d064b Compare April 1, 2020 00:06
@lem2clide
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@rhettinger, @ethanfurman: please review the changes made to this pull request.

@bedevere-bot
Copy link

GH-22338 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 21, 2020
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 21, 2020
@bedevere-bot
Copy link

GH-22339 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Sep 21, 2020
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
@miss-islington
Copy link
Contributor

Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 21, 2020
@bedevere-bot
Copy link

GH-22853 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 21, 2020
@bedevere-bot
Copy link

GH-22854 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 21, 2020
ethanfurman pushed a commit that referenced this pull request Dec 9, 2020
@ethanfurman ethanfurman added the needs backport to 3.9 only security fixes label Dec 9, 2020
@miss-islington
Copy link
Contributor

Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@ethanfurman ethanfurman added needs backport to 3.9 only security fixes and removed needs backport to 3.9 only security fixes labels Dec 10, 2020
@miss-islington
Copy link
Contributor

Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@ethanfurman ethanfurman added needs backport to 3.9 only security fixes and removed needs backport to 3.9 only security fixes labels Dec 12, 2020
@miss-islington
Copy link
Contributor

Thanks @lem2clide for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Dec 13, 2020
@bedevere-bot
Copy link

GH-23746 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 13, 2020
miss-islington added a commit that referenced this pull request Dec 14, 2020
(cherry picked from commit 68526fe)

Co-authored-by: Angelin BOOZ <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants