Adjustment: The rule is too greedy and will happily trigger in code that has nothing to do with the supposed suggested simplification in the documentation
sim401_bug.py:3:1: SIM401 Use 'retval = container_1.get(key, expensive_side_effect())' instead of an if-block
Issues with the rule
container_1 is not a dict, could be any generic container
the variable accessed inside the if block is not the same one in the condition
the else block is non-trival code with side-effects
Suggestion
The rule should only trigger in the very strict case
if key in container:
retval = container[key]
else:
retval = "value"
Where
container must be accessed in both the condition with the form key in container and then the if block container[key]
key must be reused both in the condition and if block
"value" must be a literal or simple variable name e.g. "value" or value. Else, stuff like value.property or value() can have side-effects which change the state of the application.
Desired change
Example
This testcase shows the false posivie
Output
Issues with the rule
container_1
is not a dict, could be any generic containerSuggestion
The rule should only trigger in the very strict case
Where
container
must be accessed in both the condition with the formkey in container
and then the if blockcontainer[key]
key
must be reused both in the condition and if block"value"
must be a literal or simple variable name e.g."value"
orvalue
. Else, stuff likevalue.property
orvalue()
can have side-effects which change the state of the application.