use-dict-literal / R1735#

Message emitted:

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


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]

original_dict = {"name": "Sunny", "age": 10, "favorite_color": "yellow"}
copied_dict = dict(**original_dict)  # [use-dict-literal]

response_dict = dict(answer="No")  # [use-dict-literal]

Correct code:

empty_dict = {}

original_dict = {"name": "Sunny", "age": 10, "favorite_color": "yellow"}
# shallow copy a dict
copied_dict = {**original_dict}

response_dict = {"answer": "No"}

Additional details:

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.