Skip to content

Commit f791745

Browse files
Re-export black.Mode (#3875)
1 parent 0b62b9c commit f791745

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/black/__init__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,9 @@
6363
)
6464
from black.linegen import LN, LineGenerator, transform_line
6565
from black.lines import EmptyLineTracker, LinesBlock
66-
from black.mode import (
67-
FUTURE_FLAG_TO_FEATURE,
68-
VERSION_TO_FEATURES,
69-
Feature,
70-
Mode,
71-
TargetVersion,
72-
supports_feature,
73-
)
66+
from black.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
67+
from black.mode import Mode as Mode # re-exported
68+
from black.mode import TargetVersion, supports_feature
7469
from black.nodes import (
7570
STARS,
7671
is_number_token,

tests/test_black.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,41 @@ def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None:
24822482
)
24832483

24842484

2485+
class TestDeFactoAPI:
2486+
"""Test that certain symbols that are commonly used externally keep working.
2487+
2488+
We don't (yet) formally expose an API (see issue #779), but we should endeavor to
2489+
keep certain functions that external users commonly rely on working.
2490+
2491+
"""
2492+
2493+
def test_format_str(self) -> None:
2494+
# format_str and Mode should keep working
2495+
assert (
2496+
black.format_str("print('hello')", mode=black.Mode()) == 'print("hello")\n'
2497+
)
2498+
2499+
# you can pass line length
2500+
assert (
2501+
black.format_str("print('hello')", mode=black.Mode(line_length=42))
2502+
== 'print("hello")\n'
2503+
)
2504+
2505+
# invalid input raises InvalidInput
2506+
with pytest.raises(black.InvalidInput):
2507+
black.format_str("syntax error", mode=black.Mode())
2508+
2509+
def test_format_file_contents(self) -> None:
2510+
# You probably should be using format_str() instead, but let's keep
2511+
# this one around since people do use it
2512+
assert (
2513+
black.format_file_contents("x=1", fast=True, mode=black.Mode()) == "x = 1\n"
2514+
)
2515+
2516+
with pytest.raises(black.NothingChanged):
2517+
black.format_file_contents("x = 1\n", fast=True, mode=black.Mode())
2518+
2519+
24852520
try:
24862521
with open(black.__file__, "r", encoding="utf-8") as _bf:
24872522
black_source_lines = _bf.readlines()

0 commit comments

Comments
 (0)