Skip to content

Enhance dir(module) to be informed by __all__ by updating module.__dir__ #75684

Closed

Description

@codypiersall
BPO 31503
Nosy @brettcannon, @rhettinger, @terryjreedy, @ncoghlan, @bitdancer, @serhiy-storchaka, @csabella, @codypiersall
PRs
  • bpo-31503: Customize module.__dir__. #3610
  • Superseder
  • 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 = None
    closed_at = <Date 2018-12-21.23:21:07.122>
    created_at = <Date 2017-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 = <Date 2018-12-21.23:21:07.121>
    user = 'https://github.com/codypiersall'

    bugs.python.org fields:

    activity = <Date 2018-12-21.23:21:07.121>
    actor = 'cheryl.sabella'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-12-21.23:21:07.122>
    closer = 'cheryl.sabella'
    components = []
    creation = <Date 2017-09-18.02:20:36.031>
    creator = 'codypiersall'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31503
    keywords = []
    message_count = 8.0
    messages = ['302404', '302405', '302406', '302407', '302408', '302409', '302425', '332309']
    nosy_count = 8.0
    nosy_names = ['brett.cannon', 'rhettinger', 'terry.reedy', 'ncoghlan', 'r.david.murray', 'serhiy.storchaka', 'cheryl.sabella', 'codypiersall']
    pr_nums = ['3610']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '32225'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue31503'
    versions = ['Python 3.7']

    Activity

    1. codypiersall commented on Sep 18, 2017

      codypiersallmannequin
      MannequinAuthor

      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.

    2. added
      3.7 (EOL)
      type-feature
      on Sep 18, 2017
    3. rhettinger commented on Sep 18, 2017

      @rhettinger
      Contributor

      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.

    4. terryjreedy commented on Sep 18, 2017

      @terryjreedy
      Member

      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.

    5. ncoghlan commented on Sep 18, 2017

      @ncoghlan
      Contributor

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

    6. ncoghlan commented on Sep 18, 2017

      @ncoghlan
      Contributor

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

    7. serhiy-storchaka commented on Sep 18, 2017

      @serhiy-storchaka
      Member

      This would break tab completion of names not included in __all__.

    8. ncoghlan commented on Sep 18, 2017

      @ncoghlan
      Contributor

      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.

    9. csabella commented on Dec 21, 2018

      @csabella
      Contributor

      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.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)type-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions