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

Initialization from dictionary does not provide code completion #50

Open
chrgraham opened this issue May 1, 2019 · 2 comments
Open

Comments

@chrgraham
Copy link

chrgraham commented May 1, 2019

Python 3.6.5
dotmap 1.3.8
Pycharm Community 2019.1.1

I was hoping to use dotmap to provide dot notation code completion to some existing dictionaries. However, when initialized from an existing dictionary it does not seem to provide the code completion suggestions like it should.

Note that functionally everything works, you just have to already know what the keys are since the code completion fails.

from dotmap import DotMap

d1 = DotMap()
d1.dog = 'pug'
d1.cat = 'tabby'
print(d1)
# Start typing d1. and you get all of the existing keys suggested as results, as expected.
print(d1.dog)
print(d1.cat)

d2 = {'first': 'Carl', 'last': 'Smith'}
d2 = DotMap(d2)
print(d2)
# Start typing d2.    and none of the keys are suggested.
# However, if you know the key names and manually type them they function.
print(d2.first)
print(d2.last)

Screen Shot 2019-05-01 at 6 25 36 PM

@chrgraham
Copy link
Author

Same thing if you add keys like you'd do with a native dictionary.

d3 = DotMap()
d3['fruit'] = 'apple'
d3['vegetable'] = 'carrot'

Or via key initialization:

d4 = DotMap(a=1, b=2)

Or via the update() method.

d5 = DotMap()
d5.update(DotMap(color='red', temperature='cold'))

The only way code completion works for me appears to via manual input of the data via dot notation.

d6 = DotMap()
d6.meat = 'pork'

Screen Shot 2019-05-01 at 8 13 25 PM

@gevorg-vardanyan-im
Copy link

gevorg-vardanyan-im commented Dec 8, 2021

This is is still actual.
I have also come up to the above solution, but there is one issue: I have declared the following in the __init__ method of class like this:

class SomeClass:
    def __init__(self):
        self.fields = DotMap()
        self.fields.dev = 'dev'
        self.fields.qa = 'qa'

You can use dot under a method where you have declared it, but it does not work in other methods.

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

2 participants