-
-
Notifications
You must be signed in to change notification settings - Fork 8
Feature: two-pass formatting of the tokens #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import annotations | ||
|
||
import tokenize | ||
|
||
from pydocstringformatter._formatting import Formatter | ||
from pydocstringformatter._utils.file_diference import compare_formatters | ||
|
||
|
||
def create_gh_issue_template( | ||
token: tokenize.TokenInfo, formatters: dict[str, Formatter], filename: str | ||
) -> str: | ||
"""Make a template for a GitHub issue. | ||
|
||
Args: | ||
token: The token that caused the issue. | ||
formatters: The formatters that caused the issue. | ||
filename: The filename of the file that caused the issue. | ||
""" | ||
formatter_names = list(formatters) | ||
msg = "" | ||
if len(formatter_names) > 2: | ||
msg = f""" | ||
Conflicting formatters: {", ".join(formatters)} | ||
""" | ||
diff = f"Diff too intricate to compute for {len(formatter_names)} formatters." | ||
else: | ||
if len(formatter_names) == 2: | ||
msg = f""" | ||
Conflicting formatters: {" and ".join(formatter_names)} | ||
These formatters conflict with each other for: | ||
|
||
```python | ||
{token.string} | ||
``` | ||
""" | ||
formatter_1 = formatters[formatter_names[0]] | ||
formatter_2 = formatters[formatter_names[1]] | ||
else: | ||
msg = f""" | ||
Formatter: {formatter_names[0]} | ||
This formatter is not able to make stable changes for: | ||
|
||
```python | ||
{token.string} | ||
``` | ||
""" | ||
formatter_1 = formatters[formatter_names[0]] | ||
formatter_2 = formatter_1 | ||
|
||
diff = compare_formatters( | ||
token, | ||
formatter_1, | ||
formatter_2, | ||
title_extra=str(filename), | ||
) | ||
|
||
out = f""" | ||
Unfortunately an error occurred while formatting a docstring. | ||
Please help us fix this bug by opening an issue at: | ||
https://github.com/DanielNoord/pydocstringformatter/issues/new | ||
|
||
{"-" * 80} | ||
|
||
You can use the following template when you open the issue: | ||
|
||
# Description: | ||
|
||
{msg} | ||
|
||
# Diff: | ||
|
||
```diff | ||
{diff} | ||
``` | ||
|
||
""" | ||
|
||
return out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
def func(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test update can only be done with the |
||
"""A very long summary line that needs to be wrapped, A very long summary line that | ||
"""A very long summary line that needs to be wrapped, A very long summary line that. | ||
|
||
needs to be wrapped. | ||
|
||
A description that is not too long. | ||
""" | ||
|
||
|
||
def func(): | ||
"""A very long multi-line summary line that needs to be wrapped, A very long multi- | ||
"""A very long multi-line summary line that needs to be wrapped, A very long multi-. | ||
|
||
line summary line that needs to be wrapped. | ||
|
||
A very long summary line that needs to be wrapped. | ||
|
@@ -28,13 +30,15 @@ def func(): | |
# Since the ending quotes will be appended on the same line this | ||
# exceeds the max length. | ||
def func(): | ||
"""A multi-line summary that can be on one line, Something that is just too | ||
"""A multi-line summary that can be on one line, Something that is just too. | ||
|
||
longgg. | ||
""" | ||
|
||
|
||
def func(): | ||
"""A multi-line summary that can be on one line, Something that is just too | ||
"""A multi-line summary that can be on one line, Something that is just too. | ||
|
||
long. | ||
""" | ||
|
||
|
@@ -46,6 +50,7 @@ def func(): | |
# Regression for bug found in pylint | ||
# We should re-add the quotes to line length if they will never be on the first line. | ||
class LinesChunk: | ||
"""The LinesChunk object computes and stores the hash of some consecutive stripped | ||
"""The LinesChunk object computes and stores the hash of some consecutive stripped. | ||
|
||
lines of a lineset. | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pydocstringformatter import _formatting | ||
from pydocstringformatter import _testutils as test_utils | ||
from pydocstringformatter._formatting import Formatter | ||
from pydocstringformatter._utils import UnstableResultError | ||
from pydocstringformatter.run import _Run | ||
|
||
|
||
@contextmanager | ||
def patched_run(formatters: list[Formatter]) -> Generator[type[_Run], None, None]: | ||
"""Patches formatters so Run only uses the provided formatters.""" | ||
old_formatters = _formatting.FORMATTERS | ||
_formatting.FORMATTERS = formatters | ||
yield _Run | ||
jspaezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_formatting.FORMATTERS = old_formatters | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"formatters,expected_errors", | ||
[ | ||
( | ||
[test_utils.MakeAFormatter(), test_utils.MakeBFormatter()], | ||
["Conflicting formatters"], | ||
), | ||
( | ||
[test_utils.MakeBFormatter(), test_utils.AddBFormatter()], | ||
["not able to make stable changes"], | ||
), | ||
( | ||
[ | ||
test_utils.MakeBFormatter(), | ||
test_utils.AddBFormatter(), | ||
test_utils.MakeAFormatter(), | ||
], | ||
["Conflicting formatters:", "Diff too intricate to compute"], | ||
), | ||
], | ||
) | ||
def test_conflicting_formatters( | ||
formatters: list[Formatter], | ||
expected_errors: list[str], | ||
tmp_path: Path, | ||
) -> None: | ||
"""Tests that conflicting formatters raise an error.""" | ||
jspaezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tmp_file = tmp_path / "test.py" | ||
with open(tmp_file, "w", encoding="utf-8") as f: | ||
content = [ | ||
'"""AAA AA AAA"""', | ||
] | ||
f.writelines(content) | ||
|
||
jspaezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with patched_run(formatters) as run: | ||
with pytest.raises(UnstableResultError) as err: | ||
run([str(tmp_file)]) | ||
|
||
for expect_err in expected_errors: | ||
assert expect_err in str(err.value), str(err.value) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.