Open navotarad opened 2 years ago
The warning points at the wrong line in the code, and is unavoidable in some situations
In this example the warning message is confusing as it is raised against the a = {'b': 'c'} line, which is what the rule recommends writing
a = {'b': 'c'}
def f(a=None): if a is None: a = {"b": "c"} else: a["b"] = "c"
scratch.py:3:9: SIM904 Initialize dictionary 'a' directly
The warning can be avoided by writing the logic as
def f(a=None): if a is None: a = {} a['b'] = 'c'
which could be argued is better but it does not solve the issue of the next example.
Here the creation logic for the dictionary is distinct from the update logic, and it also triggers the warning
def f(a=None): if a is None: a = {"b": "x"} else: a["b"] = "c"
Again the code can be rewritten as
def f(a=None): if a is None: a = {} b = "x" else: b = "c" a["b"] = b
to avoid the warning, but I certainly don't think this is better
Desired change
Explanation
The warning points at the wrong line in the code, and is unavoidable in some situations
Example 1
In this example the warning message is confusing as it is raised against the
a = {'b': 'c'}
line, which is what the rule recommends writingThe warning can be avoided by writing the logic as
which could be argued is better but it does not solve the issue of the next example.
Example 2
Here the creation logic for the dictionary is distinct from the update logic, and it also triggers the warning
Again the code can be rewritten as
to avoid the warning, but I certainly don't think this is better