From: Bryan Forbes Date: Sun, 2 May 2021 12:48:54 +0000 (-0500) Subject: Set `is_pyi` if `stdin_filename` ends with `.pyi` (#2169) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/35e8d1560d68e8113ff926a9f832582f8f4a694f?ds=sidebyside Set `is_pyi` if `stdin_filename` ends with `.pyi` (#2169) Fixes #2167 Co-authored-by: Jelle Zijlstra --- diff --git a/CHANGES.md b/CHANGES.md index 6675c48..321f8c0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ ## Change Log +### Unreleased + +#### _Black_ + +- Set `--pyi` mode if `--stdin-filename` ends in `.pyi` (#2169) + ### 21.4b2 #### _Black_ diff --git a/src/black/__init__.py b/src/black/__init__.py index 1c69cc4..49d088b 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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: diff --git a/tests/test_black.py b/tests/test_black.py index c643f27..43368d4 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1578,8 +1578,34 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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: