Skip to content

Commit 69ea1b3

Browse files
authored
gh-136839: Refactor simple dict.update calls (#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}) ```
1 parent 67036f1 commit 69ea1b3

File tree

4 files changed

+5
-9
lines changed

4 files changed

+5
-9
lines changed

Lib/dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ def _create_slots(defined_fields, inherited_slots, field_names, weakref_slot):
12651265
doc = getattr(defined_fields.get(slot), 'doc', None)
12661266
if doc is not None:
12671267
seen_docs = True
1268-
slots.update({slot: doc})
1268+
slots[slot] = doc
12691269

12701270
# We only return dict if there's at least one doc member,
12711271
# otherwise we return tuple, which is the old default format.

Lib/multiprocessing/resource_tracker.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ def cleanup_noop(name):
4747
# absence of POSIX named semaphores. In that case, no named semaphores were
4848
# ever opened, so no cleanup would be necessary.
4949
if hasattr(_multiprocessing, 'sem_unlink'):
50-
_CLEANUP_FUNCS.update({
51-
'semaphore': _multiprocessing.sem_unlink,
52-
})
53-
_CLEANUP_FUNCS.update({
54-
'shared_memory': _posixshmem.shm_unlink,
55-
})
50+
_CLEANUP_FUNCS['semaphore'] = _multiprocessing.sem_unlink
51+
_CLEANUP_FUNCS['shared_memory'] = _posixshmem.shm_unlink
5652

5753

5854
class ReentrantCallError(RuntimeError):

Lib/tkinter/scrolledtext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, master=None, **kw):
2323
self.vbar = Scrollbar(self.frame)
2424
self.vbar.pack(side=RIGHT, fill=Y)
2525

26-
kw.update({'yscrollcommand': self.vbar.set})
26+
kw['yscrollcommand'] = self.vbar.set
2727
Text.__init__(self, self.frame, **kw)
2828
self.pack(side=LEFT, fill=BOTH, expand=True)
2929
self.vbar['command'] = self.yview

Lib/xmlrpc/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def register_multicall_functions(self):
239239
240240
see http://www.xmlrpc.com/discuss/msgReader$1208"""
241241

242-
self.funcs.update({'system.multicall' : self.system_multicall})
242+
self.funcs['system.multicall'] = self.system_multicall
243243

244244
def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
245245
"""Dispatches an XML-RPC method from marshalled (XML) data.

0 commit comments

Comments
 (0)