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

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

Open
thorwhalen opened this issue Jul 25, 2022 · 0 comments
Open

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

thorwhalen opened this issue Jul 25, 2022 · 0 comments

Comments

@thorwhalen
Copy link
Member

thorwhalen commented Jul 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant