Skip to content

Commit 59068df

Browse files
[3.13] gh-137986: Fix and improve the csv functions docstrings (GH-137987) (GH-138108)
The csv.register_dialect() docstring no longer imply that it returns a dialect. All functions have now signatures. (cherry picked from commit aa1dbd4) Co-authored-by: maurycy <[email protected]>
1 parent f12e7cb commit 59068df

File tree

3 files changed

+40
-52
lines changed

3 files changed

+40
-52
lines changed

Doc/library/csv.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ The :mod:`csv` module defines the following functions:
113113
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
114114

115115

116-
.. function:: register_dialect(name[, dialect[, **fmtparams]])
116+
.. function:: register_dialect(name, /, dialect='excel', **fmtparams)
117117

118118
Associate *dialect* with *name*. *name* must be a string. The
119119
dialect can be specified either by passing a sub-class of :class:`Dialect`, or
@@ -139,7 +139,8 @@ The :mod:`csv` module defines the following functions:
139139
Return the names of all registered dialects.
140140

141141

142-
.. function:: field_size_limit([new_limit])
142+
.. function:: field_size_limit()
143+
field_size_limit(new_limit)
143144

144145
Returns the current maximum field size allowed by the parser. If *new_limit* is
145146
given, this becomes the new limit.
@@ -526,7 +527,7 @@ out surrounded by parens. This may cause some problems for other programs which
526527
read CSV files (assuming they support complex numbers at all).
527528

528529

529-
.. method:: csvwriter.writerow(row)
530+
.. method:: csvwriter.writerow(row, /)
530531

531532
Write the *row* parameter to the writer's file object, formatted according
532533
to the current :class:`Dialect`. Return the return value of the call to the
@@ -535,7 +536,7 @@ read CSV files (assuming they support complex numbers at all).
535536
.. versionchanged:: 3.5
536537
Added support of arbitrary iterables.
537538

538-
.. method:: csvwriter.writerows(rows)
539+
.. method:: csvwriter.writerows(rows, /)
539540

540541
Write all elements in *rows* (an iterable of *row* objects as described
541542
above) to the writer's file object, formatted according to the current

Modules/_csv.c

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,11 @@ join_append_lineterminator(WriterObj *self)
12911291
}
12921292

12931293
PyDoc_STRVAR(csv_writerow_doc,
1294-
"writerow(iterable)\n"
1294+
"writerow($self, row, /)\n"
1295+
"--\n\n"
1296+
"Construct and write a CSV record from an iterable of fields.\n"
12951297
"\n"
1296-
"Construct and write a CSV record from an iterable of fields. Non-string\n"
1297-
"elements will be converted to string.");
1298+
"Non-string elements will be converted to string.");
12981299

12991300
static PyObject *
13001301
csv_writerow(WriterObj *self, PyObject *seq)
@@ -1400,10 +1401,11 @@ csv_writerow(WriterObj *self, PyObject *seq)
14001401
}
14011402

14021403
PyDoc_STRVAR(csv_writerows_doc,
1403-
"writerows(iterable of iterables)\n"
1404+
"writerows($self, rows, /)\n"
1405+
"--\n\n"
1406+
"Construct and write a series of iterables to a csv file.\n"
14041407
"\n"
1405-
"Construct and write a series of iterables to a csv file. Non-string\n"
1406-
"elements will be converted to string.");
1408+
"Non-string elements will be converted to string.");
14071409

14081410
static PyObject *
14091411
csv_writerows(WriterObj *self, PyObject *seqseq)
@@ -1555,13 +1557,11 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
15551557
_csv.list_dialects
15561558
15571559
Return a list of all known dialect names.
1558-
1559-
names = csv.list_dialects()
15601560
[clinic start generated code]*/
15611561

15621562
static PyObject *
15631563
_csv_list_dialects_impl(PyObject *module)
1564-
/*[clinic end generated code: output=a5b92b215b006a6d input=8953943eb17d98ab]*/
1564+
/*[clinic end generated code: output=a5b92b215b006a6d input=ec58040aafd6a20a]*/
15651565
{
15661566
return PyDict_Keys(get_csv_state(module)->dialects);
15671567
}
@@ -1598,13 +1598,11 @@ _csv.unregister_dialect
15981598
name: object
15991599
16001600
Delete the name/dialect mapping associated with a string name.
1601-
1602-
csv.unregister_dialect(name)
16031601
[clinic start generated code]*/
16041602

16051603
static PyObject *
16061604
_csv_unregister_dialect_impl(PyObject *module, PyObject *name)
1607-
/*[clinic end generated code: output=0813ebca6c058df4 input=6b5c1557bf60c7e7]*/
1605+
/*[clinic end generated code: output=0813ebca6c058df4 input=e1cf81bfe3ba0f62]*/
16081606
{
16091607
_csvstate *module_state = get_csv_state(module);
16101608
int rc = PyDict_Pop(module_state->dialects, name, NULL);
@@ -1624,13 +1622,11 @@ _csv.get_dialect
16241622
name: object
16251623
16261624
Return the dialect instance associated with name.
1627-
1628-
dialect = csv.get_dialect(name)
16291625
[clinic start generated code]*/
16301626

16311627
static PyObject *
16321628
_csv_get_dialect_impl(PyObject *module, PyObject *name)
1633-
/*[clinic end generated code: output=aa988cd573bebebb input=edf9ddab32e448fb]*/
1629+
/*[clinic end generated code: output=aa988cd573bebebb input=74865c659dcb441f]*/
16341630
{
16351631
return get_dialect_from_registry(name, get_csv_state(module));
16361632
}
@@ -1642,15 +1638,13 @@ _csv.field_size_limit
16421638
16431639
Sets an upper limit on parsed fields.
16441640
1645-
csv.field_size_limit([limit])
1646-
16471641
Returns old limit. If limit is not given, no new limit is set and
16481642
the old limit is returned
16491643
[clinic start generated code]*/
16501644

16511645
static PyObject *
16521646
_csv_field_size_limit_impl(PyObject *module, PyObject *new_limit)
1653-
/*[clinic end generated code: output=f2799ecd908e250b input=cec70e9226406435]*/
1647+
/*[clinic end generated code: output=f2799ecd908e250b input=77db7485ee3ae90a]*/
16541648
{
16551649
_csvstate *module_state = get_csv_state(module);
16561650
Py_ssize_t old_limit = FT_ATOMIC_LOAD_SSIZE_RELAXED(module_state->field_limit);
@@ -1686,37 +1680,38 @@ PyType_Spec error_spec = {
16861680
PyDoc_STRVAR(csv_module_doc, "CSV parsing and writing.\n");
16871681

16881682
PyDoc_STRVAR(csv_reader_doc,
1689-
" csv_reader = reader(iterable [, dialect='excel']\n"
1690-
" [optional keyword args])\n"
1691-
" for row in csv_reader:\n"
1692-
" process(row)\n"
1683+
"reader($module, iterable, /, dialect='excel', **fmtparams)\n"
1684+
"--\n\n"
1685+
"Return a reader object that will process lines from the given iterable.\n"
16931686
"\n"
16941687
"The \"iterable\" argument can be any object that returns a line\n"
16951688
"of input for each iteration, such as a file object or a list. The\n"
1696-
"optional \"dialect\" parameter is discussed below. The function\n"
1689+
"optional \"dialect\" argument defines a CSV dialect. The function\n"
16971690
"also accepts optional keyword arguments which override settings\n"
16981691
"provided by the dialect.\n"
16991692
"\n"
17001693
"The returned object is an iterator. Each iteration returns a row\n"
17011694
"of the CSV file (which can span multiple input lines).\n");
17021695

17031696
PyDoc_STRVAR(csv_writer_doc,
1704-
" csv_writer = csv.writer(fileobj [, dialect='excel']\n"
1705-
" [optional keyword args])\n"
1706-
" for row in sequence:\n"
1707-
" csv_writer.writerow(row)\n"
1697+
"writer($module, fileobj, /, dialect='excel', **fmtparams)\n"
1698+
"--\n\n"
1699+
"Return a writer object that will write user data on the given file object.\n"
17081700
"\n"
1709-
" [or]\n"
1710-
"\n"
1711-
" csv_writer = csv.writer(fileobj [, dialect='excel']\n"
1712-
" [optional keyword args])\n"
1713-
" csv_writer.writerows(rows)\n"
1714-
"\n"
1715-
"The \"fileobj\" argument can be any object that supports the file API.\n");
1701+
"The \"fileobj\" argument can be any object that supports the file API.\n"
1702+
"The optional \"dialect\" argument defines a CSV dialect. The function\n"
1703+
"also accepts optional keyword arguments which override settings\n"
1704+
"provided by the dialect.\n");
17161705

17171706
PyDoc_STRVAR(csv_register_dialect_doc,
1718-
"Create a mapping from a string name to a dialect class.\n"
1719-
" dialect = csv.register_dialect(name[, dialect[, **fmtparams]])");
1707+
"register_dialect($module, name, /, dialect='excel', **fmtparams)\n"
1708+
"--\n\n"
1709+
"Create a mapping from a string name to a CVS dialect.\n"
1710+
"\n"
1711+
"The optional \"dialect\" argument specifies the base dialect instance\n"
1712+
"or the name of the registered dialect. The function also accepts\n"
1713+
"optional keyword arguments which override settings provided by the\n"
1714+
"dialect.\n");
17201715

17211716
static struct PyMethodDef csv_methods[] = {
17221717
{ "reader", _PyCFunction_CAST(csv_reader),

Modules/clinic/_csv.c.h

Lines changed: 4 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)