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 5ac7cb4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
@@ -1,5 +1,11 @@
## Change Log

### 21.4b3

#### _Black_

- Set `--pyi` mode if `--stdin-filename` ends in `.pyi` (#2169)

### 21.4b2

#### _Black_
Expand Down
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
32 changes: 29 additions & 3 deletions tests/test_black.py
Expand Up @@ -1578,8 +1578,34 @@ def test_reformat_one_with_stdin_filename(self) -> None:
mode=DEFAULT_MODE,
report=report,
)
fsts.assert_called_once()
# __BLACK_STDIN_FILENAME__ should have been striped
fsts.assert_called_once_with(
fast=True, write_back=black.WriteBack.YES, mode=DEFAULT_MODE
)
# __BLACK_STDIN_FILENAME__ should have been stripped
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 stripped
report.done.assert_called_with(expected, black.Changed.YES)

def test_reformat_one_with_stdin_and_existing_path(self) -> None:
Expand All @@ -1603,7 +1629,7 @@ def test_reformat_one_with_stdin_and_existing_path(self) -> None:
report=report,
)
fsts.assert_called_once()
# __BLACK_STDIN_FILENAME__ should have been striped
# __BLACK_STDIN_FILENAME__ should have been stripped
report.done.assert_called_with(expected, black.Changed.YES)

def test_gitignore_exclude(self) -> None:
Expand Down

0 comments on commit 5ac7cb4

Please sign in to comment.