Skip to content

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented Jul 19, 2025

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 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 [email protected]

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 <[email protected]>
@bedevere-app bedevere-app bot added skip news type-refactor Code refactoring (with no changes in behavior) labels Jul 19, 2025
@gpshead gpshead enabled auto-merge (squash) July 19, 2025 17:12
@gpshead gpshead merged commit 4606b40 into python:3.14 Jul 19, 2025
46 checks passed
kumaraditya303 pushed a commit to miss-islington/cpython that referenced this pull request Sep 9, 2025
…36811) (python#136840)

pythongh-136839: Refactor simple dict.update calls (pythonGH-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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news type-refactor Code refactoring (with no changes in behavior)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants