Skip to content
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

Set is_pyi if stdin_filename ends with .pyi #2169

Merged
merged 2 commits into from May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
@@ -1,5 +1,11 @@
## Change Log

### 21.4b3
bryanforbes marked this conversation as resolved.
Show resolved Hide resolved

#### _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