Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

disconnect3d
Copy link
Contributor

@disconnect3ddisconnect3d commentedJul 19, 2025
edited by bedevere-appbot
Loading

This commit refactors simpledict.update({key: value}) calls which can be done viadict[key] = value instead.

I found those cases with thesemgrep 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})

This commit refactors simple `dict.update({key: value})` calls which canbe 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)
ifdocisnotNone:
seen_docs=True
slots.update({slot:doc})
slots[slot]=doc
Copy link
ContributorAuthor

@disconnect3ddisconnect3dJul 19, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Theslots is a dict here defined in line 1256

gpshead reacted with thumbs up emoji
'shared_memory':_posixshmem.shm_unlink,
})
_CLEANUP_FUNCS['semaphore']=_multiprocessing.sem_unlink
_CLEANUP_FUNCS['shared_memory']=_posixshmem.shm_unlink
Copy link
ContributorAuthor

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

gpshead reacted with thumbs up emoji
@@ -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
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thekw is a dict as passed to__init__(..., **kw) in line 21

gpshead reacted with thumbs up emoji
@@ -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
Copy link
ContributorAuthor

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

gpshead reacted with thumbs up emoji
@StanFromIrelandStanFromIreland added skip issue skip news type-refactorCode refactoring (with no changes in behavior) labelsJul 19, 2025
@gpsheadgpshead changed the titleRefactor simple dict.update callsgh-136839: Refactor simple dict.update callsJul 19, 2025
@gpsheadgpshead self-assigned thisJul 19, 2025
@gpsheadgpshead added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes and removed skip issue labelsJul 19, 2025
@gpshead
Copy link
Member

all of these are clearly correct. thanks.

@gpsheadgpshead merged commit69ea1b3 intopython:mainJul 19, 2025
54 checks passed
@miss-islington-app
Copy link

Thanks@disconnect3d for the PR, and@gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestJul 19, 2025
Refactor simple dict.update callsThis commit refactors simple `dict.update({key: value})` calls which canbe 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 commit69ea1b3)Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
@miss-islington-app
Copy link

Sorry,@disconnect3d and@gpshead, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 69ea1b3a8f45fec46add3272ad47f14ff5321ae8 3.13

@bedevere-app
Copy link

GH-136840 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelJul 19, 2025
@gpsheadgpshead removed the needs backport to 3.13bugs and security fixes labelJul 19, 2025
gpshead pushed a commit that referenced this pull requestJul 19, 2025
)gh-136839: Refactor simple dict.update calls (GH-136811)Refactor simple dict.update callsThis commit refactors simple `dict.update({key: value})` calls which canbe 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 commit69ea1b3)Co-authored-by: Disconnect3d <dominik.b.czarnota@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ericvsmithericvsmithAwaiting requested review from ericvsmithericvsmith is a code owner

@gpsheadgpsheadAwaiting requested review from gpsheadgpshead is a code owner

Assignees

@gpsheadgpshead

Labels
skip newstype-refactorCode refactoring (with no changes in behavior)
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@disconnect3d@gpshead@StanFromIreland

[8]ページ先頭

©2009-2025 Movatter.jp