Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a284a54

Browse files
committed
Write data parameter docs as regular parameter not as note (v2)
Replaces matplotlib#19859. This approach uses an explicit template ``` data : indexable object, optional # data_parameter_message ``` in the docstring and does not try to guess a reasonable insertion position based on the docstring content. This is much simpler.
1 parent 9b9c42d commit a284a54

File tree

7 files changed

+154
-29
lines changed

7 files changed

+154
-29
lines changed

‎lib/matplotlib/__init__.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,24 +1256,6 @@ def _label_from_arg(y, default_name):
12561256
return None
12571257

12581258

1259-
_DATA_DOC_TITLE = """
1260-
1261-
Notes
1262-
-----
1263-
"""
1264-
1265-
_DATA_DOC_APPENDIX = """
1266-
1267-
.. note::
1268-
In addition to the above described arguments, this function can take
1269-
a *data* keyword argument. If such a *data* argument is given,
1270-
{replaced}
1271-
1272-
Objects passed as **data** must support item access (``data[s]``) and
1273-
membership test (``s in data``).
1274-
"""
1275-
1276-
12771259
def _add_data_doc(docstring, replace_names):
12781260
"""
12791261
Add documentation for a *data* field to the given docstring.
@@ -1296,17 +1278,35 @@ def _add_data_doc(docstring, replace_names):
12961278
or replace_names is not None and len(replace_names) == 0):
12971279
return docstring
12981280
docstring = inspect.cleandoc(docstring)
1299-
repl = (
1300-
(" every other argument can also be string ``s``, which is\n"
1301-
" interpreted as ``data[s]`` (unless this raises an exception).")
1302-
if replace_names is None else
1303-
(" the following arguments can also be string ``s``, which is\n"
1304-
" interpreted as ``data[s]`` (unless this raises an exception):\n"
1305-
" " + ", ".join(map("*{}*".format, replace_names))) + ".")
1306-
addendum = _DATA_DOC_APPENDIX.format(replaced=repl)
1307-
if _DATA_DOC_TITLE not in docstring:
1308-
addendum = _DATA_DOC_TITLE + addendum
1309-
return docstring + addendum
1281+
1282+
data_doc = ("""\
1283+
If given, all parameters also accept a string ``s``, which is
1284+
interpreted as ``data[s]`` (unless this raises an exception).
1285+
1286+
Objects passed as **data** must support item access (``data[s]``) and
1287+
membership test (``s in data``)."""
1288+
if replace_names is None else ("""\
1289+
If given, the following parameters also accept a string ``s``, which
1290+
is interpreted as ``data[s]`` (unless this raises an exception):
1291+
1292+
{names}
1293+
1294+
Objects passed as **data** must support item access (``data[s]``) and
1295+
membership test (``s in data``).""".format(
1296+
names=", ".join(map("*{}*".format, replace_names)))
1297+
)
1298+
)
1299+
# using string replacement instead of formatting has the advantages
1300+
# 1) simpler indent handling
1301+
# 2) prevent problems with formatting characters '{', '%' in the docstring
1302+
if _log.level <= logging.DEBUG:
1303+
# test_data_parameter_replacement() tests against these log messages
1304+
# make sure to keep message and test in sync
1305+
if "data : indexable object, optional" not in docstring:
1306+
_log.debug("data parameter docstring error: no data parameter")
1307+
if '# data_parameter_message' not in docstring:
1308+
_log.debug("data parameter docstring error: missing placeholder")
1309+
return docstring.replace(' # data_parameter_message', data_doc)
13101310

13111311

13121312
def _preprocess_data(func=None, *, replace_names=None, label_namer=None):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /