GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `PyDict_Merge` and `PyDict_Update` have conflicting docs: both require `.keys` to exist · Issue #160669 · python/cpython

PyDict_Merge and PyDict_Update have conflicting docs: both require .keys to exist #160669

New issue
Copy link
New issue
Copy link
Open
Open
PyDict_Merge and PyDict_Update have conflicting docs: both require .keys to exist#160669
Copy link
Assignees
sobolevn
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirtopic-C-API

Description

@sobolevn
sobolevn
opened on Oct 25, 2026
Issue body actions

While working on msgspec we noticed that PyDict_Merge does not follow the documented way of merging objects: msgspec/msgspec#1309 (comment)

The docs (https://docs.python.org/3/c-api/dict.html#c.PyDict_Update) of PyDict_Update say:

This is the same as PyDict_Merge(a, b, 1) in C, and is similar to a.update(b) in Python except that PyDict_Update() doesn’t fall back to the iterating over a sequence of key value pairs if the second argument has no “keys” attribute.

So, this confuses me:

  • PyDict_Merge says:

Iterate over mapping object b adding key-value pairs to dictionary a. b may be a dictionary, or any object supporting PyMapping_Keys() and PyObject_GetItem().

  • PyDict_Update says:

PyDict_Update() doesn’t fall back to the iterating over a sequence of key value pairs if the second argument has no “keys” attribute.

So, I have 2 questions:

  • Is this a PyDict_Update doc issue?
  • Should PyDict_Merge require .keys attribute? Or can it work with an iterable of key-value pairs?

Repro

I can re-create this problem in CAPI tests:

diff --git Lib/test/test_capi/test_dict.py Lib/test/test_capi/test_dict.py
index 4cf404e9f56..6323a3ea28f 100644
--- Lib/test/test_capi/test_dict.py
+++ Lib/test/test_capi/test_dict.py
@@ -460,6 +460,11 @@ def test_dict_update(self):
     def test_dict_merge(self):
         # Test PyDict_Merge()
         merge = _testlimitedcapi.dict_merge
+
+        dct = {'a': 1, 'b': 2}
+        merge(dct, [('b', 3), ('c', 4)], 0)
+        self.assertEqual(dct, {'a': 1, 'b': 2, 'c': 4})
+
         for cls1 in DICT_TYPES:
             for cls2 in ANYDICT_TYPES + MAPPING_TYPES:
                 dct = cls1({'a': 1, 'b': 2})   
» ./python.exe -m test test_capi.test_dict
Using random seed: 1975933612
0:00:00 load avg: 1.91 mem:   27.6 MiB Run 1 test sequentially in a single process
0:00:00 load avg: 1.91 mem:   27.6 MiB [1/1] test_capi.test_dict
test test_capi.test_dict failed -- Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython/Lib/test/test_capi/test_dict.py", line 465, in test_dict_merge
    merge(dct, [('b', 3), ('c', 4)], 0)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'keys'

Linked PRs

  • gh-160669: Clarify PyDict_Update docs #161314
Reacted by Janek Nouvertné, Shamil and bug

Activity

  1. sobolevn
    added
    topic-C-API
    on Oct 25, 2026
  2. aisk
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Oct 25, 2026
  3. github-project-automation
    added this to docs issueson Oct 25, 2026
  4. github-project-automation
    moved this to Todo in docs issueson Oct 25, 2026
  5. commit-msuyog commented on Oct 26, 2026

    @commit-msuyog
    commit-msuyog
    on Oct 26, 2026
    More actions

    Hi @sobolevn! Is this issue still free? I'd like to work on it.

    It looks like a docs clarification in Doc/c-api/dict.rst. PyDict_Merge needs a keys() method and does not accept a sequence of pairs, so the docs should point to PyDict_MergeFromSeq2 for that case.

    Could you confirm that direction? Thanks!

  6. sobolevn commented on Oct 26, 2026

    @sobolevn
    sobolevn
    on Oct 26, 2026
    MemberAuthor
    More actions

    @commit-msuyog nope, please, wait for the consensus.

    Reacted by Suyog Verma
  7. encukou commented on Nov 4, 2026

    @encukou
    encukou
    on Nov 4, 2026
    Member
    More actions

    As the docs say, PyDict_Update “is the same as PyDict_Merge(a, b, 1) in C”. IMO, the sentence (or ideally the paragraph) should end there.

    Compared to a.update(b) in Python, neither PyDict_Merge nor PyDict_Update falls back to iterating key/value pairs.

    Reacted by Neko Asakura
  8. sobolevn
    self-assigned this
    on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

Labels

docsDocumentation in the Doc dirDocumentation in the Doc dirtopic-C-API

Projects

  • docs issues
    Status
    Todo
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.