Skip to content

Commit 536087e

Browse files
authored
Set up system of code examples and details for message documentation (#5934)
1 parent 9c90db1 commit 536087e

File tree

9 files changed

+89
-3
lines changed

9 files changed

+89
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
rev: v1.4
1414
hooks:
1515
- id: autoflake
16-
exclude: &fixtures tests/functional/|tests/input|tests/regrtest_data/|tests/data/
16+
exclude: &fixtures tests/functional/|tests/input|tests/regrtest_data/|tests/data/|doc/data/messages
1717
args:
1818
- --in-place
1919
- --remove-all-unused-imports

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
# List of patterns, relative to source directory, that match files and
7575
# directories to ignore when looking for source files.
76-
exclude_patterns = ["_build"]
76+
exclude_patterns = ["_build", "data/**"]
7777

7878
# The reST default role (used for this markup: `text`) to use for all documents.
7979
# default_role = None
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo():
2+
pass # [emtpy-docstring]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo():
2+
"""A dummy description."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def foo():
2+
yield from [1, 2, 3] # [yield-inside-async-function]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The message can't be emitted when using Python < 3.5.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
async def foo():
2+
def _inner_foo():
3+
yield from [1, 2, 3]
4+
5+
6+
async def foo():
7+
yield 42
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `PEP 525 <https://peps.python.org/pep-0525/>`_

doc/exts/pylint_messages.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
PYLINT_MESSAGES_PATH = PYLINT_BASE_PATH / "doc" / "messages"
2424
"""Path to the messages documentation folder."""
2525

26+
PYLINT_MESSAGES_DATA_PATH = PYLINT_BASE_PATH / "doc" / "data" / "messages"
27+
"""Path to the folder with data for the messages documentation."""
2628

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

@@ -32,6 +34,10 @@ class MessageData(NamedTuple):
3234
id: str
3335
name: str
3436
definition: MessageDefinition
37+
good_code: str
38+
bad_code: str
39+
details: str
40+
related_links: str
3541

3642

3743
MessagesDict = Dict[str, List[MessageData]]
@@ -47,6 +53,54 @@ def _register_all_checkers_and_extensions(linter: PyLinter) -> None:
4753
initialize_extensions(linter)
4854

4955

56+
def _get_message_data(data_path: Path) -> Tuple[str, str, str, str]:
57+
"""Get the message data from the specified path."""
58+
good_code, bad_code, details, related = "", "", "", ""
59+
60+
if not data_path.exists():
61+
return good_code, bad_code, details, related
62+
63+
if (data_path / "good.py").exists():
64+
with open(data_path / "good.py", encoding="utf-8") as file:
65+
file_content = file.readlines()
66+
indented_file_content = "".join(" " + i for i in file_content)
67+
good_code = f"""
68+
**Correct code:**
69+
70+
.. code-block:: python
71+
72+
{indented_file_content}"""
73+
74+
if (data_path / "bad.py").exists():
75+
with open(data_path / "bad.py", encoding="utf-8") as file:
76+
file_content = file.readlines()
77+
indented_file_content = "".join(" " + i for i in file_content)
78+
bad_code = f"""
79+
**Problematic code:**
80+
81+
.. code-block:: python
82+
83+
{indented_file_content}"""
84+
85+
if (data_path / "details.rst").exists():
86+
with open(data_path / "details.rst", encoding="utf-8") as file:
87+
file_content_string = file.read()
88+
details = f"""
89+
**Additional details:**
90+
91+
{file_content_string}"""
92+
93+
if (data_path / "related.rst").exists():
94+
with open(data_path / "related.rst", encoding="utf-8") as file:
95+
file_content_string = file.read()
96+
related = f"""
97+
**Related links:**
98+
99+
{file_content_string}"""
100+
101+
return good_code, bad_code, details, related
102+
103+
50104
def _get_all_messages(
51105
linter: PyLinter,
52106
) -> Tuple[MessagesDict, OldMessagesDict]:
@@ -72,8 +126,20 @@ def _get_all_messages(
72126
"information": defaultdict(list),
73127
}
74128
for message in linter.msgs_store.messages:
129+
message_data_path = (
130+
PYLINT_MESSAGES_DATA_PATH / message.symbol[0] / message.symbol
131+
)
132+
good_code, bad_code, details, related = _get_message_data(message_data_path)
133+
75134
message_data = MessageData(
76-
message.checker_name, message.msgid, message.symbol, message
135+
message.checker_name,
136+
message.msgid,
137+
message.symbol,
138+
message,
139+
good_code,
140+
bad_code,
141+
details,
142+
related,
77143
)
78144
messages_dict[MSG_TYPES_DOC[message.msgid[0]]].append(message_data)
79145

@@ -108,6 +174,11 @@ def _write_message_page(messages_dict: MessagesDict) -> None:
108174
109175
*{message.definition.description}*
110176
177+
{message.good_code}
178+
{message.bad_code}
179+
{message.details}
180+
{message.related_links}
181+
111182
Created by ``{message.checker}`` checker
112183
"""
113184
)

0 commit comments

Comments
 (0)