2323PYLINT_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
2729MSG_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
3743MessagesDict = 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+
50104def _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+
111182Created by ``{ message .checker } `` checker
112183"""
113184 )
0 commit comments