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

PythonPycharm

Python Problem Overview


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: https://stackoverflow.com/questions/2745008/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

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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

Solution 4 - Python

mydict = {
  a: 5,
  b:z+c/2
}

The dictionary could have been created directly without initialising them first and then reassigning new values.

Solution 5 - Python

I have a situation where this warning is bugging the hell out of me. In my case, I'm populating my dict partially as literals and partially from a tuple output by a function, like so:

def get_other_values():
    return 3, 4

foo = {
    "a": 1,
    "b": 2
}
foo["c"], foo["d"] = get_other_values()

So, unless I create interim vars for the output of get_other_values, PEP8 generates this warning even though I'm creating the dict with literals. And I can't assign the c and d keys in the literal, since the values are output as a tuple.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris SearsView Question on Stackoverflow
Solution 1 - PythonmacView Answer on Stackoverflow
Solution 2 - PythonCraig JacksonView Answer on Stackoverflow
Solution 3 - PythonIgor.KView Answer on Stackoverflow
Solution 4 - PythonAsnim P AnsariView Answer on Stackoverflow
Solution 5 - PythonChris WoodfieldView Answer on Stackoverflow