use-dict-literal / R1735#

Message emitted:

Consider using '%s' instead of a call to 'dict'.

Description:

Emitted when using dict() to create a dictionary instead of a literal '{ ... }'. The literal is faster as it avoids an additional function call.

Problematic code:

empty_dict = dict()  # [use-dict-literal]
new_dict = dict(foo="bar")  # [use-dict-literal]
new_dict = dict(**another_dict)  # [use-dict-literal]

Correct code:

empty_dict = {}

# create using a literal dict
new_dict = {"foo": "bar"}

# shallow copy a dict
new_dict = {**another_dict}

Additional details:

https://gist.github.com/hofrob/ad143aaa84c096f42489c2520a3875f9

This example script shows an 18% increase in performance when using a literal over the constructor in python version 3.10.6.

Created by the refactoring checker.