Skip to content

Commit

Permalink
Disable string splitting/merging by default (#1609)
Browse files Browse the repository at this point in the history
* put experimental string stuff behind a flag
* update tests
* don't need an output section if it's the same as the input
* Primer: Expect no formatting changes in attrs, hypothesis and poetry with --experimental-string-processing off

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
  • Loading branch information
JelleZijlstra and hugovk authored Aug 20, 2020
1 parent e1027e2 commit e5bb92f
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 122 deletions.
86 changes: 49 additions & 37 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class Mode:
target_versions: Set[TargetVersion] = field(default_factory=set)
line_length: int = DEFAULT_LINE_LENGTH
string_normalization: bool = True
experimental_string_processing: bool = False
is_pyi: bool = False

def get_cache_key(self) -> str:
Expand Down Expand Up @@ -376,6 +377,15 @@ def target_version_option_callback(
is_flag=True,
help="Don't normalize string quotes or prefixes.",
)
@click.option(
"--experimental-string-processing",
is_flag=True,
hidden=True,
help=(
"Experimental option that performs more normalization on string literals."
" Currently disabled because it leads to some crashes."
),
)
@click.option(
"--check",
is_flag=True,
Expand Down Expand Up @@ -485,6 +495,7 @@ def main(
fast: bool,
pyi: bool,
skip_string_normalization: bool,
experimental_string_processing: bool,
quiet: bool,
verbose: bool,
include: str,
Expand All @@ -505,6 +516,7 @@ def main(
line_length=line_length,
is_pyi=pyi,
string_normalization=not skip_string_normalization,
experimental_string_processing=experimental_string_processing,
)
if config and verbose:
out(f"Using configuration from {config}.", bold=False, fg="blue")
Expand Down Expand Up @@ -984,10 +996,7 @@ def f(
before, after = elt.maybe_empty_lines(current_line)
dst_contents.append(str(empty_line) * before)
for line in transform_line(
current_line,
line_length=mode.line_length,
normalize_strings=mode.string_normalization,
features=split_line_features,
current_line, mode=mode, features=split_line_features
):
dst_contents.append(str(line))
return "".join(dst_contents)
Expand Down Expand Up @@ -2649,10 +2658,7 @@ def make_comment(content: str) -> str:


def transform_line(
line: Line,
line_length: int,
normalize_strings: bool,
features: Collection[Feature] = (),
line: Line, mode: Mode, features: Collection[Feature] = ()
) -> Iterator[Line]:
"""Transform a `line`, potentially splitting it into many lines.
Expand All @@ -2668,7 +2674,7 @@ def transform_line(

def init_st(ST: Type[StringTransformer]) -> StringTransformer:
"""Initialize StringTransformer"""
return ST(line_length, normalize_strings)
return ST(mode.line_length, mode.string_normalization)

string_merge = init_st(StringMerger)
string_paren_strip = init_st(StringParenStripper)
Expand All @@ -2681,21 +2687,26 @@ def init_st(ST: Type[StringTransformer]) -> StringTransformer:
and not line.should_explode
and not line.is_collection_with_optional_trailing_comma
and (
is_line_short_enough(line, line_length=line_length, line_str=line_str)
is_line_short_enough(line, line_length=mode.line_length, line_str=line_str)
or line.contains_unsplittable_type_ignore()
)
and not (line.contains_standalone_comments() and line.inside_brackets)
):
# Only apply basic string preprocessing, since lines shouldn't be split here.
transformers = [string_merge, string_paren_strip]
if mode.experimental_string_processing:
transformers = [string_merge, string_paren_strip]
else:
transformers = []
elif line.is_def:
transformers = [left_hand_split]
else:

def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
for omit in generate_trailers_to_omit(line, line_length):
lines = list(right_hand_split(line, line_length, features, omit=omit))
if is_line_short_enough(lines[0], line_length=line_length):
for omit in generate_trailers_to_omit(line, mode.line_length):
lines = list(
right_hand_split(line, mode.line_length, features, omit=omit)
)
if is_line_short_enough(lines[0], line_length=mode.line_length):
yield from lines
return

Expand All @@ -2706,24 +2717,30 @@ def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
# See #762 and #781 for the full story.
yield from right_hand_split(line, line_length=1, features=features)

if line.inside_brackets:
transformers = [
string_merge,
string_paren_strip,
delimiter_split,
standalone_comment_split,
string_split,
string_paren_wrap,
rhs,
]
if mode.experimental_string_processing:
if line.inside_brackets:
transformers = [
string_merge,
string_paren_strip,
delimiter_split,
standalone_comment_split,
string_split,
string_paren_wrap,
rhs,
]
else:
transformers = [
string_merge,
string_paren_strip,
string_split,
string_paren_wrap,
rhs,
]
else:
transformers = [
string_merge,
string_paren_strip,
string_split,
string_paren_wrap,
rhs,
]
if line.inside_brackets:
transformers = [delimiter_split, standalone_comment_split, rhs]
else:
transformers = [rhs]

for transform in transformers:
# We are accumulating lines in `result` because we might want to abort
Expand All @@ -2738,12 +2755,7 @@ def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
)

result.extend(
transform_line(
transformed_line,
line_length=line_length,
normalize_strings=normalize_strings,
features=features,
)
transform_line(transformed_line, mode=mode, features=features)
)
except CannotTransform:
continue
Expand Down
6 changes: 3 additions & 3 deletions src/black_primer/primer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"attrs": {
"cli_arguments": [],
"expect_formatting_changes": true,
"expect_formatting_changes": false,
"git_clone_url": "https://github.com/python-attrs/attrs.git",
"long_checkout": false,
"py_versions": ["all"]
Expand Down Expand Up @@ -47,7 +47,7 @@
},
"hypothesis": {
"cli_arguments": [],
"expect_formatting_changes": true,
"expect_formatting_changes": false,
"git_clone_url": "https://github.com/HypothesisWorks/hypothesis.git",
"long_checkout": false,
"py_versions": ["all"]
Expand All @@ -63,7 +63,7 @@
},
"poetry": {
"cli_arguments": [],
"expect_formatting_changes": true,
"expect_formatting_changes": false,
"git_clone_url": "https://github.com/python-poetry/poetry.git",
"long_checkout": false,
"py_versions": ["all"]
Expand Down
Loading

0 comments on commit e5bb92f

Please sign in to comment.