summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-03-23 19:35:33 +0100
committerGitHub <noreply@github.com>2022-03-23 19:35:33 +0100
commit536087e448f9204ddbaea10a55ca5eab7d64de0d (patch)
treef33267e8afb985a74f8c0ba1a8a1d7a75361271c
parent9c90db16a860d5e33b4916feb232a0a6fe9f420d (diff)
downloadpylint-git-536087e448f9204ddbaea10a55ca5eab7d64de0d.tar.gz
Set up system of code examples and details for message documentation (#5934)
-rw-r--r--.pre-commit-config.yaml2
-rw-r--r--doc/conf.py2
-rw-r--r--doc/data/messages/e/empty-docstring/bad.py2
-rw-r--r--doc/data/messages/e/empty-docstring/good.py2
-rw-r--r--doc/data/messages/y/yield-inside-async-function/bad.py2
-rw-r--r--doc/data/messages/y/yield-inside-async-function/details.rst1
-rw-r--r--doc/data/messages/y/yield-inside-async-function/good.py7
-rw-r--r--doc/data/messages/y/yield-inside-async-function/related.rst1
-rw-r--r--doc/exts/pylint_messages.py73
9 files changed, 89 insertions, 3 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index d6b2226db..f19027503 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -13,7 +13,7 @@ repos:
rev: v1.4
hooks:
- id: autoflake
- exclude: &fixtures tests/functional/|tests/input|tests/regrtest_data/|tests/data/
+ exclude: &fixtures tests/functional/|tests/input|tests/regrtest_data/|tests/data/|doc/data/messages
args:
- --in-place
- --remove-all-unused-imports
diff --git a/doc/conf.py b/doc/conf.py
index 1c44c89c6..0f15ffd91 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -73,7 +73,7 @@ release = __version__
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
-exclude_patterns = ["_build"]
+exclude_patterns = ["_build", "data/**"]
# The reST default role (used for this markup: `text`) to use for all documents.
# default_role = None
diff --git a/doc/data/messages/e/empty-docstring/bad.py b/doc/data/messages/e/empty-docstring/bad.py
new file mode 100644
index 000000000..83d360125
--- /dev/null
+++ b/doc/data/messages/e/empty-docstring/bad.py
@@ -0,0 +1,2 @@
+def foo():
+ pass # [emtpy-docstring]
diff --git a/doc/data/messages/e/empty-docstring/good.py b/doc/data/messages/e/empty-docstring/good.py
new file mode 100644
index 000000000..b587ca4c6
--- /dev/null
+++ b/doc/data/messages/e/empty-docstring/good.py
@@ -0,0 +1,2 @@
+def foo():
+ """A dummy description."""
diff --git a/doc/data/messages/y/yield-inside-async-function/bad.py b/doc/data/messages/y/yield-inside-async-function/bad.py
new file mode 100644
index 000000000..6e1d6bd28
--- /dev/null
+++ b/doc/data/messages/y/yield-inside-async-function/bad.py
@@ -0,0 +1,2 @@
+async def foo():
+ yield from [1, 2, 3] # [yield-inside-async-function]
diff --git a/doc/data/messages/y/yield-inside-async-function/details.rst b/doc/data/messages/y/yield-inside-async-function/details.rst
new file mode 100644
index 000000000..7d05701ae
--- /dev/null
+++ b/doc/data/messages/y/yield-inside-async-function/details.rst
@@ -0,0 +1 @@
+The message can't be emitted when using Python < 3.5.
diff --git a/doc/data/messages/y/yield-inside-async-function/good.py b/doc/data/messages/y/yield-inside-async-function/good.py
new file mode 100644
index 000000000..1af96506b
--- /dev/null
+++ b/doc/data/messages/y/yield-inside-async-function/good.py
@@ -0,0 +1,7 @@
+async def foo():
+ def _inner_foo():
+ yield from [1, 2, 3]
+
+
+async def foo():
+ yield 42
diff --git a/doc/data/messages/y/yield-inside-async-function/related.rst b/doc/data/messages/y/yield-inside-async-function/related.rst
new file mode 100644
index 000000000..98cb4e3a9
--- /dev/null
+++ b/doc/data/messages/y/yield-inside-async-function/related.rst
@@ -0,0 +1 @@
+- `PEP 525 <https://peps.python.org/pep-0525/>`_
diff --git a/doc/exts/pylint_messages.py b/doc/exts/pylint_messages.py
index 7f910918f..0f9ac35de 100644
--- a/doc/exts/pylint_messages.py
+++ b/doc/exts/pylint_messages.py
@@ -23,6 +23,8 @@ PYLINT_BASE_PATH = Path(__file__).resolve().parent.parent.parent
PYLINT_MESSAGES_PATH = PYLINT_BASE_PATH / "doc" / "messages"
"""Path to the messages documentation folder."""
+PYLINT_MESSAGES_DATA_PATH = PYLINT_BASE_PATH / "doc" / "data" / "messages"
+"""Path to the folder with data for the messages documentation."""
MSG_TYPES_DOC = {k: v if v != "info" else "information" for k, v in MSG_TYPES.items()}
@@ -32,6 +34,10 @@ class MessageData(NamedTuple):
id: str
name: str
definition: MessageDefinition
+ good_code: str
+ bad_code: str
+ details: str
+ related_links: str
MessagesDict = Dict[str, List[MessageData]]
@@ -47,6 +53,54 @@ def _register_all_checkers_and_extensions(linter: PyLinter) -> None:
initialize_extensions(linter)
+def _get_message_data(data_path: Path) -> Tuple[str, str, str, str]:
+ """Get the message data from the specified path."""
+ good_code, bad_code, details, related = "", "", "", ""
+
+ if not data_path.exists():
+ return good_code, bad_code, details, related
+
+ if (data_path / "good.py").exists():
+ with open(data_path / "good.py", encoding="utf-8") as file:
+ file_content = file.readlines()
+ indented_file_content = "".join(" " + i for i in file_content)
+ good_code = f"""
+**Correct code:**
+
+.. code-block:: python
+
+{indented_file_content}"""
+
+ if (data_path / "bad.py").exists():
+ with open(data_path / "bad.py", encoding="utf-8") as file:
+ file_content = file.readlines()
+ indented_file_content = "".join(" " + i for i in file_content)
+ bad_code = f"""
+**Problematic code:**
+
+.. code-block:: python
+
+{indented_file_content}"""
+
+ if (data_path / "details.rst").exists():
+ with open(data_path / "details.rst", encoding="utf-8") as file:
+ file_content_string = file.read()
+ details = f"""
+**Additional details:**
+
+{file_content_string}"""
+
+ if (data_path / "related.rst").exists():
+ with open(data_path / "related.rst", encoding="utf-8") as file:
+ file_content_string = file.read()
+ related = f"""
+**Related links:**
+
+{file_content_string}"""
+
+ return good_code, bad_code, details, related
+
+
def _get_all_messages(
linter: PyLinter,
) -> Tuple[MessagesDict, OldMessagesDict]:
@@ -72,8 +126,20 @@ def _get_all_messages(
"information": defaultdict(list),
}
for message in linter.msgs_store.messages:
+ message_data_path = (
+ PYLINT_MESSAGES_DATA_PATH / message.symbol[0] / message.symbol
+ )
+ good_code, bad_code, details, related = _get_message_data(message_data_path)
+
message_data = MessageData(
- message.checker_name, message.msgid, message.symbol, message
+ message.checker_name,
+ message.msgid,
+ message.symbol,
+ message,
+ good_code,
+ bad_code,
+ details,
+ related,
)
messages_dict[MSG_TYPES_DOC[message.msgid[0]]].append(message_data)
@@ -108,6 +174,11 @@ def _write_message_page(messages_dict: MessagesDict) -> None:
*{message.definition.description}*
+{message.good_code}
+{message.bad_code}
+{message.details}
+{message.related_links}
+
Created by ``{message.checker}`` checker
"""
)