Skip to content

Commit 7cfcc6b

Browse files
authored
Fix versionadded directive formatting (#13471)
1 parent 1815534 commit 7cfcc6b

File tree

9 files changed

+96
-3
lines changed

9 files changed

+96
-3
lines changed

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"newcontrib_substitutions",
129129
"unit_role",
130130
"related_software",
131+
"directive_formatting",
131132
]
132133

133134
# Add any paths that contain templates here, relative to this directory.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Authors: The MNE-Python contributors.
2+
# License: BSD-3-Clause
3+
# Copyright the MNE-Python contributors.
4+
5+
import re
6+
7+
from mne_doc_utils import sphinx_logger
8+
9+
10+
def setup(app):
11+
app.connect("source-read", check_directive_formatting)
12+
app.connect("autodoc-process-docstring", check_directive_formatting)
13+
14+
15+
def setup_module():
16+
# HACK: Stop nosetests running setup() above
17+
pass
18+
19+
20+
def check_directive_formatting(*args):
21+
"""Check that directives are not missing a space.
22+
23+
For args, see Sphinx events 'source-read' and 'autodoc-process-docstring'.
24+
"""
25+
# Extract relevant info from args
26+
if len(args) == 3: # from source-read
27+
source_type = "File"
28+
name = args[1]
29+
source = args[2][0]
30+
source_concat = source # content already a single string
31+
elif len(args) == 6: # from autodoc-process-docstring
32+
source_type = "Docstring"
33+
name = args[2]
34+
source = args[5]
35+
source_concat = "\n".join(source) # combine lines into single string
36+
else:
37+
raise RuntimeError("Unexpected number of arguments from Sphinx event")
38+
39+
# Check if any directives are present
40+
if re.search(r"\.\.\s*[a-zA-Z]+::", source_concat) is None:
41+
return
42+
43+
# Separate content into lines (docstrings already are)
44+
if source_type == "File":
45+
source = source.split("\n")
46+
47+
# Check for bad formatting
48+
for idx, line in enumerate(source):
49+
# Check for missing space after '..'
50+
missing = re.search(r"\.\.[a-zA-Z]+::", line)
51+
if missing is not None:
52+
sphinx_logger.warning(
53+
f"{source_type} '{name}' is missing a space after '..' in the "
54+
f"directive '{missing.group()}'"
55+
)
56+
# Extra spaces after '..' don't affect formatting
57+
58+
# Check for missing preceding blank line
59+
# (exceptions are for directives at the start of files, after a header, or after
60+
# another directive/another directive's content)
61+
if idx == 0:
62+
continue
63+
dir_pattern = r"\.\. [a-zA-Z]+::"
64+
head_pattern = r"^[-|=|\^]+$"
65+
directive = re.search(dir_pattern, line)
66+
if directive is not None:
67+
line_prev = source[idx - 1].strip()
68+
if ( # If previous line is...
69+
line_prev != "" # not empty
70+
and not re.search(head_pattern, line_prev) # not a header
71+
and not re.search(dir_pattern, line_prev) # not a directive
72+
):
73+
# Check if previous line is part of another directive
74+
bad = True
75+
for line_prev in reversed(source[: idx - 1]):
76+
line_prev = line_prev.strip()
77+
if line_prev == "" or re.search(head_pattern, line_prev):
78+
# is a blank line or header, so not part of another directive
79+
break # must be bad formatting
80+
if re.search(dir_pattern, line_prev):
81+
bad = False # is part of another directive, is good formatting
82+
break
83+
# or keep going until we reach the first line (so must be bad)
84+
if bad:
85+
sphinx_logger.warning(
86+
f"{source_type} '{name}' is missing a blank line before the "
87+
f"directive '{directive.group()}'"
88+
)

examples/preprocessing/css.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def subcortical_waveform(times):
9999
ax.set(ylabel="EEG Power spectral density", xlabel="Frequency (Hz)")
100100
ax.legend()
101101

102+
###############################################################################
102103
# References
103104
# ^^^^^^^^^^
104105
#

examples/time_frequency/compute_source_psd_epochs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
# Compute source space PSD in label
7373
# ---------------------------------
7474
#
75-
# ..note:: By using "return_generator=True" stcs will be a generator object
75+
# .. note:: By using "return_generator=True" stcs will be a generator object
7676
# instead of a list. This allows us so to iterate without having to
7777
# keep everything in memory.
7878

mne/epochs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,7 @@ def save(
22112211
-------
22122212
fnames : List of path-like
22132213
List of path-like objects containing the path to each file split.
2214+
22142215
.. versionadded:: 1.9
22152216
22162217
Notes

mne/io/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,7 @@ def save(
17551755
-------
17561756
fnames : List of path-like
17571757
List of path-like objects containing the path to each file split.
1758+
17581759
.. versionadded:: 1.9
17591760
17601761
Notes

mne/io/egi/egi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def read_raw_egi(
131131
Channel naming convention for the data channels. Defaults to ``'E%%d'``
132132
(resulting in channel names ``'E1'``, ``'E2'``, ``'E3'``...). The
133133
effective default prior to 0.14.0 was ``'EEG %%03d'``.
134+
134135
.. versionadded:: 0.14.0
135136
136137
events_as_annotations : bool

mne/io/nicolet/nicolet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def read_raw_nicolet(
2121
) -> "RawNicolet":
2222
"""Read Nicolet data as raw object.
2323
24-
..note:: This reader takes data files with the extension ``.data`` as an
24+
.. note:: This reader takes data files with the extension ``.data`` as an
2525
input. The header file with the same file name stem and an
2626
extension ``.head`` is expected to be found in the same
2727
directory.

mne/label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def compute_area(
949949
950950
Notes
951951
-----
952-
..versionadded:: 0.24
952+
.. versionadded:: 0.24
953953
"""
954954
_, _, surf = self._load_surface(
955955
subject, subjects_dir, surface, return_dict=True

0 commit comments

Comments
 (0)