Skip to content

Commit

Permalink
Set is_pyi if stdin_filename ends with .pyi
Browse files Browse the repository at this point in the history
Fixes #2167
  • Loading branch information
bryanforbes committed Apr 29, 2021
1 parent b39999d commit 6734557
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/black/__init__.py
Expand Up @@ -755,6 +755,8 @@ def reformat_one(
is_stdin = False

if is_stdin:
if src.suffix == ".pyi":
mode = replace(mode, is_pyi=True)
if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
changed = Changed.YES
else:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_black.py
Expand Up @@ -1578,7 +1578,33 @@ def test_reformat_one_with_stdin_filename(self) -> None:
mode=DEFAULT_MODE,
report=report,
)
fsts.assert_called_once()
fsts.assert_called_once_with(
fast=True, write_back=black.WriteBack.YES, mode=DEFAULT_MODE
)
# __BLACK_STDIN_FILENAME__ should have been striped
report.done.assert_called_with(expected, black.Changed.YES)

def test_reformat_one_with_stdin_filename_pyi(self) -> None:
with patch(
"black.format_stdin_to_stdout",
return_value=lambda *args, **kwargs: black.Changed.YES,
) as fsts:
report = MagicMock()
p = "foo.pyi"
path = Path(f"__BLACK_STDIN_FILENAME__{p}")
expected = Path(p)
black.reformat_one(
path,
fast=True,
write_back=black.WriteBack.YES,
mode=DEFAULT_MODE,
report=report,
)
fsts.assert_called_once_with(
fast=True,
write_back=black.WriteBack.YES,
mode=replace(DEFAULT_MODE, is_pyi=True),
)
# __BLACK_STDIN_FILENAME__ should have been striped
report.done.assert_called_with(expected, black.Changed.YES)

Expand Down

0 comments on commit 6734557

Please sign in to comment.