Skip to content

Commit 681962d

Browse files
committed
Change plurality in the message depending on the size of the list.
1 parent cb9a68d commit 681962d

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

Tools/check-c-api-docs/main.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,72 @@
22
from pathlib import Path
33
import sys
44
import _colorize
5+
import textwrap
56

67
SIMPLE_FUNCTION_REGEX = re.compile(r"PyAPI_FUNC(.+) (\w+)\(")
78
SIMPLE_MACRO_REGEX = re.compile(r"# *define *(\w+)(\(.+\))? ")
89
SIMPLE_INLINE_REGEX = re.compile(r"static inline .+( |\n)(\w+)")
910
SIMPLE_DATA_REGEX = re.compile(r"PyAPI_DATA\(.+\) (\w+)")
1011

12+
CPYTHON = Path(__file__).parent.parent.parent
13+
INCLUDE = CPYTHON / "Include"
14+
C_API_DOCS = CPYTHON / "Doc" / "c-api"
15+
IGNORED = (
16+
(CPYTHON / "Tools" / "check-c-api-docs" / "ignored_c_api.txt")
17+
.read_text()
18+
.split("\n")
19+
)
20+
21+
for index, line in enumerate(IGNORED):
22+
if line.startswith("#"):
23+
IGNORED.pop(index)
24+
1125
MISTAKE = """\
1226
If this is a mistake and this script should not be failing, create an
1327
issue and tag Peter (@ZeroIntensity) on it.\
1428
"""
1529

16-
FOUND_UNDOCUMENTED = f"""\
17-
Found some undocumented C API(s)!
1830

19-
Python requires documentation on all public C API symbols, macros, and types.
20-
If these API(s) were not meant to be public, prefix them with a
21-
leading underscore (_PySomething_API) or move them to the internal C API
22-
(pycore_*.h files).
31+
def found_undocumented(singular: bool):
32+
some = "an" if singular else "some"
33+
s = "" if singular else "s"
34+
these = "this" if singular else "these"
35+
them = "it" if singular else "them"
36+
were = "was" if singular else "were"
2337

24-
In exceptional cases, certain functions can be ignored by adding them to
25-
Tools/c-api-docs-check/ignored_c_api.txt
38+
return textwrap.dedent(
39+
f"""\
40+
Found {some} undocumented C API{s}!
2641
27-
{MISTAKE}\
28-
"""
42+
Python requires documentation on all public C API symbols, macros, and types.
43+
If {these} API{s} {were} not meant to be public, prefix {them} with a
44+
leading underscore (_PySomething_API) or move {them} to the internal C API
45+
(pycore_*.h files).
2946
30-
FOUND_IGNORED_DOCUMENTED = f"""\
31-
Some C API(s) were listed in Tools/c-api-docs-check/ignored_c_api.txt, but
32-
they were found in the documentation. To fix this, remove them from
33-
ignored_c_api.txt.
47+
In exceptional cases, certain APIs can be ignored by adding them to
48+
Tools/c-api-docs-check/ignored_c_api.txt
3449
35-
{MISTAKE}\
36-
"""
50+
{MISTAKE}\
51+
"""
52+
)
3753

38-
_CPYTHON = Path(__file__).parent.parent.parent
39-
INCLUDE = _CPYTHON / "Include"
40-
C_API_DOCS = _CPYTHON / "Doc" / "c-api"
41-
IGNORED = (
42-
(_CPYTHON / "Tools" / "check-c-api-docs" / "ignored_c_api.txt")
43-
.read_text()
44-
.split("\n")
45-
)
4654

47-
for index, line in enumerate(IGNORED):
48-
if line.startswith("#"):
49-
IGNORED.pop(index)
55+
def found_ignored_documented(singular: bool) -> str:
56+
some = "a" if singular else "some"
57+
s = "" if singular else "s"
58+
them = "it" if singular else "them"
59+
were = "was" if singular else "were"
60+
they = "it" if singular else "they"
61+
62+
return textwrap.dedent(
63+
f"""\
64+
Found {some} C API{s} listed in Tools/c-api-docs-check/ignored_c_api.txt, but
65+
{they} {were} found in the documentation. To fix this, remove {them} from
66+
ignored_c_api.txt.
67+
68+
{MISTAKE}\
69+
"""
70+
)
5071

5172

5273
def is_documented(name: str) -> bool:
@@ -145,17 +166,22 @@ def main() -> None:
145166

146167
fail = False
147168
to_check = [
148-
(all_missing, "missing", FOUND_UNDOCUMENTED),
149-
(all_found_ignored, "documented but ignored", FOUND_IGNORED_DOCUMENTED),
169+
(all_missing, "missing", found_undocumented(len(all_missing) == 1)),
170+
(
171+
all_found_ignored,
172+
"documented but ignored",
173+
found_ignored_documented(len(all_found_ignored) == 1),
174+
),
150175
]
151176
for name_list, what, message in to_check:
152177
if not name_list:
153178
continue
154179

155180
s = "s" if len(name_list) != 1 else ""
156181
print(f"-- {len(name_list)} {what} C API{s} --")
157-
for name in all_missing:
182+
for name in name_list:
158183
print(f" - {name}")
184+
print()
159185
print(message)
160186
fail = True
161187

0 commit comments

Comments
 (0)