We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I often see the code like:
def foo(d: dict) -> None: d.update({'request': HttpRequest()}) frobnicate(d)
Here .update can and should be replaced with:
.update
d['request'] = HttpRequest()
0.9.5
Pycharm v2021.2.2
Arch Linux (Linux lenovo 5.14.7-arch1-1 #1 SMP PREEMPT Wed, 22 Sep 2021 21:35:11 +0000 x86_64 GNU/Linux)
The text was updated successfully, but these errors were encountered:
yeah this looks like a nice little improvement
Sorry, something went wrong.
Be careful with multiple keys:
from typing import Dict d1: Dict[str, int] = {'a': 0, 'b': 10} d1.update({'a': d1['b'], 'b': d1['a']}) print(d1) # {'a': 10, 'b': 0} d2: Dict[str, int] = {'a': 0, 'b': 10} d2['a'] = d2['b'] d2['b'] = d2['a'] print(d2) # {'a': 10, 'b': 10}
Another possible refactoring here is when the "update_dict" keys are all str, in which case we can update the original dict using keyword arguments:
update_dict
str
d1: Dict[str, int] = {'a': 0, 'b': 10} d1.update({'a': d1['b'], 'b': d1['a']}) print(d1) # {'a': 10, 'b': 0} d2: Dict[str, int] = {'a': 0, 'b': 10} d2.update(a=d2['b'], b=d2['a']) print(d2) # {'a': 10, 'b': 0}
Added in 0.9.6
No branches or pull requests
Issue description or question
I often see the code like:
Here
.update
can and should be replaced with:Sourcery Version
0.9.5
Code editor or IDE name and version
Pycharm v2021.2.2
OS name and version
Arch Linux (Linux lenovo 5.14.7-arch1-1 #1 SMP PREEMPT Wed, 22 Sep 2021 21:35:11 +0000 x86_64 GNU/Linux)
The text was updated successfully, but these errors were encountered: