Simplify Dictionary Update
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: simplify-dictionary-update
Section titled “Sourcery refactoring id: simplify-dictionary-update”Description:
Section titled “Description:”Add single value to dictionary directly rather than using update()
Before:
Section titled “Before:”def foo(d: dict) -> None: d.update({"request": HttpRequest()}) frobnicate(d)After:
Section titled “After:”def foo(d: dict) -> None: d["request"] = HttpRequest() frobnicate(d)Explanation:
Section titled “Explanation:”When we are just adding one entry to a dictionary it is simpler to do it
directly rather than via the update() method. This also eliminates the
overhead of constructing another dictionary and calling a method, so should
slightly improve performance.