[3.14] gh-136839: Refactor simple dict.update calls (GH-136811) (#136840)

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 69ea1b3a8f)

Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-07-19 19:36:31 +02:00 committed by GitHub
parent f8af7cb723
commit 4606b4089d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 5 additions and 9 deletions

View file

@ -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
# We only return dict if there's at least one doc member,
# otherwise we return tuple, which is the old default format.