Why does Pycharm's inspector complain about "d = {}"?

90,965

Solution 1

What is the following code to your dictionary declaration?

I think PyCharm will trigger the error if you have something like:

dic = {}
dic['aaa'] = 5

as you could have written

dic = {'aaa': 5}

Note: The fact that the error goes away if you use the function doesn't necessarily mean that pycharm believes dict() is a literal. It could just mean that it doesn't complain for:

dic = dict()
dic['aaa'] = 5

Solution 2

This can be disabled in the Project Settings or Default Settings.

  • Navigate to Settings -> Inspections -> Python
  • Uncheck "Dictionary creation could be rewritten by dictionary literal"

Solution 3

for those who like (just like me) to initialize dictionaries with single operation

d = {
  'a': 12,
  'b': 'foo',
  'c': 'bar'
}

instead of many lines like

d = dict()
d['a'] = 12
d['b'] = ....

in the end I ended up with this:

d = dict()
d.update({
  'a': 12,
  'b': 'foo',
  'c': 'bar'
})

Pycharm is not complaining on this

Share:
90,965

Related videos on Youtube

Chris Sears
Author by

Chris Sears

Updated on November 04, 2021

Comments

  • Chris Sears
    Chris Sears over 2 years

    When initializing a dictionary with d = {} Pycharm's code inspector generates a warning, saying

    This dictionary creation could be rewritten as a dictionary literal.

    If I rewrite it d = dict() the warning goes away. Since {} already is a dictionary literal, I'm pretty sure the message is erroneous. Furthermore, it seems like both d = {} and d = dict() are valid and Pythonic.

    This related question seems to conclude that the choice is just a matter of style/preference: differences between "d = dict()" and "d = {}"

    Why would Pycharm complain about d = {}?

    UPDATE:

    Mac nailed it. The warning actually applied to multiple lines, not just the one that was flagged.

    Pycharm seems to look for a sequence of consecutive statements where you initialize a dictionary and then set values in the dictionary. For example, this will trigger the warning:

    d = {}
    d['a'] = 1
    

    But this code will not:

    d = {}
    pass
    d['a'] = 1
    
    • dashesy
      dashesy over 9 years
      too noisy, and there is no real performance gain, just one more superfluous inspection
    • cleros
      cleros almost 9 years
      Same thing happens for lists: a = [1]; a.append(2), probably because a=[1, 2] is nicer ....
    • Rajkumar R
      Rajkumar R over 8 years
      yup. annoying message. all those underlines by PyCharm makes one uncomfortable before executing the program.
    • Dudnikof
      Dudnikof over 7 years
      I found similar issue in JetBrains YouTrack - youtrack.jetbrains.com/issue/PY-19269#u=1461253420326 and it says: In this case PyCharm suggests that you can provide the value for the something attribute right into the dict literal instead of assigning it at the next line.
  • dashesy
    dashesy over 9 years
    apparently it is for all these non-useful noisy inspections unfortunately some of my colleagues turn it off altogether, it is a shame because it is useful for many things like PEP, ..., real problems and real performance hints.
  • HelloGoodbye
    HelloGoodbye almost 8 years
    In my case that type of rewriting is not possible, since each dictionary item that is created (but the first one) depends on the previous dictionary item created. So they have to be assigned to the dictionary one by one rather than every item at the same time. However, PyCharm still complains and says that I should create the dictionary as a dictionary literal. I guess I have to use the dic = dict() workaround...
  • mac
    mac almost 8 years
    @HelloGoodbye - Without knowing the problem you are trying to solve I can't express a qualified opinion, but have you considered starting with d = { 'aaa': f1(something) } then d = f2(d) then d = f3(d) etc... Or alternatively d['bbb'] = f2(d), d['ccc'] = f3(d) ...?
  • HelloGoodbye
    HelloGoodbye almost 8 years
    The construction I have is d = {}, d['a'] = A, d['b'] = f(d['a']), d['c'] = f(d['b']), etc.
  • mac
    mac almost 8 years
    @HelloGoodbye - So, why not to merge the first two with d = {'a': A} and then just keep the sequence as you outlined?
  • mac
    mac over 6 years
    I cringe. :( So, you actually increased the amount of code and made it less clear and running slower just to get rid of a warning in the editor you happen to use... I don't use pycharm, but I would assume there is some sort of configuration toggle that will disable the warning and allow you to keep on coding in a pythonic way. :)
  • pa1983
    pa1983 over 6 years
    This is what I did and can confirm it works well. My code was: payload = {**BASEPAYLOAD, **ExtraPayload} to merge two dictionaries and it was throwing the error.
  • Igor.K
    Igor.K over 6 years
    @mac I agree now. I was young and stupid ) since then I've changed (a bit) and just disabled those warnings
  • mac
    mac over 6 years
    Lol! This must be the most memorable time-offset comment I ever received! ;)
  • Jeyekomon
    Jeyekomon over 2 years
    @dashesy On the contrary, these inspections make the code more clear and readable for anyone who will have to maintain your code in the future. The real noise happens when you turn them off and start to write code based on "your own style".