Unverified Commit 35e8d156 authored by Bryan Forbes's avatar Bryan Forbes Committed by GitHub
Browse files

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

parent 24bd6b98
Loading
Loading
Loading
Loading
+6 −0
Changes for CHANGES.md: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Change Log

### Unreleased

#### _Black_

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

### 21.4b2

#### _Black_
+2 −0
Changes for src/black/__init__.py: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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:
+29 −3
Changes for tests/test_black.py: 29 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -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: