[3.12] gh-116938: Clarify documentation of dict and dict.update regarding the positional argument they accept (GH-125213) (#125337)

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-10-12 01:23:54 +02:00 committed by GitHub
parent 4ab19f912d
commit 3f38ea11c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View file

@ -4460,14 +4460,14 @@ can be used interchangeably to index the same dictionary entry.
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``
If no positional argument is given, an empty dictionary is created. If no positional argument is given, an empty dictionary is created.
If a positional argument is given and it is a mapping object, a dictionary If a positional argument is given and it defines a ``keys()`` method, a
is created with the same key-value pairs as the mapping object. Otherwise, dictionary is created by calling :meth:`~object.__getitem__` on the argument with
the positional argument must be an :term:`iterable` object. Each item in each returned key from the method. Otherwise, the positional argument must be an
the iterable must itself be an iterable with exactly two objects. The :term:`iterable` object. Each item in the iterable must itself be an iterable
first object of each item becomes a key in the new dictionary, and the with exactly two elements. The first element of each item becomes a key in the
second object the corresponding value. If a key occurs more than once, the new dictionary, and the second element the corresponding value. If a key occurs
last value for that key becomes the corresponding value in the new more than once, the last value for that key becomes the corresponding value in
dictionary. the new dictionary.
If keyword arguments are given, the keyword arguments and their values are If keyword arguments are given, the keyword arguments and their values are
added to the dictionary created from the positional argument. If a key added to the dictionary created from the positional argument. If a key
@ -4624,10 +4624,11 @@ can be used interchangeably to index the same dictionary entry.
Update the dictionary with the key/value pairs from *other*, overwriting Update the dictionary with the key/value pairs from *other*, overwriting
existing keys. Return ``None``. existing keys. Return ``None``.
:meth:`update` accepts either another dictionary object or an iterable of :meth:`update` accepts either another object with a ``keys()`` method (in
key/value pairs (as tuples or other iterables of length two). If keyword which case :meth:`~object.__getitem__` is called with every key returned from
arguments are specified, the dictionary is then updated with those the method). or an iterable of key/value pairs (as tuples or other iterables
key/value pairs: ``d.update(red=1, blue=2)``. of length two). If keyword arguments are specified, the dictionary is then
updated with those key/value pairs: ``d.update(red=1, blue=2)``.
.. method:: values() .. method:: values()

View file

@ -973,7 +973,7 @@ class MutableMapping(Mapping):
def update(self, other=(), /, **kwds): def update(self, other=(), /, **kwds):
''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k, v in F.items(): D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
''' '''