Skip to content

Commit 4ed71da

Browse files
Issues #29311, #29289: Fixed and improved docstrings for dict and OrderedDict
methods.
1 parent eb57b9f commit 4ed71da

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

Lib/collections/__init__.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ def clear(self):
157157
dict.clear(self)
158158

159159
def popitem(self, last=True):
160-
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
161-
Pairs are returned in LIFO order if last is true or FIFO order if false.
160+
'''Remove and return a (key, value) pair from the dictionary.
162161
162+
Pairs are returned in LIFO order if last is true or FIFO order if false.
163163
'''
164164
if not self:
165165
raise KeyError('dictionary is empty')
@@ -180,11 +180,9 @@ def popitem(self, last=True):
180180
return key, value
181181

182182
def move_to_end(self, key, last=True):
183-
'''Move an existing element to the end (or beginning if last==False).
184-
185-
Raises KeyError if the element does not exist.
186-
When last=True, acts like a fast version of self[key]=self.pop(key).
183+
'''Move an existing element to the end (or beginning if last is false).
187184
185+
Raise KeyError if the element does not exist.
188186
'''
189187
link = self.__map[key]
190188
link_prev = link.prev
@@ -248,7 +246,10 @@ def pop(self, key, default=__marker):
248246
return default
249247

250248
def setdefault(self, key, default=None):
251-
'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
249+
'''Insert key with a value of default if key is not in the dictionary.
250+
251+
Return the value for key if key is in the dictionary, else default.
252+
'''
252253
if key in self:
253254
return self[key]
254255
self[key] = default
@@ -274,9 +275,7 @@ def copy(self):
274275

275276
@classmethod
276277
def fromkeys(cls, iterable, value=None):
277-
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
278-
If not specified, the value defaults to None.
279-
278+
'''Create a new ordered dictionary with keys from iterable and values set to value.
280279
'''
281280
self = cls()
282281
for key in iterable:

Objects/clinic/dictobject.c.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PyDoc_STRVAR(dict_fromkeys__doc__,
66
"fromkeys($type, iterable, value=None, /)\n"
77
"--\n"
88
"\n"
9-
"Returns a new dict with keys from iterable and values equal to value.");
9+
"Create a new dictionary with keys from iterable and values set to value.");
1010

1111
#define DICT_FROMKEYS_METHODDEF \
1212
{"fromkeys", (PyCFunction)dict_fromkeys, METH_FASTCALL|METH_CLASS, dict_fromkeys__doc__},
@@ -40,7 +40,7 @@ PyDoc_STRVAR(dict___contains____doc__,
4040
"__contains__($self, key, /)\n"
4141
"--\n"
4242
"\n"
43-
"True if D has a key k, else False.");
43+
"True if the dictionary has a specified key, else False.");
4444

4545
#define DICT___CONTAINS___METHODDEF \
4646
{"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
@@ -49,7 +49,7 @@ PyDoc_STRVAR(dict_get__doc__,
4949
"get($self, key, default=None, /)\n"
5050
"--\n"
5151
"\n"
52-
"D.get(key[, default]) -> D[key] if key in D, else default.");
52+
"Return the value for key if key is in the dictionary, else default.");
5353

5454
#define DICT_GET_METHODDEF \
5555
{"get", (PyCFunction)dict_get, METH_FASTCALL, dict_get__doc__},
@@ -83,7 +83,9 @@ PyDoc_STRVAR(dict_setdefault__doc__,
8383
"setdefault($self, key, default=None, /)\n"
8484
"--\n"
8585
"\n"
86-
"D.get(key,default), also set D[key]=default if key not in D.");
86+
"Insert key with a value of default if key is not in the dictionary.\n"
87+
"\n"
88+
"Return the value for key if key is in the dictionary, else default.");
8789

8890
#define DICT_SETDEFAULT_METHODDEF \
8991
{"setdefault", (PyCFunction)dict_setdefault, METH_FASTCALL, dict_setdefault__doc__},
@@ -113,4 +115,4 @@ dict_setdefault(PyDictObject *self, PyObject **args, Py_ssize_t nargs, PyObject
113115
exit:
114116
return return_value;
115117
}
116-
/*[clinic end generated code: output=6e9d917602373072 input=a9049054013a1b77]*/
118+
/*[clinic end generated code: output=91aa6a9f3c402b1b input=a9049054013a1b77]*/

Objects/clinic/odictobject.c.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ PyDoc_STRVAR(OrderedDict_fromkeys__doc__,
66
"fromkeys($type, /, iterable, value=None)\n"
77
"--\n"
88
"\n"
9-
"New ordered dictionary with keys from S.\n"
10-
"\n"
11-
"If not specified, the value defaults to None.");
9+
"Create a new ordered dictionary with keys from iterable and values set to value.");
1210

1311
#define ORDEREDDICT_FROMKEYS_METHODDEF \
1412
{"fromkeys", (PyCFunction)OrderedDict_fromkeys, METH_FASTCALL|METH_CLASS, OrderedDict_fromkeys__doc__},
@@ -39,7 +37,9 @@ PyDoc_STRVAR(OrderedDict_setdefault__doc__,
3937
"setdefault($self, /, key, default=None)\n"
4038
"--\n"
4139
"\n"
42-
"od.get(k,d), also set od[k]=d if k not in od.");
40+
"Insert key with a value of default if key is not in the dictionary.\n"
41+
"\n"
42+
"Return the value for key if key is in the dictionary, else default.");
4343

4444
#define ORDEREDDICT_SETDEFAULT_METHODDEF \
4545
{"setdefault", (PyCFunction)OrderedDict_setdefault, METH_FASTCALL, OrderedDict_setdefault__doc__},
@@ -71,7 +71,7 @@ PyDoc_STRVAR(OrderedDict_popitem__doc__,
7171
"popitem($self, /, last=True)\n"
7272
"--\n"
7373
"\n"
74-
"Return (k, v) and remove a (key, value) pair.\n"
74+
"Remove and return a (key, value) pair from the dictionary.\n"
7575
"\n"
7676
"Pairs are returned in LIFO order if last is true or FIFO order if false.");
7777

@@ -103,10 +103,9 @@ PyDoc_STRVAR(OrderedDict_move_to_end__doc__,
103103
"move_to_end($self, /, key, last=True)\n"
104104
"--\n"
105105
"\n"
106-
"\"Move an existing element to the end (or beginning if last==False).\n"
106+
"Move an existing element to the end (or beginning if last is false).\n"
107107
"\n"
108-
" Raises KeyError if the element does not exist.\n"
109-
" When last=True, acts like a fast version of self[key]=self.pop(key).");
108+
"Raise KeyError if the element does not exist.");
110109

111110
#define ORDEREDDICT_MOVE_TO_END_METHODDEF \
112111
{"move_to_end", (PyCFunction)OrderedDict_move_to_end, METH_FASTCALL, OrderedDict_move_to_end__doc__},
@@ -132,4 +131,4 @@ OrderedDict_move_to_end(PyODictObject *self, PyObject **args, Py_ssize_t nargs,
132131
exit:
133132
return return_value;
134133
}
135-
/*[clinic end generated code: output=84ef19e7b5db0086 input=a9049054013a1b77]*/
134+
/*[clinic end generated code: output=a19a24ac37b42e5e input=a9049054013a1b77]*/

Objects/dictobject.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -2311,12 +2311,12 @@ dict.fromkeys
23112311
value: object=None
23122312
/
23132313
2314-
Returns a new dict with keys from iterable and values equal to value.
2314+
Create a new dictionary with keys from iterable and values set to value.
23152315
[clinic start generated code]*/
23162316

23172317
static PyObject *
23182318
dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
2319-
/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
2319+
/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
23202320
{
23212321
return _PyDict_FromKeys((PyObject *)type, iterable, value);
23222322
}
@@ -2764,12 +2764,12 @@ dict.__contains__
27642764
key: object
27652765
/
27662766
2767-
True if D has a key k, else False.
2767+
True if the dictionary has the specified key, else False.
27682768
[clinic start generated code]*/
27692769

27702770
static PyObject *
27712771
dict___contains__(PyDictObject *self, PyObject *key)
2772-
/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
2772+
/*[clinic end generated code: output=a3d03db709ed6e6b input=f39613886bf975b7]*/
27732773
{
27742774
register PyDictObject *mp = self;
27752775
Py_hash_t hash;
@@ -2797,12 +2797,12 @@ dict.get
27972797
default: object = None
27982798
/
27992799
2800-
D.get(key[, default]) -> D[key] if key in D, else default.
2800+
Return the value for key if key is in the dictionary, else default.
28012801
[clinic start generated code]*/
28022802

28032803
static PyObject *
28042804
dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
2805-
/*[clinic end generated code: output=bba707729dee05bf input=e73ab0f028f4b2be]*/
2805+
/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
28062806
{
28072807
PyObject *val = NULL;
28082808
Py_hash_t hash;
@@ -2915,13 +2915,15 @@ dict.setdefault
29152915
default: object = None
29162916
/
29172917
2918-
D.get(key,default), also set D[key]=default if key not in D.
2918+
Insert key with a value of default if key is not in the dictionary.
2919+
2920+
Return the value for key if key is in the dictionary, else default.
29192921
[clinic start generated code]*/
29202922

29212923
static PyObject *
29222924
dict_setdefault_impl(PyDictObject *self, PyObject *key,
29232925
PyObject *default_value)
2924-
/*[clinic end generated code: output=f8c1101ebf69e220 input=b2826255bacd845a]*/
2926+
/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
29252927
{
29262928
PyObject *val;
29272929

Objects/odictobject.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,12 @@ OrderedDict.fromkeys
926926
iterable as seq: object
927927
value: object = None
928928
929-
New ordered dictionary with keys from S.
930-
931-
If not specified, the value defaults to None.
929+
Create a new ordered dictionary with keys from iterable and values set to value.
932930
[clinic start generated code]*/
933931

934932
static PyObject *
935933
OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value)
936-
/*[clinic end generated code: output=c10390d452d78d6d input=33eefc496d5eee7b]*/
934+
/*[clinic end generated code: output=c10390d452d78d6d input=1a0476c229c597b3]*/
937935
{
938936
return _PyDict_FromKeys((PyObject *)type, seq, value);
939937
}
@@ -1014,13 +1012,15 @@ OrderedDict.setdefault
10141012
key: object
10151013
default: object = None
10161014
1017-
od.get(k,d), also set od[k]=d if k not in od.
1015+
Insert key with a value of default if key is not in the dictionary.
1016+
1017+
Return the value for key if key is in the dictionary, else default.
10181018
[clinic start generated code]*/
10191019

10201020
static PyObject *
10211021
OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
10221022
PyObject *default_value)
1023-
/*[clinic end generated code: output=97537cb7c28464b6 input=d5e940fcea7a5a67]*/
1023+
/*[clinic end generated code: output=97537cb7c28464b6 input=38e098381c1efbc6]*/
10241024
{
10251025
PyObject *result = NULL;
10261026

@@ -1165,14 +1165,14 @@ OrderedDict.popitem
11651165
11661166
last: bool = True
11671167
1168-
Return (k, v) and remove a (key, value) pair.
1168+
Remove and return a (key, value) pair from the dictionary.
11691169
11701170
Pairs are returned in LIFO order if last is true or FIFO order if false.
11711171
[clinic start generated code]*/
11721172

11731173
static PyObject *
11741174
OrderedDict_popitem_impl(PyODictObject *self, int last)
1175-
/*[clinic end generated code: output=98e7d986690d49eb input=4937da2015939126]*/
1175+
/*[clinic end generated code: output=98e7d986690d49eb input=d992ac5ee8305e1a]*/
11761176
{
11771177
PyObject *key, *value, *item = NULL;
11781178
_ODictNode *node;
@@ -1324,15 +1324,14 @@ OrderedDict.move_to_end
13241324
key: object
13251325
last: bool = True
13261326
1327-
"Move an existing element to the end (or beginning if last==False).
1327+
Move an existing element to the end (or beginning if last is false).
13281328
1329-
Raises KeyError if the element does not exist.
1330-
When last=True, acts like a fast version of self[key]=self.pop(key).
1329+
Raise KeyError if the element does not exist.
13311330
[clinic start generated code]*/
13321331

13331332
static PyObject *
13341333
OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last)
1335-
/*[clinic end generated code: output=fafa4c5cc9b92f20 input=3b8283f7d0e15e43]*/
1334+
/*[clinic end generated code: output=fafa4c5cc9b92f20 input=d6ceff7132a2fcd7]*/
13361335
{
13371336
_ODictNode *node;
13381337

0 commit comments

Comments
 (0)