ChatGPT解决这个技术问题 Extra ChatGPT

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

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
too noisy, and there is no real performance gain, just one more superfluous inspection
Same thing happens for lists: a = [1]; a.append(2), probably because a=[1, 2] is nicer ....
yup. annoying message. all those underlines by PyCharm makes one uncomfortable before executing the program.
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.

m
mac

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

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.
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...
@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) ...?
The construction I have is d = {}, d['a'] = A, d['b'] = f(d['a']), d['c'] = f(d['b']), etc.
@HelloGoodbye - So, why not to merge the first two with d = {'a': A} and then just keep the sequence as you outlined?
C
Craig Jackson

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"


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.
I
Igor.K

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


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. :)
@mac I agree now. I was young and stupid ) since then I've changed (a bit) and just disabled those warnings
Lol! This must be the most memorable time-offset comment I ever received! ;)
C
CertainPerformance
mydict = {
  a: 5,
  b:z+c/2
}

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


C
Chris Woodfield

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.