Skip to content

Commit a7d16d9

Browse files
serhiy-storchakaGlyphack
authored andcommitted
pythongh-111999: Add signatures and improve docstrings for builtins (pythonGH-112000)
1 parent 68ce15f commit a7d16d9

File tree

8 files changed

+74
-39
lines changed

8 files changed

+74
-39
lines changed

Lib/test/test_inspect/test_inspect.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,12 +4734,13 @@ def test_builtins_have_signatures(self):
47344734
needs_null = {"anext"}
47354735
no_signature |= needs_null
47364736
# These need *args support in Argument Clinic
4737-
needs_varargs = {"breakpoint", "min", "max", "__build_class__"}
4737+
needs_varargs = {"min", "max", "__build_class__"}
47384738
no_signature |= needs_varargs
47394739
# These builtin types are expected to provide introspection info
47404740
types_with_signatures = {
4741-
'complex', 'enumerate', 'float', 'list', 'memoryview', 'object',
4742-
'property', 'reversed', 'tuple',
4741+
'bool', 'classmethod', 'complex', 'enumerate', 'filter', 'float',
4742+
'frozenset', 'list', 'map', 'memoryview', 'object', 'property',
4743+
'reversed', 'set', 'staticmethod', 'tuple', 'zip'
47434744
}
47444745
# Check the signatures we expect to be there
47454746
ns = vars(builtins)
@@ -4753,6 +4754,10 @@ def test_builtins_have_signatures(self):
47534754
if (name in no_signature):
47544755
# Not yet converted
47554756
continue
4757+
if name in {'classmethod', 'staticmethod'}:
4758+
# Bug gh-112006: inspect.unwrap() does not work with types
4759+
# with the __wrapped__ data descriptor.
4760+
continue
47564761
with self.subTest(builtin=name):
47574762
self.assertIsNotNone(inspect.signature(obj))
47584763
# Check callables that haven't been converted don't claim a signature

Objects/boolobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ bool_xor(PyObject *a, PyObject *b)
110110
/* Doc string */
111111

112112
PyDoc_STRVAR(bool_doc,
113-
"bool(x) -> bool\n\
113+
"bool(object=False, /)\n\
114+
--\n\
114115
\n\
115-
Returns True when the argument x is true, False otherwise.\n\
116+
Returns True when the argument is true, False otherwise.\n\
116117
The builtins True and False are the only two instances of the class bool.\n\
117118
The class bool is a subclass of the class int, and cannot be subclassed.");
118119

Objects/descrobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,9 @@ property_deleter(PyObject *self, PyObject *deleter)
15411541

15421542

15431543
PyDoc_STRVAR(set_name_doc,
1544+
"__set_name__($self, owner, name, /)\n"
1545+
"--\n"
1546+
"\n"
15441547
"Method to set name of a property.");
15451548

15461549
static PyObject *

Objects/funcobject.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,8 @@ cm_repr(classmethod *cm)
11641164
}
11651165

11661166
PyDoc_STRVAR(classmethod_doc,
1167-
"classmethod(function) -> method\n\
1167+
"classmethod(function, /)\n\
1168+
--\n\
11681169
\n\
11691170
Convert a function to be a class method.\n\
11701171
\n\
@@ -1359,7 +1360,8 @@ sm_repr(staticmethod *sm)
13591360
}
13601361

13611362
PyDoc_STRVAR(staticmethod_doc,
1362-
"staticmethod(function) -> method\n\
1363+
"staticmethod(function, /)\n\
1364+
--\n\
13631365
\n\
13641366
Convert a function to be a static method.\n\
13651367
\n\

Objects/setobject.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,10 @@ set_update(PySetObject *so, PyObject *args)
942942
}
943943

944944
PyDoc_STRVAR(update_doc,
945-
"Update a set with the union of itself and others.");
945+
"update($self, /, *others)\n\
946+
--\n\
947+
\n\
948+
Update the set, adding elements from all others.");
946949

947950
/* XXX Todo:
948951
If aligned memory allocations become available, make the
@@ -1141,9 +1144,10 @@ set_union(PySetObject *so, PyObject *args)
11411144
}
11421145

11431146
PyDoc_STRVAR(union_doc,
1144-
"Return the union of sets as a new set.\n\
1147+
"union($self, /, *others)\n\
1148+
--\n\
11451149
\n\
1146-
(i.e. all elements that are in either set.)");
1150+
Return a new set with elements from the set and all others.");
11471151

11481152
static PyObject *
11491153
set_or(PySetObject *so, PyObject *other)
@@ -1281,9 +1285,10 @@ set_intersection_multi(PySetObject *so, PyObject *args)
12811285
}
12821286

12831287
PyDoc_STRVAR(intersection_doc,
1284-
"Return the intersection of two sets as a new set.\n\
1288+
"intersection($self, /, *others)\n\
1289+
--\n\
12851290
\n\
1286-
(i.e. all elements that are in both sets.)");
1291+
Return a new set with elements common to the set and all others.");
12871292

12881293
static PyObject *
12891294
set_intersection_update(PySetObject *so, PyObject *other)
@@ -1312,7 +1317,10 @@ set_intersection_update_multi(PySetObject *so, PyObject *args)
13121317
}
13131318

13141319
PyDoc_STRVAR(intersection_update_doc,
1315-
"Update a set with the intersection of itself and another.");
1320+
"intersection_update($self, /, *others)\n\
1321+
--\n\
1322+
\n\
1323+
Update the set, keeping only elements found in it and all others.");
13161324

13171325
static PyObject *
13181326
set_and(PySetObject *so, PyObject *other)
@@ -1470,7 +1478,10 @@ set_difference_update(PySetObject *so, PyObject *args)
14701478
}
14711479

14721480
PyDoc_STRVAR(difference_update_doc,
1473-
"Remove all elements of another set from this set.");
1481+
"difference_update($self, /, *others)\n\
1482+
--\n\
1483+
\n\
1484+
Update the set, removing elements found in others.");
14741485

14751486
static PyObject *
14761487
set_copy_and_difference(PySetObject *so, PyObject *other)
@@ -1587,9 +1598,10 @@ set_difference_multi(PySetObject *so, PyObject *args)
15871598
}
15881599

15891600
PyDoc_STRVAR(difference_doc,
1590-
"Return the difference of two or more sets as a new set.\n\
1601+
"difference($self, /, *others)\n\
1602+
--\n\
15911603
\n\
1592-
(i.e. all elements that are in this set but not the others.)");
1604+
Return a new set with elements in the set that are not in the others.");
15931605
static PyObject *
15941606
set_sub(PySetObject *so, PyObject *other)
15951607
{
@@ -1673,7 +1685,10 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other)
16731685
}
16741686

16751687
PyDoc_STRVAR(symmetric_difference_update_doc,
1676-
"Update a set with the symmetric difference of itself and another.");
1688+
"symmetric_difference_update($self, other, /)\n\
1689+
--\n\
1690+
\n\
1691+
Update the set, keeping only elements found in either set, but not in both.");
16771692

16781693
static PyObject *
16791694
set_symmetric_difference(PySetObject *so, PyObject *other)
@@ -1694,9 +1709,10 @@ set_symmetric_difference(PySetObject *so, PyObject *other)
16941709
}
16951710

16961711
PyDoc_STRVAR(symmetric_difference_doc,
1697-
"Return the symmetric difference of two sets as a new set.\n\
1712+
"symmetric_difference($self, other, /)\n\
1713+
--\n\
16981714
\n\
1699-
(i.e. all elements that are in exactly one of the sets.)");
1715+
Return a new set with elements in either the set or other but not both.");
17001716

17011717
static PyObject *
17021718
set_xor(PySetObject *so, PyObject *other)
@@ -2100,8 +2116,8 @@ static PyNumberMethods set_as_number = {
21002116
};
21012117

21022118
PyDoc_STRVAR(set_doc,
2103-
"set() -> new empty set object\n\
2104-
set(iterable) -> new set object\n\
2119+
"set(iterable=(), /)\n\
2120+
--\n\
21052121
\n\
21062122
Build an unordered collection of unique elements.");
21072123

@@ -2201,8 +2217,8 @@ static PyNumberMethods frozenset_as_number = {
22012217
};
22022218

22032219
PyDoc_STRVAR(frozenset_doc,
2204-
"frozenset() -> empty frozenset object\n\
2205-
frozenset(iterable) -> frozenset object\n\
2220+
"frozenset(iterable=(), /)\n\
2221+
--\n\
22062222
\n\
22072223
Build an immutable unordered collection of unique elements.");
22082224

Objects/typeobject.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5254,8 +5254,10 @@ static PyMethodDef type_methods[] = {
52545254
TYPE___SUBCLASSES___METHODDEF
52555255
{"__prepare__", _PyCFunction_CAST(type_prepare),
52565256
METH_FASTCALL | METH_KEYWORDS | METH_CLASS,
5257-
PyDoc_STR("__prepare__() -> dict\n"
5258-
"used to create the namespace for the class statement")},
5257+
PyDoc_STR("__prepare__($cls, name, bases, /, **kwds)\n"
5258+
"--\n"
5259+
"\n"
5260+
"Create the namespace for the class statement")},
52595261
TYPE___INSTANCECHECK___METHODDEF
52605262
TYPE___SUBCLASSCHECK___METHODDEF
52615263
TYPE___DIR___METHODDEF

Objects/unicodeobject.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13390,15 +13390,17 @@ _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer)
1339013390
#include "stringlib/unicode_format.h"
1339113391

1339213392
PyDoc_STRVAR(format__doc__,
13393-
"S.format(*args, **kwargs) -> str\n\
13393+
"format($self, /, *args, **kwargs)\n\
13394+
--\n\
1339413395
\n\
13395-
Return a formatted version of S, using substitutions from args and kwargs.\n\
13396+
Return a formatted version of the string, using substitutions from args and kwargs.\n\
1339613397
The substitutions are identified by braces ('{' and '}').");
1339713398

1339813399
PyDoc_STRVAR(format_map__doc__,
13399-
"S.format_map(mapping) -> str\n\
13400+
"format_map($self, /, mapping)\n\
13401+
--\n\
1340013402
\n\
13401-
Return a formatted version of S, using substitutions from mapping.\n\
13403+
Return a formatted version of the string, using substitutions from mapping.\n\
1340213404
The substitutions are identified by braces ('{' and '}').");
1340313405

1340413406
/*[clinic input]
@@ -14696,7 +14698,7 @@ errors is specified, then the object must expose a data buffer\n\
1469614698
that will be decoded using the given encoding and error handler.\n\
1469714699
Otherwise, returns the result of object.__str__() (if defined)\n\
1469814700
or repr(object).\n\
14699-
encoding defaults to sys.getdefaultencoding().\n\
14701+
encoding defaults to 'utf-8'.\n\
1470014702
errors defaults to 'strict'.");
1470114703

1470214704
static PyObject *unicode_iter(PyObject *seq);

Python/bltinmodule.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
482482

483483
PyDoc_STRVAR(breakpoint_doc,
484484
"breakpoint(*args, **kws)\n\
485+
--\n\
485486
\n\
486487
Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
487488
whatever arguments are passed.\n\
@@ -626,7 +627,8 @@ static PyMethodDef filter_methods[] = {
626627
};
627628

628629
PyDoc_STRVAR(filter_doc,
629-
"filter(function or None, iterable) --> filter object\n\
630+
"filter(function, iterable, /)\n\
631+
--\n\
630632
\n\
631633
Return an iterator yielding those items of iterable for which function(item)\n\
632634
is true. If function is None, return the items that are true.");
@@ -1449,7 +1451,8 @@ static PyMethodDef map_methods[] = {
14491451

14501452

14511453
PyDoc_STRVAR(map_doc,
1452-
"map(func, *iterables) --> map object\n\
1454+
"map(function, /, *iterables)\n\
1455+
--\n\
14531456
\n\
14541457
Make an iterator that computes the function using arguments from\n\
14551458
each of the iterables. Stops when the shortest iterable is exhausted.");
@@ -1888,7 +1891,7 @@ min(arg1, arg2, *args, *[, key=func]) -> value\n\
18881891
With a single iterable argument, return its smallest item. The\n\
18891892
default keyword-only argument specifies an object to return if\n\
18901893
the provided iterable is empty.\n\
1891-
With two or more arguments, return the smallest argument.");
1894+
With two or more positional arguments, return the smallest argument.");
18921895

18931896

18941897
/* AC: cannot convert yet, waiting for *args support */
@@ -1905,7 +1908,7 @@ max(arg1, arg2, *args, *[, key=func]) -> value\n\
19051908
With a single iterable argument, return its biggest item. The\n\
19061909
default keyword-only argument specifies an object to return if\n\
19071910
the provided iterable is empty.\n\
1908-
With two or more arguments, return the largest argument.");
1911+
With two or more positional arguments, return the largest argument.");
19091912

19101913

19111914
/*[clinic input]
@@ -2958,18 +2961,19 @@ static PyMethodDef zip_methods[] = {
29582961
};
29592962

29602963
PyDoc_STRVAR(zip_doc,
2961-
"zip(*iterables, strict=False) --> Yield tuples until an input is exhausted.\n\
2962-
\n\
2963-
>>> list(zip('abcdefg', range(3), range(4)))\n\
2964-
[('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]\n\
2964+
"zip(*iterables, strict=False)\n\
2965+
--\n\
29652966
\n\
29662967
The zip object yields n-length tuples, where n is the number of iterables\n\
29672968
passed as positional arguments to zip(). The i-th element in every tuple\n\
29682969
comes from the i-th iterable argument to zip(). This continues until the\n\
29692970
shortest argument is exhausted.\n\
29702971
\n\
29712972
If strict is true and one of the arguments is exhausted before the others,\n\
2972-
raise a ValueError.");
2973+
raise a ValueError.\n\
2974+
\n\
2975+
>>> list(zip('abcdefg', range(3), range(4)))\n\
2976+
[('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]");
29732977

29742978
PyTypeObject PyZip_Type = {
29752979
PyVarObject_HEAD_INIT(&PyType_Type, 0)

0 commit comments

Comments
 (0)