You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhance dir(module) to be informed by __all__ by updating module.__dir__ #75684
bpo-32225: Implement PEP 562: module getattr and dir
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-12-21.23:21:07.122>created_at=<Date2017-09-18.02:20:36.031>labels= ['type-feature', '3.7']
title='Enhance dir(module) to be informed by __all__ by updating module.__dir__'updated_at=<Date2018-12-21.23:21:07.121>user='https://github.com/codypiersall'
If a some_module defines __all__, dir(some_module) should only return what is in __all__. This is already a mechanism that Python provides to specify module-level APIs. Currently, dir(some_module) returns some_module.__dict__.keys().
The concern with this enhancement is backwards compatibility. It is conceivable that some library's code would be broken with the different return value of dir(some_module). However, it seems unlikely that any code, other than tests, depends on the current behavior of dir(some_module).
If __all__ is not defined in some_module, the old behavior is preserved.
I think change this would be risky and could break code that uses dir() to actually know what is in a module.
IIRC, this was discussed when __all__ was introduced. I believe it was decided that __all__ would affect from-imports and help() but not dir(). This decision has been in place for a long time now and I would expect that people (and tools) have come to rely on the current behavior.
Guido on python-ideas: I ave to agree with the other committers who already spoke up.
Me: I use dir to see the complete list now presently presented. The presence or absence of __file__ indicates coded on Python or not.
IDLE module completion start with public items (I believe __all__ or not start with _). Type '_' and the complete list appears. I think we should reject this.
While I agree changing the default would be risky from a compatibility perspective, I do think it would be helpful to offer a straightforward way to filter out transitive imports and other non-public implementation details from the result of dir(module), and require folks to use list(vars(module).keys()) if they want a reliably complete listing of all global variables defined in a module.
The simplest spelling that comes to mind would be to allow people to write:
__dir__=__all__
to get __all__ to also affect the result of dir(), permitting module authors to make their own determination as to whether they consider "The result of dir() includes non-public implementation details" to be covered by any backwards compatibility guarantees they might offer.
A less simple, but potentially more flexible, mechanism would be to require that to be spelled as:
def__dir__():
return__all__
but I'm not sure the extra flexibility would be useful in any meaningful way (especially if one of the options for easier definition of dynamic module level properties were eventually adopted).
As far the implementation goes, since the __dir__ overload support in the dir() builtin ignores instance dictionaries, this could be implemented in module.dir (or object.dir) rather than directly in the builtin.
Alternatively, it could use a different attribute name rather than having __dir__ be a list of strings on instances, and a protocol method on classes.
If we went with a different name, then I think __public__ would be a reasonable option, since the purpose of the attribute is to make it easy to programmatically distinguish public attributes from non-public ones (independently of the established leading underscore convention, which doesn't work well for transitive module references).
The OP commented on the PR that a feature close enough to the original request was being implemented in PEP-562, so I will close this issue with that one as a superseder.
Activity
codypiersall commentedon Sep 18, 2017
If a some_module defines __all__, dir(some_module) should only return what is in __all__. This is already a mechanism that Python provides to specify module-level APIs. Currently, dir(some_module) returns some_module.__dict__.keys().
The concern with this enhancement is backwards compatibility. It is conceivable that some library's code would be broken with the different return value of dir(some_module). However, it seems unlikely that any code, other than tests, depends on the current behavior of dir(some_module).
If __all__ is not defined in some_module, the old behavior is preserved.
rhettinger commentedon Sep 18, 2017
I think change this would be risky and could break code that uses dir() to actually know what is in a module.
IIRC, this was discussed when __all__ was introduced. I believe it was decided that __all__ would affect from-imports and help() but not dir(). This decision has been in place for a long time now and I would expect that people (and tools) have come to rely on the current behavior.
terryjreedy commentedon Sep 18, 2017
Guido on python-ideas: I ave to agree with the other committers who already spoke up.
Me: I use dir to see the complete list now presently presented. The presence or absence of __file__ indicates coded on Python or not.
IDLE module completion start with public items (I believe __all__ or not start with _). Type '_' and the complete list appears. I think we should reject this.
ncoghlan commentedon Sep 18, 2017
While I agree changing the default would be risky from a compatibility perspective, I do think it would be helpful to offer a straightforward way to filter out transitive imports and other non-public implementation details from the result of
dir(module), and require folks to uselist(vars(module).keys())if they want a reliably complete listing of all global variables defined in a module.The simplest spelling that comes to mind would be to allow people to write:
to get
__all__to also affect the result ofdir(), permitting module authors to make their own determination as to whether they consider "The result of dir() includes non-public implementation details" to be covered by any backwards compatibility guarantees they might offer.A less simple, but potentially more flexible, mechanism would be to require that to be spelled as:
but I'm not sure the extra flexibility would be useful in any meaningful way (especially if one of the options for easier definition of dynamic module level properties were eventually adopted).
ncoghlan commentedon Sep 18, 2017
As far the implementation goes, since the
__dir__overload support in thedir()builtin ignores instance dictionaries, this could be implemented in module.dir (or object.dir) rather than directly in the builtin.Alternatively, it could use a different attribute name rather than having
__dir__be a list of strings on instances, and a protocol method on classes.If we went with a different name, then I think
__public__would be a reasonable option, since the purpose of the attribute is to make it easy to programmatically distinguish public attributes from non-public ones (independently of the established leading underscore convention, which doesn't work well for transitive module references).serhiy-storchaka commentedon Sep 18, 2017
This would break tab completion of names not included in __all__.
ncoghlan commentedon Sep 18, 2017
Yes, that's one of the goals of the feature: to allow module authors to decide if they want tab completion for all attributes, or just the public API.
csabella commentedon Dec 21, 2018
The OP commented on the PR that a feature close enough to the original request was being implemented in PEP-562, so I will close this issue with that one as a superseder.