Skip to content

Commit ad9f251

Browse files
DanielNoordjspaezp
andcommitted
Add create_gh_issue_template utility
Co-authored-by: J. Sebastian Paez <[email protected]>
1 parent 282cf7f commit ad9f251

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

pydocstringformatter/_utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pydocstringformatter._utils.file_diference import compare_formatters, generate_diff
88
from pydocstringformatter._utils.find_docstrings import is_docstring
99
from pydocstringformatter._utils.find_python_file import find_python_files
10+
from pydocstringformatter._utils.issue_template import create_gh_issue_template
1011
from pydocstringformatter._utils.output import print_to_console, sys_exit
1112

1213
__all__ = [
@@ -18,6 +19,7 @@
1819
"PydocstringFormatterError",
1920
"TomlParsingError",
2021
"UnstableResultError",
22+
"create_gh_issue_template",
2123
"print_to_console",
2224
"sys_exit",
2325
]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from __future__ import annotations
2+
3+
import tokenize
4+
5+
from pydocstringformatter._formatting import Formatter
6+
from pydocstringformatter._utils.file_diference import compare_formatters
7+
8+
9+
def create_gh_issue_template(
10+
token: tokenize.TokenInfo, formatters: dict[str, Formatter], filename: str
11+
) -> str:
12+
"""Make a template for a GitHub issue.
13+
14+
Args:
15+
token: The token that caused the issue.
16+
formatters: The formatters that caused the issue.
17+
filename: The filename of the file that caused the issue.
18+
"""
19+
formatter_names = list(formatters)
20+
msg = ""
21+
if len(formatter_names) > 2:
22+
msg = f"""
23+
Conflicting formatters: {", ".join(formatters)}
24+
"""
25+
diff = f"Diff too intricate to compute for {len(formatter_names)} formatters."
26+
else:
27+
if len(formatter_names) == 2:
28+
msg = f"""
29+
Conflicting formatters: {" and ".join(formatter_names)}
30+
These formatters conflict with each other for:
31+
32+
```python
33+
{token.string}
34+
```
35+
"""
36+
formatter_1 = formatters[formatter_names[0]]
37+
formatter_2 = formatters[formatter_names[1]]
38+
else:
39+
msg = f"""
40+
Formatter: {formatter_names[0]}
41+
This formatter is not able to make stable changes for:
42+
43+
```python
44+
{token.string}
45+
```
46+
"""
47+
formatter_1 = formatters[formatter_names[0]]
48+
formatter_2 = formatter_1
49+
50+
diff = compare_formatters(
51+
token,
52+
formatter_1,
53+
formatter_2,
54+
title_extra=str(filename),
55+
)
56+
57+
out = f"""
58+
Unfortunately an error occurred while formatting a docstring.
59+
Please help us fix this bug by opening an issue at:
60+
https://github.com/DanielNoord/pydocstringformatter/issues/new
61+
62+
{"-" * 80}
63+
64+
You can use the following template when you open the issue:
65+
66+
# Description:
67+
68+
{msg}
69+
70+
# Diff:
71+
72+
```diff
73+
{diff}
74+
```
75+
76+
"""
77+
78+
return out

0 commit comments

Comments
 (0)