-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-136839: Refactor simple dict.update calls #136811
New issue
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
gh-136839: Refactor simple dict.update calls #136811
Conversation
This commit refactors simple `dict.update({key: value})` calls which can be done via `dict[key] = value` instead. I found those cases with the [semgrep](https://semgrep.dev/) tool: ``` $ semgrep --lang python --pattern '$DICT.update({$A: ...})' ┌─────────────────┐ │ 5 Code Findings │ └─────────────────┘ Lib/dataclasses.py 1268┆ slots.update({slot: doc}) Lib/multiprocessing/resource_tracker.py 50┆ _CLEANUP_FUNCS.update({ 51┆ 'semaphore': _multiprocessing.sem_unlink, 52┆ }) ⋮┆---------------------------------------- 53┆ _CLEANUP_FUNCS.update({ 54┆ 'shared_memory': _posixshmem.shm_unlink, 55┆ }) Lib/tkinter/scrolledtext.py 26┆ kw.update({'yscrollcommand': self.vbar.set}) Lib/xmlrpc/server.py 242┆ self.funcs.update({'system.multicall' : self.system_multicall}) ```
@@ -1265,7 +1265,7 @@ def _create_slots(defined_fields, inherited_slots, field_names, weakref_slot): | |||
doc = getattr(defined_fields.get(slot), 'doc', None) | |||
if doc is not None: | |||
seen_docs = True | |||
slots.update({slot: doc}) | |||
slots[slot] = doc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The slots
is a dict here defined in line 1256
'shared_memory': _posixshmem.shm_unlink, | ||
}) | ||
_CLEANUP_FUNCS['semaphore'] = _multiprocessing.sem_unlink | ||
_CLEANUP_FUNCS['shared_memory'] = _posixshmem.shm_unlink |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_CLEANUP_FUNCS
is a dict defined in line 35
@@ -23,7 +23,7 @@ def __init__(self, master=None, **kw): | |||
self.vbar = Scrollbar(self.frame) | |||
self.vbar.pack(side=RIGHT, fill=Y) | |||
|
|||
kw.update({'yscrollcommand': self.vbar.set}) | |||
kw['yscrollcommand'] = self.vbar.set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The kw
is a dict as passed to __init__(..., **kw)
in line 21
@@ -239,7 +239,7 @@ def register_multicall_functions(self): | |||
|
|||
see http://www.xmlrpc.com/discuss/msgReader$1208""" | |||
|
|||
self.funcs.update({'system.multicall' : self.system_multicall}) | |||
self.funcs['system.multicall'] = self.system_multicall |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.funcs = {}
is a dict as defined in line 167 of this file
all of these are clearly correct. thanks. |
Thanks @disconnect3d for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
Refactor simple dict.update calls This commit refactors simple `dict.update({key: value})` calls which can be done via `dict[key] = value` instead. I found those cases with the [semgrep](https://semgrep.dev/) tool: ``` $ semgrep --lang python --pattern '$DICT.update({$A: ...})' ┌─────────────────┐ │ 5 Code Findings │ └─────────────────┘ Lib/dataclasses.py 1268┆ slots.update({slot: doc}) Lib/multiprocessing/resource_tracker.py 50┆ _CLEANUP_FUNCS.update({ 51┆ 'semaphore': _multiprocessing.sem_unlink, 52┆ }) ⋮┆---------------------------------------- 53┆ _CLEANUP_FUNCS.update({ 54┆ 'shared_memory': _posixshmem.shm_unlink, 55┆ }) Lib/tkinter/scrolledtext.py 26┆ kw.update({'yscrollcommand': self.vbar.set}) Lib/xmlrpc/server.py 242┆ self.funcs.update({'system.multicall' : self.system_multicall}) ``` (cherry picked from commit 69ea1b3) Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
Sorry, @disconnect3d and @gpshead, I could not cleanly backport this to
|
GH-136840 is a backport of this pull request to the 3.14 branch. |
) gh-136839: Refactor simple dict.update calls (GH-136811) Refactor simple dict.update calls This commit refactors simple `dict.update({key: value})` calls which can be done via `dict[key] = value` instead. I found those cases with the [semgrep](https://semgrep.dev/) tool: ``` $ semgrep --lang python --pattern '$DICT.update({$A: ...})' ┌─────────────────┐ │ 5 Code Findings │ └─────────────────┘ Lib/dataclasses.py 1268┆ slots.update({slot: doc}) Lib/multiprocessing/resource_tracker.py 50┆ _CLEANUP_FUNCS.update({ 51┆ 'semaphore': _multiprocessing.sem_unlink, 52┆ }) ⋮┆---------------------------------------- 53┆ _CLEANUP_FUNCS.update({ 54┆ 'shared_memory': _posixshmem.shm_unlink, 55┆ }) Lib/tkinter/scrolledtext.py 26┆ kw.update({'yscrollcommand': self.vbar.set}) Lib/xmlrpc/server.py 242┆ self.funcs.update({'system.multicall' : self.system_multicall}) ``` (cherry picked from commit 69ea1b3) Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
This commit refactors simple
dict.update({key: value})
calls which can be done viadict[key] = value
instead.I found those cases with the semgrep tool: