Skip to content

Prefer dict literal over dict constructor #7690

Closed
@hofrob

Description

@hofrob

Current problem

Constructing dicts using the constructor should be avoided. It has no advantages and may be a bit slower.

Invalid code

# construct dict using keywords
dict(foo="bar")
# construct dict using a dict
dict(**boo)

Valid code

# dict literal
{"foo": "bar"}

# casting values to a dict
dict(some_method())

# methods that are not the builtin `dict`
def dict(baz):
    ...

dict(1)

Desired solution

Create a message that finds and highlights invalid code from above. Maybe as an extension of R1735 use-dict-literal.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions