Skip to content

Commit bd97b93

Browse files
[doc framework] Assert that the good and bad example exists in the doc (#9936)
1 parent 7aa4436 commit bd97b93

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

doc/data/messages/u/unrecognize-option/details.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

doc/data/messages/u/unrecognized-option/details.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ One of your options is not recognized. There's nothing to change in
22
your code, but your pylint configuration or the way you launch
33
pylint needs to be modified.
44

5-
For example you might be launching pylint with the following ``toml`` configuration::
5+
For example, this message would be raised when invoking pylint with
6+
``pylint --unknown-option=yes test.py``. Or you might be launching
7+
pylint with the following ``toml`` configuration::
68

79
[tool.pylint]
810
jars = "10"

doc/exts/pylint_messages.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434

3535
MSG_TYPES_DOC = {k: v if v != "info" else "information" for k, v in MSG_TYPES.items()}
3636

37+
MESSAGES_WITHOUT_EXAMPLES = {
38+
"astroid-error", # internal
39+
"bad-configuration-section", # configuration
40+
"bad-plugin-value", # internal
41+
"c-extension-no-member", # not easy to implement in the current doc framework
42+
"config-parse-error", # configuration
43+
"fatal", # internal
44+
"import-self", # not easy to implement in the current doc framework
45+
"invalid-character-nul", # not easy to implement in the current doc framework
46+
"invalid-characters-in-docstring", # internal in py-enchant
47+
"invalid-unicode-codec", # placeholder (not implemented yet)
48+
"method-check-failed", # internal
49+
"parse-error", # internal
50+
"raw-checker-failed", # internal
51+
"unrecognized-option", # configuration
52+
}
53+
MESSAGES_WITHOUT_BAD_EXAMPLES = {
54+
"invalid-character-carriage-return", # can't be raised in normal pylint use
55+
"return-arg-in-generator", # can't be raised in modern python
56+
}
57+
3758

3859
class MessageData(NamedTuple):
3960
checker: str
@@ -99,6 +120,11 @@ def _get_pylintrc_code(data_path: Path) -> str:
99120

100121
def _get_demo_code_for(data_path: Path, example_type: ExampleType) -> str:
101122
"""Get code examples while handling multi-file code templates."""
123+
if data_path.name in MESSAGES_WITHOUT_EXAMPLES or (
124+
data_path.name in MESSAGES_WITHOUT_BAD_EXAMPLES
125+
and example_type is ExampleType.BAD
126+
):
127+
return ""
102128
single_file_path = data_path / f"{example_type.value}.py"
103129
multiple_code_path = data_path / f"{example_type.value}"
104130

@@ -130,8 +156,9 @@ def _get_demo_code_for(data_path: Path, example_type: ExampleType) -> str:
130156
"""
131157
)
132158
return _get_titled_rst(title=title, text="\n".join(files))
133-
134-
return ""
159+
raise AssertionError(
160+
f"Please add a {example_type.value} code example for {data_path}"
161+
)
135162

136163

137164
def _check_placeholders(
@@ -187,9 +214,7 @@ def _get_ini_as_rst(code_path: Path) -> str:
187214
"""
188215

189216

190-
def _get_all_messages(
191-
linter: PyLinter,
192-
) -> tuple[MessagesDict, OldMessagesDict]:
217+
def _get_all_messages(linter: PyLinter) -> tuple[MessagesDict, OldMessagesDict]:
193218
"""Get all messages registered to a linter and return a dictionary indexed by
194219
message type.
195220
@@ -241,8 +266,8 @@ def _get_all_messages(
241266
if message.old_names:
242267
for old_name in message.old_names:
243268
category = MSG_TYPES_DOC[old_name[0][0]]
244-
# We check if the message is already in old_messages so
245-
# we don't duplicate shared messages.
269+
# We check if the message is already in old_messages, so we don't
270+
# duplicate shared messages.
246271
if (message.symbol, msg_type) not in old_messages[category][
247272
(old_name[1], old_name[0])
248273
]:
@@ -328,7 +353,7 @@ def _generate_single_message_body(message: MessageData) -> str:
328353
329354
"""
330355

331-
body += "\n" + message.example_code + "\n"
356+
body += f"\n{message.example_code}\n"
332357

333358
if message.checker_module_name.startswith("pylint.extensions."):
334359
body += f"""

0 commit comments

Comments
 (0)