Skip to content

Making AttrContainer work (with tab-completion, in pycharm) #15

@thorwhalen

Description

@thorwhalen

One of the main uses of dol.sources.AttrContainer is to be able to wrap mappings (say dicts) that allow a user to use tab-completion to access values through a path of keys. For example, being able to write this:

value = a.key.path

instead of

value = a['key']['path']

With dol.sources.AttrContainer, tab-completion works in browser-based jupyter, but doesn't work in PyCharm.

Is there a magic/dunder method that I can add to make it work?

image

If I define attributes at class definition time, tab-completion works though.

class SettingAttrWithSetattr:
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

from dol.sources import AttrContainer

def wrap_mapping_with_attr_access(**kwargs):
    return type('A', (), kwargs)()  # works


# mk_instance = AttrContainer  # (tab-completion) doesn't work (in pycharm)
mk_instance = SettingAttrWithSetattr  # (tab-completion) doesn't work (in pycharm)
# mk_instance = wrap_mapping_with_attr_access  # WORKS
# mk_instance = WithHardCodedClassAttributes  # WORKS

a = mk_instance(apple=1, banana=2)

t = a.apple
setattr(a, 'cherry', 3)
tt = a.cherry  # tab-complete DOES NOT work

a.cherry = 3 
tt = a.cherry  # tab-complete works

┆Issue is synchronized with this Asana task by Unito

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions