From 47ff3ac7ec41c9abb46d34ca1426eaa65c693bb7 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 10 Sep 2023 15:21:01 -0700 Subject: [PATCH 1/4] Re-export black.Mode --- src/black/__init__.py | 2 +- tests/test_black.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 6fc91d2e6d3..9fc44fb19be 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -67,7 +67,7 @@ FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature, - Mode, + Mode as Mode, # re-exported TargetVersion, supports_feature, ) diff --git a/tests/test_black.py b/tests/test_black.py index badb8fff5fb..341197e6c08 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2482,6 +2482,41 @@ def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None: ) +class TestDeFactoAPI: + """Test that certain symbols that are commonly used externally keep working. + + We don't (yet) formally expose an API (see issue #779), but we should endeavor to + keep certain functions that external users commonly rely on working. + + """ + + def test_format_str(self): + # format_str and Mode should keep working + assert ( + black.format_str("print('hello')", mode=black.Mode()) == 'print("hello")\n' + ) + + # you can pass line length + assert ( + black.format_str("print('hello')", mode=black.Mode(line_length=42)) + == 'print("hello")\n' + ) + + # invalid input raises InvalidInput + with pytest.raises(black.InvalidInput): + black.format_str("syntax error", mode=black.Mode()) + + def test_format_file_contents(self): + # You probably should be using format_str() instead, but let's keep this one around + # since people do use it + assert ( + black.format_file_contents("x=1", fast=True, mode=black.Mode()) == "x = 1\n" + ) + + with pytest.raises(black.NothingChanged): + black.format_file_contents("x = 1\n", fast=True, mode=black.Mode()) + + try: with open(black.__file__, "r", encoding="utf-8") as _bf: black_source_lines = _bf.readlines() From fba85b5439821cbb8251bf83969a55677172d6ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 10 Sep 2023 22:21:56 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/__init__.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 9fc44fb19be..188a4f79f0e 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -63,14 +63,9 @@ ) from black.linegen import LN, LineGenerator, transform_line from black.lines import EmptyLineTracker, LinesBlock -from black.mode import ( - FUTURE_FLAG_TO_FEATURE, - VERSION_TO_FEATURES, - Feature, - Mode as Mode, # re-exported - TargetVersion, - supports_feature, -) +from black.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature +from black.mode import Mode as Mode # re-exported +from black.mode import TargetVersion, supports_feature from black.nodes import ( STARS, is_number_token, From 294230966b8a898ec2375cabed447d4f0fd99ceb Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 10 Sep 2023 15:35:34 -0700 Subject: [PATCH 3/4] return types --- tests/test_black.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_black.py b/tests/test_black.py index 341197e6c08..87ebb7f240f 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2490,7 +2490,7 @@ class TestDeFactoAPI: """ - def test_format_str(self): + def test_format_str(self) -> None: # format_str and Mode should keep working assert ( black.format_str("print('hello')", mode=black.Mode()) == 'print("hello")\n' @@ -2506,7 +2506,7 @@ def test_format_str(self): with pytest.raises(black.InvalidInput): black.format_str("syntax error", mode=black.Mode()) - def test_format_file_contents(self): + def test_format_file_contents(self) -> None: # You probably should be using format_str() instead, but let's keep this one around # since people do use it assert ( From 9eac0de2f7906909449363fe4ec98b76878bb9cb Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 10 Sep 2023 15:44:00 -0700 Subject: [PATCH 4/4] line length in comment --- tests/test_black.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_black.py b/tests/test_black.py index 87ebb7f240f..d22b6859607 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2507,8 +2507,8 @@ def test_format_str(self) -> None: black.format_str("syntax error", mode=black.Mode()) def test_format_file_contents(self) -> None: - # You probably should be using format_str() instead, but let's keep this one around - # since people do use it + # You probably should be using format_str() instead, but let's keep + # this one around since people do use it assert ( black.format_file_contents("x=1", fast=True, mode=black.Mode()) == "x = 1\n" )