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

Re-export black.Mode #3875

Merged
merged 4 commits into from Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 3 additions & 8 deletions src/black/__init__.py
Expand Up @@ -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,
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,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_black.py
Expand Up @@ -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) -> None:
# 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) -> None:
# 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()
Expand Down