Skip to content

Commit 17070f4

Browse files
gh-144837: Improve documentation for more collection methods (GH-144841)
Use uniform standard signature syntax in the tutorial and in the array and collections modules documentation.
1 parent 5c0dcb3 commit 17070f4

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
@@ -133,9 +133,9 @@ The module defines the following type:
133133
The length in bytes of one array item in the internal representation.
134134

135135

136-
.. method:: append(x)
136+
.. method:: append(value, /)
137137

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

140140

141141
.. method:: buffer_info()
@@ -166,20 +166,20 @@ The module defines the following type:
166166
components (the real part, followed by imaginary part) is preserved.
167167

168168

169-
.. method:: count(x)
169+
.. method:: count(value, /)
170170

171-
Return the number of occurrences of *x* in the array.
171+
Return the number of occurrences of *value* in the array.
172172

173173

174-
.. method:: extend(iterable)
174+
.. method:: extend(iterable, /)
175175

176176
Append items from *iterable* to the end of the array. If *iterable* is another
177177
array, it must have *exactly* the same type code; if not, :exc:`TypeError` will
178178
be raised. If *iterable* is not an array, it must be iterable and its elements
179179
must be the right type to be appended to the array.
180180

181181

182-
.. method:: frombytes(buffer)
182+
.. method:: frombytes(buffer, /)
183183

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

191191

192-
.. method:: fromfile(f, n)
192+
.. method:: fromfile(f, n, /)
193193

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

199199

200-
.. method:: fromlist(list)
200+
.. method:: fromlist(list, /)
201201

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

205205

206-
.. method:: fromunicode(s)
206+
.. method:: fromunicode(ustr, /)
207207

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

213213

214-
.. method:: index(x[, start[, stop]])
214+
.. method:: index(value[, start[, stop]])
215215

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

221221
.. versionchanged:: 3.10
222222
Added optional *start* and *stop* parameters.
223223

224224

225-
.. method:: insert(i, x)
225+
.. method:: insert(index, value, /)
226226

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

230230

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

233233
Removes the item with the index *i* from the array and returns it. The optional
234234
argument defaults to ``-1``, so that by default the last item is removed and
235235
returned.
236236

237237

238-
.. method:: remove(x)
238+
.. method:: remove(value, /)
239239

240-
Remove the first occurrence of *x* from the array.
240+
Remove the first occurrence of *value* from the array.
241241

242242

243243
.. method:: clear()
@@ -262,7 +262,7 @@ The module defines the following type:
262262
:meth:`!tostring` is renamed to :meth:`tobytes` for clarity.
263263

264264

265-
.. method:: tofile(f)
265+
.. method:: tofile(f, /)
266266

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

Doc/library/collections.rst

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

240-
.. class:: Counter([iterable-or-mapping])
240+
.. class:: Counter(**kwargs)
241+
Counter(iterable, /, **kwargs)
242+
Counter(mapping, /, **kwargs)
241243
242244
A :class:`Counter` is a :class:`dict` subclass for counting :term:`hashable` objects.
243245
It is a collection where elements are stored as dictionary keys
@@ -287,7 +289,7 @@ For example::
287289
>>> sorted(c.elements())
288290
['a', 'a', 'a', 'a', 'b', 'b']
289291

290-
.. method:: most_common([n])
292+
.. method:: most_common(n=None)
291293

292294
Return a list of the *n* most common elements and their counts from the
293295
most common to the least. If *n* is omitted or ``None``,
@@ -297,7 +299,9 @@ For example::
297299
>>> Counter('abracadabra').most_common(3)
298300
[('a', 5), ('b', 2), ('r', 2)]
299301

300-
.. method:: subtract([iterable-or-mapping])
302+
.. method:: subtract(**kwargs)
303+
subtract(iterable, /, **kwargs)
304+
subtract(mapping, /, **kwargs)
301305
302306
Elements are subtracted from an *iterable* or from another *mapping*
303307
(or counter). Like :meth:`dict.update` but subtracts counts instead
@@ -328,7 +332,9 @@ For example::
328332

329333
This class method is not implemented for :class:`Counter` objects.
330334

331-
.. method:: update([iterable-or-mapping])
335+
.. method:: update(**kwargs)
336+
update(iterable, /, **kwargs)
337+
update(mapping, /, **kwargs)
332338
333339
Elements are counted from an *iterable* or added-in from another
334340
*mapping* (or counter). Like :meth:`dict.update` but adds counts
@@ -481,14 +487,14 @@ or subtracting from an empty counter.
481487

482488
Deque objects support the following methods:
483489

484-
.. method:: append(x)
490+
.. method:: append(item, /)
485491

486-
Add *x* to the right side of the deque.
492+
Add *item* to the right side of the deque.
487493

488494

489-
.. method:: appendleft(x)
495+
.. method:: appendleft(item, /)
490496

491-
Add *x* to the left side of the deque.
497+
Add *item* to the left side of the deque.
492498

493499

494500
.. method:: clear()
@@ -503,38 +509,38 @@ or subtracting from an empty counter.
503509
.. versionadded:: 3.5
504510

505511

506-
.. method:: count(x)
512+
.. method:: count(value, /)
507513

508-
Count the number of deque elements equal to *x*.
514+
Count the number of deque elements equal to *value*.
509515

510516
.. versionadded:: 3.2
511517

512518

513-
.. method:: extend(iterable)
519+
.. method:: extend(iterable, /)
514520

515521
Extend the right side of the deque by appending elements from the iterable
516522
argument.
517523

518524

519-
.. method:: extendleft(iterable)
525+
.. method:: extendleft(iterable, /)
520526

521527
Extend the left side of the deque by appending elements from *iterable*.
522528
Note, the series of left appends results in reversing the order of
523529
elements in the iterable argument.
524530

525531

526-
.. method:: index(x[, start[, stop]])
532+
.. method:: index(value[, start[, stop]])
527533

528-
Return the position of *x* in the deque (at or after index *start*
534+
Return the position of *value* in the deque (at or after index *start*
529535
and before index *stop*). Returns the first match or raises
530536
:exc:`ValueError` if not found.
531537

532538
.. versionadded:: 3.5
533539

534540

535-
.. method:: insert(i, x)
541+
.. method:: insert(index, value, /)
536542

537-
Insert *x* into the deque at position *i*.
543+
Insert *value* into the deque at position *index*.
538544

539545
If the insertion would cause a bounded deque to grow beyond *maxlen*,
540546
an :exc:`IndexError` is raised.
@@ -554,7 +560,7 @@ or subtracting from an empty counter.
554560
elements are present, raises an :exc:`IndexError`.
555561

556562

557-
.. method:: remove(value)
563+
.. method:: remove(value, /)
558564

559565
Remove the first occurrence of *value*. If not found, raises a
560566
:exc:`ValueError`.
@@ -567,7 +573,7 @@ or subtracting from an empty counter.
567573
.. versionadded:: 3.2
568574

569575

570-
.. method:: rotate(n=1)
576+
.. method:: rotate(n=1, /)
571577

572578
Rotate the deque *n* steps to the right. If *n* is negative, rotate
573579
to the left.
@@ -719,7 +725,9 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
719725
:class:`defaultdict` objects
720726
----------------------------
721727

722-
.. class:: defaultdict(default_factory=None, /, [...])
728+
.. class:: defaultdict(default_factory=None, /, **kwargs)
729+
defaultdict(default_factory, mapping, /, **kwargs)
730+
defaultdict(default_factory, iterable, /, **kwargs)
723731
724732
Return a new dictionary-like object. :class:`defaultdict` is a subclass of the
725733
built-in :class:`dict` class. It overrides one method and adds one writable
@@ -735,7 +743,7 @@ stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
735743
:class:`defaultdict` objects support the following method in addition to the
736744
standard :class:`dict` operations:
737745

738-
.. method:: __missing__(key)
746+
.. method:: __missing__(key, /)
739747

740748
If the :attr:`default_factory` attribute is ``None``, this raises a
741749
:exc:`KeyError` exception with the *key* as argument.
@@ -941,7 +949,7 @@ In addition to the methods inherited from tuples, named tuples support
941949
three additional methods and two attributes. To prevent conflicts with
942950
field names, the method and attribute names start with an underscore.
943951

944-
.. classmethod:: somenamedtuple._make(iterable)
952+
.. classmethod:: somenamedtuple._make(iterable, /)
945953

946954
Class method that makes a new instance from an existing sequence or iterable.
947955

@@ -1138,7 +1146,9 @@ Some differences from :class:`dict` still remain:
11381146
* Until Python 3.8, :class:`dict` lacked a :meth:`~object.__reversed__` method.
11391147

11401148

1141-
.. class:: OrderedDict([items])
1149+
.. class:: OrderedDict(**kwargs)
1150+
OrderedDict(mapping, /, **kwargs)
1151+
OrderedDict(iterable, /, **kwargs)
11421152
11431153
Return an instance of a :class:`dict` subclass that has methods
11441154
specialized for rearranging dictionary order.
@@ -1319,16 +1329,17 @@ subclass directly from :class:`dict`; however, this class can be easier
13191329
to work with because the underlying dictionary is accessible as an
13201330
attribute.
13211331

1322-
.. class:: UserDict([initialdata])
1332+
.. class:: UserDict(**kwargs)
1333+
UserDict(mapping, /, **kwargs)
1334+
UserDict(iterable, /, **kwargs)
13231335
13241336
Class that simulates a dictionary. The instance's contents are kept in a
13251337
regular dictionary, which is accessible via the :attr:`data` attribute of
1326-
:class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
1327-
initialized with its contents; note that a reference to *initialdata* will not
1328-
be kept, allowing it to be used for other purposes.
1338+
:class:`!UserDict` instances. If arguments are provided, they are used to
1339+
initialize :attr:`data`, like a regular dictionary.
13291340

13301341
In addition to supporting the methods and operations of mappings,
1331-
:class:`UserDict` instances provide the following attribute:
1342+
:class:`!UserDict` instances provide the following attribute:
13321343

13331344
.. attribute:: data
13341345

Doc/library/stdtypes.rst

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

11641164
Return the total number of occurrences of *value* in *sequence*.
11651165

1166-
.. method:: list.index(value[, start[, stop])
1167-
range.index(value[, start[, stop])
1168-
tuple.index(value[, start[, stop])
1166+
.. method:: list.index(value[, start[, stop]])
1167+
range.index(value[, start[, stop]])
1168+
tuple.index(value[, start[, stop]])
11691169
:no-contents-entry:
11701170
:no-index-entry:
11711171
:no-typesetting:
1172-
.. method:: sequence.index(value[, start[, stop])
1172+
.. method:: sequence.index(value[, start[, stop]])
11731173

11741174
Return the index of the first occurrence of *value* in *sequence*.
11751175

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
@@ -151,7 +151,7 @@ listen([n]) -- start listening for incoming connections\n\
151151
recv(buflen[, flags]) -- receive data\n\
152152
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
153153
recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
154-
recvfrom_into(buffer[, nbytes, [, flags])\n\
154+
recvfrom_into(buffer[, nbytes, [, flags]])\n\
155155
-- receive data and sender\'s address (into a buffer)\n\
156156
sendall(data[, flags]) -- send all data\n\
157157
send(data[, flags]) -- send data, may not send all of it\n\

0 commit comments

Comments
 (0)