Set is_pyi if stdin_filename ends with .pyi (#2169)

Fixes #2167

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Bryan Forbes 2021-05-02 07:48:54 -05:00 committed by GitHub
parent 24bd6b983a
commit 35e8d1560d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -1,5 +1,11 @@
## Change Log
### Unreleased
#### _Black_
- Set `--pyi` mode if `--stdin-filename` ends in `.pyi` (#2169)
### 21.4b2
#### _Black_

View File

@ -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:

View File

@ -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:
@ -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: