-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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-45535: Improve output of Enum dir()
#29316
Conversation
This PR modifies the ``EnumType.__dir__()`` and ``Enum.__dir__()`` to ensure that user-defined methods and methods inherited from mixin classes always show up in the output of `help()`. This change also makes it easier for IDEs to provide auto-completion. Apologies for the big diff on the tests.
…d/cpython into Enum-dir-improvements
|
||
>>> dir(Planet) | ||
['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__'] | ||
['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__init__', '__members__', '__module__', 'surface_gravity'] |
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.
Does __init__
need to be here? It will never be called after the enum class is created.
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.
surface_gravity
is here so completion works?
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.
I think having __init__
show up on dir(Planet)
improves the output of help(Planet.EARTH)
. Without __init__
being present on dir(Planet)
, there is no reference in the output from help(Planet.EARTH)
to the fact that the member has two named attributes, mass
and radius
. With __init__
being present on dir(Planet)
, however, the signature of the __init__
function shows up in the output of help(Planet.EARTH)
, so we can clearly see that the member has two named attributes, mass
and radius
.
self.assertNotIn('__format__', int_enum_dir) | ||
self.assertNotIn('__hash__', int_enum_dir) | ||
|
||
class OverridesFormatOutsideEnumModule(Enum): |
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.
Do we lose IDE or completion help by not listing __format__
in dir()
?
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.
No, we don't, and I agree that __format__
should be excluded from the dir()
of Enum
, IntEnum
and StrEnum
, as well as any classes that inherit from these enum classes without overriding __format__
. This is still the case in my PR.
But if a user is defining a custom __format__
method in their own Enum
class, I think it would be surprising for the user if __format__
did not show up in dir()
or help()
. Their implementation of __format__
could have some unusual or surprising behaviour, which they might explain in detail in the docstring; but the docstring is of limited use if it doesn't show up in the output of help()
.
This PR is stale because it has been open for 30 days with no activity. |
I haven't forgotten. :-) |
Alex, I'd like to accept and merge this PR and give you credit since the only reason The other option is to close the PR, and simply add your name to the whatsnew in the improved I think the first option is better for your Github stats. :-) Which option would you like to take? |
Thank you, this is such a lovely message to get! It may seem silly, but could we possibly go for Option 1? I'm not currently employed in the tech sector (and am self-taught), but it's something I'd be very interested in potentially exploring in the future, so something like this would help boost my portfolio a little :) |
Happy to. I mostly didn't want you to be disappointed in the future when you check the code base and the details don't match what is getting committed now. |
Of course, I completely understand -- thanks for the heads-up! |
|
I believe this is a false positive. |
This PR modifies the
EnumType.__dir__()
andEnum.__dir__()
to ensurethat user-defined methods and methods inherited from mixin classes always
show up in the output of
help()
. This change also makes it easier forIDEs to provide auto-completion.
Apologies for the big diff on the tests.
https://bugs.python.org/issue45535