Skip to content

Remove-None-From-Default-Get

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 rule id: remove-none-from-default-get

Section titled “Sourcery rule id: remove-none-from-default-get”

Replace dict.get(x, None) with dict.get(x)

hats = {"bowler": Bowler(), "sombrero": Sombrero()}
fedora = hats.get("fedora", None)
hats = {"bowler": Bowler(), "sombrero": Sombrero()}
fedora = hats.get("fedora")

When using a dictionary’s get method you can specify a default to return if the key is not found. This defaults to None, so it is unnecessary to specify None if this is the required behaviour. Removing the unnecessary argument makes the code slightly shorter and clearer.