Visitar URL original
enhanced dir? · Issue #91410 · python/cpython · GitHub
Skip to content

enhanced dir? #91410

Description

@ravi140222
mannequin
BPO 47254
Nosy @JelleZijlstra, @ravi140222
PRs
  • bpo-47254: working on enhanced dir #32408
  • 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 = None
    created_at = <Date 2022-04-07.22:21:22.563>
    labels = ['type-feature', '3.11']
    title = 'enhanced dir?'
    updated_at = <Date 2022-04-08.01:33:59.720>
    user = 'https://github.com/ravi140222'

    bugs.python.org fields:

    activity = <Date 2022-04-08.01:33:59.720>
    actor = 'apostofes'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2022-04-07.22:21:22.563>
    creator = 'apostofes'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 47254
    keywords = ['patch']
    message_count = 4.0
    messages = ['416948', '416951', '416954', '416955']
    nosy_count = 2.0
    nosy_names = ['JelleZijlstra', 'apostofes']
    pr_nums = ['32408']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue47254'
    versions = ['Python 3.11']

    Activity

    1. ravi140222 commented on Apr 7, 2022

      ravi140222mannequin
      MannequinAuthor

      current dir gives output like this,

      from collection import OrderedDict
      od = OrderedDict({'a': 1, 'b': 2, 'c': 3})
      print(dir(od))
      
      ['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__',
       '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
       '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__',
       '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__',
       '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__',
       '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys',
       'get', 'items', 'keys', 'move_to_end', 'pop', 'popitem', 'setdefault',
       'update', 'values']
      

      but wouldn't it be better if the output was like this,

      
      {'OrderedDict': {'__contains__', '__delitem__', '__dir__', '__eq__',
                        '__format__', '__ge__', '__getitem__', '__gt__', '__init__', '__init_subclass__',
                        '__iter__', '__le__', '__lt__', '__ne__', '__reduce__', '__reduce_ex__',
                        '__repr__', '__reversed__', '__setitem__', '__sizeof__', '__subclasshook__',
                        'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'move_to_end', 'pop',
                        'popitem', 'setdefault', 'update', 'values'},
      'dict': {'__getattribute__', '__len__', '__new__'},
      'object': {'__delattr__', '__setattr__', '__str__'}}
      

      ???

    2. JelleZijlstra commented on Apr 7, 2022

      @JelleZijlstra
      Member

      This would be nice, but backward compatibility alone means we can't change dir() to return a dictionary. What you propose would make more sense as a new function, perhaps in a package like pydoc or a third-party tool like IPython.

    3. ravi140222 commented on Apr 8, 2022

      ravi140222mannequin
      MannequinAuthor

      would adding an argument to dir be a possible solution,
      like,

      l = [1, 2, 3]
      dir(l, categorize=True)
      
    4. ravi140222 commented on Apr 8, 2022

      ravi140222mannequin
      MannequinAuthor

      plus I would want it to have some more methods,

      l = [1, 2, 3]
      

      using enhanced dir should give,

      {'list': {'__add__', '__contains__', '__delitem__', '__eq__', '__ge__',
               '__getattribute__', '__getitem__', '__gt__', '__iadd__', '__imul__',
               '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
               '__lt__', '__mul__', '__ne__', '__new__', '__repr__',
               '__reversed__', '__rmul__', '__setitem__', '__sizeof__',
               '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend',
               'index', 'insert', 'pop', 'remove', 'reverse', 'sort'},
      'object': {'__delattr__', '__dir__', '__format__', '__reduce__', '__reduce_ex__', '__setattr__', '__str__'}}
      

      obtained from, (here only printing, made a dictionary in the implementation in the PR)

      for j in dir(l):
          print(f'l.{j}.__qualname__')
      

      this check fails for,

      {'list': {'__doc__', '__hash__', '__class__'}}
      

      I would also want these,

      {'list': {'__instancecheck__','__subclasscheck__', '__subclasses__', 'mro'},
       'type': {'__call__', '__prepare__'}}
      

      which were obtained from,

      for j in set(dir(type(l))) - set(dir(l)):
          print(f'l.{j}.__qualname__')
      

      and it fails for,

      {'list': {'__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__dict__', '__dictoffset__',
                 '__flags__', '__itemsize__', '__module__', '__mro__',
                 '__name__', '__qualname__', '__text_signature__', '__weakrefoffset__'}
      
    5. transferred this issue fromon Apr 10, 2022
    6. ravi140222 commented on Apr 11, 2022

      @ravi140222
      ContributorAuthor

      I would like this enhanced dir to have one more feature, that is the type of these functions, so, our enhanced dir would work something like this,

      l = [1, 2, 3]
      enhanced_dir(l, show_types=True) # one feature is categorize which I showed above, and show_types is the second feature
      

      which gives,

      { <class 'method-wrapper': {'__add__', '__contains__', '__delattr__', ...}
      { <class 'builtin_function_or_method': {'__dir__', 'append', 'insert', ...}
      
    7. added a commit that references this issue on Apr 11, 2022
    8. vainaixr commented on Apr 13, 2022

      @vainaixr
      Contributor

      i think so one more thing could be added to it, could name the keyword as checks,
      for example,

      enhanced_dir(None, checks=True)
      

      this would run a few checks on our argument, such as, whether it,

      1. is inheritable
      2. could be used as a key in a dictionary
      3. could be passed as an argument to defaultdict
      4. ...

      so, for None, the output would be something like this,

      {inheritable: False
      dict_key: True
      defaultdict_arg: True, ...}
      
    9. added a commit that references this issue on Apr 14, 2022
    10. iritkatriel commented on Apr 14, 2022

      @iritkatriel
      Member

      This is the kind of thing that people implement as a utility and publish on pypl. After a few years, if it's stable and has many users, it becomes possible to discuss adding it to the stdlib (which requires a PEP). You don't start by adding it to the stdlib.

    11. vainaixr commented on Apr 14, 2022

      @vainaixr
      Contributor

      sorry, i am new to this, and dont know much about pypl (or is it pypi?), i created a pull request for this also, and added it to pydoc.py that is this,

      #91449

      could you inform me where would be the suitable place for the function i created. is this the pypl url?
      https://github.com/pypa/warehouse
      will i have to create some package and then do something like pip install enhanced_dir, but i have only one function in it.

    12. iritkatriel commented on Apr 14, 2022

      @iritkatriel
      Member

      The cpython bug tracker and review system are not the places for learner questions. They are the places where we actually work in cpython.

      Try python-list for general python questions and core-mentorship for questions about contributing to python.

    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.11only security fixestype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions