这是indexloc提供的服务,不要输入任何密码
Skip to content

Autocompletion for keys in VSCode etc #502

@s-github-2

Description

@s-github-2

The following subclassing worked in VScode notebook . Is it possible to enhance benedict to just do this built-in to support tab completion?
Maybe some keys would have to be converted to replace spaces with underscores etc (not done below)
Also if any subdicts could also be converted automatically to benedict so tab completion can work on nested dictionaries.

from benedict import Benedict

class AutoCompleteBenedict(Benedict):
    def __dir__(self):
        # grab the normal attributes…
        attrs = super().__dir__()
        try:
            # …and add your dynamic keys
            attrs.extend(self.keys())
        except Exception:
            pass
        return list(dict.fromkeys(attrs))  # dedupe, preserve order

# usage
mydict = AutoCompleteBenedict({'foo': 1, 'bar': 2})
# now in IPython / VSCode REPL, typing “mydict.<TAB>” will show foo, bar, etc.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions