Skip to content

Commit

Permalink
Add --skip-string-normalization
Browse files Browse the repository at this point in the history
Fixes #118
  • Loading branch information
ambv committed May 30, 2018
1 parent 608019d commit 8ebbd26
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Options:
files. This will put trailing commas in function
signatures and calls also after *args and
**kwargs. [default: per-file auto-detection]
-S, --skip-string-normalization
Don't normalize string quotes or prefixes.
--version Show the version and exit.
--help Show this message and exit.
```
Expand Down Expand Up @@ -346,6 +348,12 @@ a bit easier than double quotes. The latter requires use of the Shift
key. My recommendation here is to keep using whatever is faster to type
and let *Black* handle the transformation.

If you are adopting *Black* in a large project with pre-existing string
conventions (like the popular ["single quotes for data, double quotes for
human-readable strings"](https://stackoverflow.com/a/56190)), you can
pass `--skip-string-normalization` on the command line. This is meant as
an adoption helper, avoid using this for new projects.


### Line breaks & binary operators

Expand Down Expand Up @@ -688,6 +696,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).

## Change Log

### 18.6b0

* added `--skip-string-normalization` (#118)


### 18.5b1

* added `--pyi` (#249)
Expand Down
18 changes: 16 additions & 2 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class FileMode(Flag):
AUTO_DETECT = 0
PYTHON36 = 1
PYI = 2
NO_STRING_NORMALIZATION = 4


@click.command()
Expand Down Expand Up @@ -182,6 +183,12 @@ class FileMode(Flag):
"**kwargs. [default: per-file auto-detection]"
),
)
@click.option(
"-S",
"--skip-string-normalization",
is_flag=True,
help="Don't normalize string quotes or prefixes.",
)
@click.version_option(version=__version__)
@click.argument(
"src",
Expand All @@ -199,6 +206,7 @@ def main(
fast: bool,
pyi: bool,
py36: bool,
skip_string_normalization: bool,
quiet: bool,
src: List[str],
) -> None:
Expand Down Expand Up @@ -227,6 +235,8 @@ def main(
mode |= FileMode.PYTHON36
if pyi:
mode |= FileMode.PYI
if skip_string_normalization:
mode |= FileMode.NO_STRING_NORMALIZATION
report = Report(check=check, quiet=quiet)
if len(sources) == 0:
out("No paths given. Nothing to do 😴")
Expand Down Expand Up @@ -487,8 +497,11 @@ def format_str(
future_imports = get_future_imports(src_node)
is_pyi = bool(mode & FileMode.PYI)
py36 = bool(mode & FileMode.PYTHON36) or is_python36(src_node)
normalize_strings = not bool(mode & FileMode.NO_STRING_NORMALIZATION)
lines = LineGenerator(
remove_u_prefix=py36 or "unicode_literals" in future_imports, is_pyi=is_pyi
remove_u_prefix=py36 or "unicode_literals" in future_imports,
is_pyi=is_pyi,
normalize_strings=normalize_strings,
)
elt = EmptyLineTracker(is_pyi=is_pyi)
empty_line = Line()
Expand Down Expand Up @@ -1286,6 +1299,7 @@ class LineGenerator(Visitor[Line]):
"""

is_pyi: bool = False
normalize_strings: bool = True
current_line: Line = Factory(Line)
remove_u_prefix: bool = False

Expand Down Expand Up @@ -1354,7 +1368,7 @@ def visit_default(self, node: LN) -> Iterator[Line]:

else:
normalize_prefix(node, inside_brackets=any_open_brackets)
if node.type == token.STRING:
if self.normalize_strings and node.type == token.STRING:
normalize_string_prefix(node, remove_u_prefix=self.remove_u_prefix)
normalize_string_quotes(node)
if node.type not in WHITESPACE:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ def test_string_quotes(self) -> None:
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, line_length=ll)
mode = black.FileMode.NO_STRING_NORMALIZATION
not_normalized = fs(source, mode=mode)
self.assertFormatEqual(source, not_normalized)
black.assert_equivalent(source, not_normalized)
black.assert_stable(source, not_normalized, line_length=ll, mode=mode)

@patch("black.dump_to_file", dump_to_stderr)
def test_slices(self) -> None:
Expand Down

1 comment on commit 8ebbd26

@aaugustin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.