Skip to content

Commit 1f6e1bf

Browse files
committed
code review suggestions
1 parent 1e489ff commit 1f6e1bf

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

pydocstringformatter/run.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,15 @@ def format_file_tokens(
136136
new_tokeninfo = tokeninfo
137137

138138
if _utils.is_docstring(new_tokeninfo, tokens[index - 1]):
139-
new_tokeninfo, changers, stable = self.apply_formatters_twice(
140-
new_tokeninfo
141-
)
139+
new_tokeninfo, changers = self.apply_formatters(new_tokeninfo)
142140
is_changed = is_changed or bool(changers)
143141

144142
# Run formatters again to check if the result is stable
145-
new_tokeninfo, changers, stable = self.apply_formatters_twice(
143+
_, changers = self._apply_formatters_once(
146144
new_tokeninfo,
147145
)
148146

149-
if not stable:
147+
if changers:
150148
conflicting_formatters = {
151149
k: v
152150
for k, v in self.enabled_formatters.items()
@@ -162,9 +160,9 @@ def format_file_tokens(
162160

163161
return formatted_tokens, is_changed
164162

165-
def apply_formatters_twice(
163+
def apply_formatters(
166164
self, token: tokenize.TokenInfo
167-
) -> tuple[tokenize.TokenInfo, list[str], bool]:
165+
) -> tuple[tokenize.TokenInfo, list[str]]:
168166
"""Apply the formatters twice to a token.
169167
170168
Also tracks which formatters changed the token.
@@ -175,17 +173,13 @@ def apply_formatters_twice(
175173
[2] a list of formatters that changed the token.
176174
[3] a boolean indicating if the token was in the second pass.
177175
"""
178-
stable = False
179-
token, changers = self.apply_formatters(token)
176+
token, changers = self._apply_formatters_once(token)
180177
if changers:
181-
token, changers2 = self.apply_formatters(token)
182-
stable = not bool(changers2)
178+
token, changers2 = self._apply_formatters_once(token)
183179
changers = list(set(changers + changers2))
184-
else:
185-
stable = True
186-
return token, changers, stable
180+
return token, changers
187181

188-
def apply_formatters(
182+
def _apply_formatters_once(
189183
self, token: tokenize.TokenInfo
190184
) -> tuple[tokenize.TokenInfo, list[str]]:
191185
"""Applies formatters to a token and keeps track of what changes it.
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
from __future__ import annotations
22

3-
import tempfile
43
from collections.abc import Generator
4+
from pathlib import Path
55

66
import pytest
77
from _pytest.fixtures import SubRequest
88

9+
from pydocstringformatter import _formatting
910
from pydocstringformatter import _testutils as test_utils
1011
from pydocstringformatter._utils import UnstableResultError
1112
from pydocstringformatter.run import _Run
1213

1314

1415
@pytest.fixture(scope="function")
15-
def patched_run(request: SubRequest) -> Generator[Type[_Run], None, None]:
16+
def patched_run(request: SubRequest) -> Generator[type[_Run], None, None]:
1617
"""Patches the _Run class to use the MakeAFormatter and MakeBFormatter."""
1718
# This monkeypatches the formatters to make them conflict.
1819
# and the only formatters applied
19-
# pylint: disable=wrong-import-position, import-outside-toplevel
20-
from pydocstringformatter import _formatting # noqa: E402
2120

2221
old_formatters = _formatting.FORMATTERS
2322
_formatting.FORMATTERS = request.param
@@ -26,9 +25,8 @@ def cleanup() -> None:
2625
"""Returns the formatters to their original state."""
2726
_formatting.FORMATTERS = old_formatters
2827

29-
request.addfinalizer(cleanup)
30-
3128
yield _Run
29+
cleanup()
3230

3331

3432
@pytest.mark.parametrize(
@@ -54,20 +52,20 @@ def cleanup() -> None:
5452
indirect=["patched_run"],
5553
)
5654
def test_conflicting_formatters(
57-
patched_run: Type[_Run], # pylint: disable=redefined-outer-name
58-
expect_errors: List[str],
55+
patched_run: type[_Run], # pylint: disable=redefined-outer-name
56+
expect_errors: list[str],
57+
tmp_path: Path,
5958
) -> None:
6059
"""Tests that conflicting formatters raise an error."""
61-
with tempfile.TemporaryDirectory() as tmp:
62-
tmp_file = tmp + "/test.py"
63-
with open(tmp_file, "w", encoding="utf-8") as f:
64-
content = [
65-
'"""AAA AA AAA"""',
66-
]
67-
f.writelines(content)
60+
tmp_file = tmp_path / "test.py"
61+
with open(tmp_file, "w", encoding="utf-8") as f:
62+
content = [
63+
'"""AAA AA AAA"""',
64+
]
65+
f.writelines(content)
6866

69-
with pytest.raises(UnstableResultError) as err:
70-
patched_run([tmp_file])
67+
with pytest.raises(UnstableResultError) as err:
68+
patched_run([str(tmp_file)])
7169

72-
for expect_err in expect_errors:
73-
assert expect_err in str(err.value), str(err.value)
70+
for expect_err in expect_errors:
71+
assert expect_err in str(err.value), str(err.value)

0 commit comments

Comments
 (0)