Skip to content

Commit 2bb1ac2

Browse files
[3.13] gh-144837: Improve documentation for more collection methods (GH-144841) (GH-146484)
Use uniform standard signature syntax in the tutorial and in the array and collections modules documentation. (cherry picked from commit 17070f4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 7d565d6 commit 2bb1ac2

File tree

5 files changed

+72
-61
lines changed

5 files changed

+72
-61
lines changed

Doc/library/array.rst

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ The module defines the following type:
112112
The length in bytes of one array item in the internal representation.
113113

114114

115-
.. method:: append(x)
115+
.. method:: append(value, /)
116116

117-
Append a new item with value *x* to the end of the array.
117+
Append a new item with the specified value to the end of the array.
118118

119119

120120
.. method:: buffer_info()
@@ -144,20 +144,20 @@ The module defines the following type:
144144
different byte order.
145145

146146

147-
.. method:: count(x)
147+
.. method:: count(value, /)
148148

149-
Return the number of occurrences of *x* in the array.
149+
Return the number of occurrences of *value* in the array.
150150

151151

152-
.. method:: extend(iterable)
152+
.. method:: extend(iterable, /)
153153

154154
Append items from *iterable* to the end of the array. If *iterable* is another
155155
array, it must have *exactly* the same type code; if not, :exc:`TypeError` will
156156
be raised. If *iterable* is not an array, it must be iterable and its elements
157157
must be the right type to be appended to the array.
158158

159159

160-
.. method:: frombytes(buffer)
160+
.. method:: frombytes(buffer, /)
161161

162162
Appends items from the :term:`bytes-like object`, interpreting
163163
its content as an array of machine values (as if it had been read
@@ -167,55 +167,55 @@ The module defines the following type:
167167
:meth:`!fromstring` is renamed to :meth:`frombytes` for clarity.
168168

169169

170-
.. method:: fromfile(f, n)
170+
.. method:: fromfile(f, n, /)
171171

172172
Read *n* items (as machine values) from the :term:`file object` *f* and append
173173
them to the end of the array. If less than *n* items are available,
174174
:exc:`EOFError` is raised, but the items that were available are still
175175
inserted into the array.
176176

177177

178-
.. method:: fromlist(list)
178+
.. method:: fromlist(list, /)
179179

180180
Append items from the list. This is equivalent to ``for x in list:
181181
a.append(x)`` except that if there is a type error, the array is unchanged.
182182

183183

184-
.. method:: fromunicode(s)
184+
.. method:: fromunicode(ustr, /)
185185

186186
Extends this array with data from the given Unicode string.
187187
The array must have type code ``'u'`` or ``'w'``; otherwise a :exc:`ValueError` is raised.
188188
Use ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
189189
array of some other type.
190190

191191

192-
.. method:: index(x[, start[, stop]])
192+
.. method:: index(value[, start[, stop]])
193193

194194
Return the smallest *i* such that *i* is the index of the first occurrence of
195-
*x* in the array. The optional arguments *start* and *stop* can be
196-
specified to search for *x* within a subsection of the array. Raise
197-
:exc:`ValueError` if *x* is not found.
195+
*value* in the array. The optional arguments *start* and *stop* can be
196+
specified to search for *value* within a subsection of the array. Raise
197+
:exc:`ValueError` if *value* is not found.
198198

199199
.. versionchanged:: 3.10
200200
Added optional *start* and *stop* parameters.
201201

202202

203-
.. method:: insert(i, x)
203+
.. method:: insert(index, value, /)
204204

205-
Insert a new item with value *x* in the array before position *i*. Negative
205+
Insert a new item *value* in the array before position *index*. Negative
206206
values are treated as being relative to the end of the array.
207207

208208

209-
.. method:: pop([i])
209+
.. method:: pop(index=-1, /)
210210

211211
Removes the item with the index *i* from the array and returns it. The optional
212212
argument defaults to ``-1``, so that by default the last item is removed and
213213
returned.
214214

215215

216-
.. method:: remove(x)
216+
.. method:: remove(value, /)
217217

218-
Remove the first occurrence of *x* from the array.
218+
Remove the first occurrence of *value* from the array.
219219

220220

221221
.. method:: clear()
@@ -240,7 +240,7 @@ The module defines the following type:
240240
:meth:`!tostring` is renamed to :meth:`tobytes` for clarity.
241241

242242

243-
.. method:: tofile(f)
243+
.. method:: tofile(f, /)
244244

245245
Write all items (as machine values) to the :term:`file object` *f*.
246246

Doc/library/collections.rst

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ For example::
240240
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
241241
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
242242

243-
.. class:: Counter([iterable-or-mapping])
243+
.. class:: Counter(**kwargs)
244+
Counter(iterable, /, **kwargs)
245+
Counter(mapping, /, **kwargs)
244246
245247
A :class:`Counter` is a :class:`dict` subclass for counting :term:`hashable` objects.
246248
It is a collection where elements are stored as dictionary keys
@@ -290,7 +292,7 @@ For example::
290292
>>> sorted(c.elements())
291293
['a', 'a', 'a', 'a', 'b', 'b']
292294

293-
.. method:: most_common([n])
295+
.. method:: most_common(n=None)
294296

295297
Return a list of the *n* most common elements and their counts from the
296298
most common to the least. If *n* is omitted or ``None``,
@@ -300,7 +302,9 @@ For example::
300302
>>> Counter('abracadabra').most_common(3)
301303
[('a', 5), ('b', 2), ('r', 2)]
302304

303-
.. method:: subtract([iterable-or-mapping])
305+
.. method:: subtract(**kwargs)
306+
subtract(iterable, /, **kwargs)
307+
subtract(mapping, /, **kwargs)
304308
305309
Elements are subtracted from an *iterable* or from another *mapping*
306310
(or counter). Like :meth:`dict.update` but subtracts counts instead
@@ -331,7 +335,9 @@ For example::
331335

332336
This class method is not implemented for :class:`Counter` objects.
333337

334-
.. method:: update([iterable-or-mapping])
338+
.. method:: update(**kwargs)
339+
update(iterable, /, **kwargs)
340+
update(mapping, /, **kwargs)
335341
336342
Elements are counted from an *iterable* or added-in from another
337343
*mapping* (or counter). Like :meth:`dict.update` but adds counts
@@ -477,14 +483,14 @@ or subtracting from an empty counter.
477483

478484
Deque objects support the following methods:
479485

480-
.. method:: append(x)
486+
.. method:: append(item, /)
481487

482-
Add *x* to the right side of the deque.
488+
Add *item* to the right side of the deque.
483489

484490

485-
.. method:: appendleft(x)
491+
.. method:: appendleft(item, /)
486492

487-
Add *x* to the left side of the deque.
493+
Add *item* to the left side of the deque.
488494

489495

490496
.. method:: clear()
@@ -499,38 +505,38 @@ or subtracting from an empty counter.
499505
.. versionadded:: 3.5
500506

501507

502-
.. method:: count(x)
508+
.. method:: count(value, /)
503509

504-
Count the number of deque elements equal to *x*.
510+
Count the number of deque elements equal to *value*.
505511

506512
.. versionadded:: 3.2
507513

508514

509-
.. method:: extend(iterable)
515+
.. method:: extend(iterable, /)
510516

511517
Extend the right side of the deque by appending elements from the iterable
512518
argument.
513519

514520

515-
.. method:: extendleft(iterable)
521+
.. method:: extendleft(iterable, /)
516522

517523
Extend the left side of the deque by appending elements from *iterable*.
518524
Note, the series of left appends results in reversing the order of
519525
elements in the iterable argument.
520526

521527

522-
.. method:: index(x[, start[, stop]])
528+
.. method:: index(value[, start[, stop]])
523529

524-
Return the position of *x* in the deque (at or after index *start*
530+
Return the position of *value* in the deque (at or after index *start*
525531
and before index *stop*). Returns the first match or raises
526532
:exc:`ValueError` if not found.
527533

528534
.. versionadded:: 3.5
529535

530536

531-
.. method:: insert(i, x)
537+
.. method:: insert(index, value, /)
532538

533-
Insert *x* into the deque at position *i*.
539+
Insert *value* into the deque at position *index*.
534540

535541
If the insertion would cause a bounded deque to grow beyond *maxlen*,
536542
an :exc:`IndexError` is raised.
@@ -550,7 +556,7 @@ or subtracting from an empty counter.
550556
elements are present, raises an :exc:`IndexError`.
551557

552558

553-
.. method:: remove(value)
559+
.. method:: remove(value, /)
554560

555561
Remove the first occurrence of *value*. If not found, raises a
556562
:exc:`ValueError`.
@@ -563,7 +569,7 @@ or subtracting from an empty counter.
563569
.. versionadded:: 3.2
564570

565571

566-
.. method:: rotate(n=1)
572+
.. method:: rotate(n=1, /)
567573

568574
Rotate the deque *n* steps to the right. If *n* is negative, rotate
569575
to the left.
@@ -715,7 +721,9 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
715721
:class:`defaultdict` objects
716722
----------------------------
717723

718-
.. class:: defaultdict(default_factory=None, /, [...])
724+
.. class:: defaultdict(default_factory=None, /, **kwargs)
725+
defaultdict(default_factory, mapping, /, **kwargs)
726+
defaultdict(default_factory, iterable, /, **kwargs)
719727
720728
Return a new dictionary-like object. :class:`defaultdict` is a subclass of the
721729
built-in :class:`dict` class. It overrides one method and adds one writable
@@ -731,7 +739,7 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
731739
:class:`defaultdict` objects support the following method in addition to the
732740
standard :class:`dict` operations:
733741

734-
.. method:: __missing__(key)
742+
.. method:: __missing__(key, /)
735743

736744
If the :attr:`default_factory` attribute is ``None``, this raises a
737745
:exc:`KeyError` exception with the *key* as argument.
@@ -937,7 +945,7 @@ In addition to the methods inherited from tuples, named tuples support
937945
three additional methods and two attributes. To prevent conflicts with
938946
field names, the method and attribute names start with an underscore.
939947

940-
.. classmethod:: somenamedtuple._make(iterable)
948+
.. classmethod:: somenamedtuple._make(iterable, /)
941949

942950
Class method that makes a new instance from an existing sequence or iterable.
943951

@@ -1134,7 +1142,9 @@ Some differences from :class:`dict` still remain:
11341142
* Until Python 3.8, :class:`dict` lacked a :meth:`~object.__reversed__` method.
11351143

11361144

1137-
.. class:: OrderedDict([items])
1145+
.. class:: OrderedDict(**kwargs)
1146+
OrderedDict(mapping, /, **kwargs)
1147+
OrderedDict(iterable, /, **kwargs)
11381148
11391149
Return an instance of a :class:`dict` subclass that has methods
11401150
specialized for rearranging dictionary order.
@@ -1315,16 +1325,17 @@ subclass directly from :class:`dict`; however, this class can be easier
13151325
to work with because the underlying dictionary is accessible as an
13161326
attribute.
13171327

1318-
.. class:: UserDict([initialdata])
1328+
.. class:: UserDict(**kwargs)
1329+
UserDict(mapping, /, **kwargs)
1330+
UserDict(iterable, /, **kwargs)
13191331
13201332
Class that simulates a dictionary. The instance's contents are kept in a
13211333
regular dictionary, which is accessible via the :attr:`data` attribute of
1322-
:class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
1323-
initialized with its contents; note that a reference to *initialdata* will not
1324-
be kept, allowing it to be used for other purposes.
1334+
:class:`!UserDict` instances. If arguments are provided, they are used to
1335+
initialize :attr:`data`, like a regular dictionary.
13251336

13261337
In addition to supporting the methods and operations of mappings,
1327-
:class:`UserDict` instances provide the following attribute:
1338+
:class:`!UserDict` instances provide the following attribute:
13281339

13291340
.. attribute:: data
13301341

Doc/library/stdtypes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,13 +1111,13 @@ Sequence types also support the following methods:
11111111

11121112
Return the total number of occurrences of *value* in *sequence*.
11131113

1114-
.. method:: list.index(value[, start[, stop])
1115-
range.index(value[, start[, stop])
1116-
tuple.index(value[, start[, stop])
1114+
.. method:: list.index(value[, start[, stop]])
1115+
range.index(value[, start[, stop]])
1116+
tuple.index(value[, start[, stop]])
11171117
:no-contents-entry:
11181118
:no-index-entry:
11191119
:no-typesetting:
1120-
.. method:: sequence.index(value[, start[, stop])
1120+
.. method:: sequence.index(value[, start[, stop]])
11211121

11221122
Return the index of the first occurrence of *value* in *sequence*.
11231123

Doc/tutorial/datastructures.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@ More on Lists
1515
The :ref:`list <typesseq-list>` data type has some more methods. Here are all
1616
of the methods of list objects:
1717

18-
.. method:: list.append(x)
18+
.. method:: list.append(value, /)
1919
:noindex:
2020

2121
Add an item to the end of the list. Similar to ``a[len(a):] = [x]``.
2222

2323

24-
.. method:: list.extend(iterable)
24+
.. method:: list.extend(iterable, /)
2525
:noindex:
2626

2727
Extend the list by appending all the items from the iterable. Similar to
2828
``a[len(a):] = iterable``.
2929

3030

31-
.. method:: list.insert(i, x)
31+
.. method:: list.insert(index, value, /)
3232
:noindex:
3333

3434
Insert an item at a given position. The first argument is the index of the
3535
element before which to insert, so ``a.insert(0, x)`` inserts at the front of
3636
the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``.
3737

3838

39-
.. method:: list.remove(x)
39+
.. method:: list.remove(value, /)
4040
:noindex:
4141

42-
Remove the first item from the list whose value is equal to *x*. It raises a
42+
Remove the first item from the list whose value is equal to *value*. It raises a
4343
:exc:`ValueError` if there is no such item.
4444

4545

46-
.. method:: list.pop([i])
46+
.. method:: list.pop(index=-1, /)
4747
:noindex:
4848

4949
Remove the item at the given position in the list, and return it. If no index
@@ -58,10 +58,10 @@ of the methods of list objects:
5858
Remove all items from the list. Similar to ``del a[:]``.
5959

6060

61-
.. method:: list.index(x[, start[, end]])
61+
.. method:: list.index(value[, start[, stop]])
6262
:noindex:
6363

64-
Return zero-based index of the first occurrence of *x* in the list.
64+
Return zero-based index of the first occurrence of *value* in the list.
6565
Raises a :exc:`ValueError` if there is no such item.
6666

6767
The optional arguments *start* and *end* are interpreted as in the slice
@@ -70,10 +70,10 @@ of the methods of list objects:
7070
sequence rather than the *start* argument.
7171

7272

73-
.. method:: list.count(x)
73+
.. method:: list.count(value, /)
7474
:noindex:
7575

76-
Return the number of times *x* appears in the list.
76+
Return the number of times *value* appears in the list.
7777

7878

7979
.. method:: list.sort(*, key=None, reverse=False)

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ listen([n]) -- start listening for incoming connections\n\
149149
recv(buflen[, flags]) -- receive data\n\
150150
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
151151
recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
152-
recvfrom_into(buffer[, nbytes, [, flags])\n\
152+
recvfrom_into(buffer[, nbytes, [, flags]])\n\
153153
-- receive data and sender\'s address (into a buffer)\n\
154154
sendall(data[, flags]) -- send all data\n\
155155
send(data[, flags]) -- send data, may not send all of it\n\

0 commit comments

Comments
 (0)