Skip to content
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

enhanced dir? #91410

Closed
ravi140222 mannequin opened this issue Apr 7, 2022 · 9 comments
Closed

enhanced dir? #91410

ravi140222 mannequin opened this issue Apr 7, 2022 · 9 comments
Labels
3.11 only security fixes type-feature A feature request or enhancement

Comments

@ravi140222
Copy link
Mannequin

ravi140222 mannequin commented Apr 7, 2022

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']

    @ravi140222
    Copy link
    Mannequin Author

    ravi140222 mannequin commented Apr 7, 2022

    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__'}}
    

    ???

    @ravi140222 ravi140222 mannequin added 3.11 only security fixes type-feature A feature request or enhancement labels Apr 7, 2022
    @JelleZijlstra
    Copy link
    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.

    @ravi140222
    Copy link
    Mannequin Author

    ravi140222 mannequin commented Apr 8, 2022

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

    l = [1, 2, 3]
    dir(l, categorize=True)
    

    @ravi140222
    Copy link
    Mannequin Author

    ravi140222 mannequin commented Apr 8, 2022

    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__'}
    

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ravi140222
    Copy link
    Contributor

    ravi140222 commented Apr 11, 2022

    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', ...}
    

    ravi140222 added a commit to ravi140222/cpython that referenced this issue Apr 11, 2022
    @vainaixr
    Copy link
    Contributor

    vainaixr commented Apr 13, 2022

    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, ...}
    

    ravi140222 added a commit to ravi140222/cpython that referenced this issue Apr 14, 2022
    @iritkatriel
    Copy link
    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.

    @vainaixr
    Copy link
    Contributor

    vainaixr commented Apr 14, 2022

    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.

    @iritkatriel
    Copy link
    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
    Labels
    3.11 only security fixes type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants