dotmap icon indicating copy to clipboard operation
dotmap copied to clipboard

Initialization from dictionary does not provide code completion

Open chrgraham opened this issue 6 years ago • 2 comments

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 avatar May 01 '19 22:05 chrgraham

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

chrgraham avatar May 02 '19 00:05 chrgraham

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.

gevorg-vardanyan-im avatar Dec 08 '21 11:12 gevorg-vardanyan-im