Skip to content

Commit

Permalink
Normalize string quotes
Browse files Browse the repository at this point in the history
Convert simple double-quoted strings to single-quoted. Convert triple (single) quoted strings to triple (double) quoted. Do not touch any strings that have backslashes or quotes inside the string.

Fixes psf#51.
  • Loading branch information
zsol committed Mar 31, 2018
1 parent 4dfec56 commit f1d630e
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 173 deletions.
211 changes: 123 additions & 88 deletions black.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup
import sys

assert sys.version_info >= (3, 6, 0), "black requires Python 3.6+"
assert sys.version_info >= (3, 6, 0), 'black requires Python 3.6+'
from pathlib import Path # noqa E402

CURRENT_DIR = Path(__file__).parent
Expand All @@ -27,9 +27,9 @@ def get_version():
setup(
name='black',
version=get_version(),
description="The uncompromising code formatter.",
description='The uncompromising code formatter.',
long_description=get_long_description(),
long_description_content_type="text/markdown",
long_description_content_type='text/markdown',
keywords='automation formatter yapf autopep8 pyfmt gofmt rustfmt',
author='Łukasz Langa',
author_email='lukasz@langa.pl',
Expand All @@ -38,7 +38,7 @@ def get_version():
py_modules=['black'],
packages=['blib2to3', 'blib2to3.pgen2'],
package_data={'blib2to3': ['*.txt']},
python_requires=">=3.6",
python_requires='>=3.6',
zip_safe=False,
install_requires=['click', 'attrs>=17.4.0'],
test_suite='tests.test_black',
Expand Down
2 changes: 1 addition & 1 deletion tests/cantfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
# long arguments
normal_name = normal_function_name(
"but with super long string arguments that on their own exceed the line limit so there's no way it can ever fit",
"eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs",
'eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs with spam and eggs and spam with eggs',
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it=0,
)
2 changes: 1 addition & 1 deletion tests/composition.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class C:

def test(self) -> None:
with patch("black.out", print):
with patch('black.out', print):
self.assertEqual(
unstyle(str(report)), '1 file reformatted, 1 file failed to reformat.'
)
Expand Down
6 changes: 3 additions & 3 deletions tests/empty_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def g():
return DOUBLESPACE

# Another comment because more comments
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
assert p is not None, f'INTERNAL ERROR: hand-made leaf without parent: {leaf!r}'

prev = leaf.prev_sibling
if not prev:
Expand Down Expand Up @@ -103,7 +103,7 @@ def f():
if t == token.COMMENT: # another trailing comment
return DOUBLESPACE

assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
assert p is not None, f'INTERNAL ERROR: hand-made leaf without parent: {leaf!r}'

prev = leaf.prev_sibling
if not prev:
Expand Down Expand Up @@ -155,7 +155,7 @@ def g():
return DOUBLESPACE

# Another comment because more comments
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
assert p is not None, f'INTERNAL ERROR: hand-made leaf without parent: {leaf!r}'

prev = leaf.prev_sibling
if not prev:
Expand Down
16 changes: 8 additions & 8 deletions tests/fmtonoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def func_no_args():
for i in range(10):
print(i)
continue
exec("new-style exec", {}, {})
exec('new-style exec', {}, {})
return None
async def coroutine(arg, exec=False):
"Single-line docstring. Multiline is harder to reformat."
'Single-line docstring. Multiline is harder to reformat.'
async with some_connection() as conn:
await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
await asyncio.sleep(1)
Expand All @@ -27,7 +27,7 @@ async def coroutine(arg, exec=False):
with_args=True,
many_args=[1,2,3]
)
def function_signature_stress_test(number:int,no_annotation=None,text:str="default",* ,debug:bool=False,**kwargs) -> str:
def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
return text[number:-1]
# fmt: on
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
Expand Down Expand Up @@ -92,10 +92,10 @@ def func_no_args():
for i in range(10):
print(i)
continue
exec("new-style exec", {}, {})
exec('new-style exec', {}, {})
return None
async def coroutine(arg, exec=False):
"Single-line docstring. Multiline is harder to reformat."
'Single-line docstring. Multiline is harder to reformat.'
async with some_connection() as conn:
await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
await asyncio.sleep(1)
Expand All @@ -104,12 +104,12 @@ async def coroutine(arg, exec=False):
with_args=True,
many_args=[1,2,3]
)
def function_signature_stress_test(number:int,no_annotation=None,text:str="default",* ,debug:bool=False,**kwargs) -> str:
def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
return text[number:-1]
# fmt: on


def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h='', i=r''):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
assert task._cancel_stack[:len(old_stack)] == old_stack

Expand All @@ -122,7 +122,7 @@ def spaces_types(
e: bool = True,
f: int = -1,
g: int = 1 if False else 2,
h: str = "",
h: str = '',
i: str = r'',
):
...
Expand Down
10 changes: 5 additions & 5 deletions tests/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ def func_no_args():
print(i)
continue

exec("new-style exec", {}, {})
exec('new-style exec', {}, {})
return None


async def coroutine(arg, exec=False):
"Single-line docstring. Multiline is harder to reformat."
'Single-line docstring. Multiline is harder to reformat.'
async with some_connection() as conn:
await conn.do_what_i_mean('SELECT bobby, tables FROM xkcd', timeout=2)
await asyncio.sleep(1)
Expand All @@ -112,15 +112,15 @@ async def coroutine(arg, exec=False):
def function_signature_stress_test(
number: int,
no_annotation=None,
text: str = "default",
text: str = 'default',
*,
debug: bool = False,
**kwargs,
) -> str:
return text[number:-1]


def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''):
def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h='', i=r''):
offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
assert task._cancel_stack[:len(old_stack)] == old_stack

Expand All @@ -133,7 +133,7 @@ def spaces_types(
e: bool = True,
f: int = -1,
g: int = 1 if False else 2,
h: str = "",
h: str = '',
i: str = r'',
):
...
Expand Down
6 changes: 3 additions & 3 deletions tests/python2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def function((_globals, _locals)):

import sys

print >>sys.stderr, "Warning:",
print >>sys.stderr, "this is a blast from the past."
print >>sys.stderr, "Look, a repr:", ` sys `
print >>sys.stderr, 'Warning:',
print >>sys.stderr, 'this is a blast from the past.'
print >>sys.stderr, 'Look, a repr:', ` sys `


def function((_globals, _locals)):
Expand Down
37 changes: 37 additions & 0 deletions tests/string_quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"Hello"
"Don't do that"
'Here is a "'
'What\'s the deal here?'
"What's the deal \"here\"?"
"And \"here\"?"
"""Strings with "" in them"""
'''Strings with "" in them'''
'''Here's a "'''
'''Here's a " '''
'''Just a normal triple
quote'''
f"just a normal {f} string"
f'''This is a triple-quoted {f}-string'''
f'MOAR {" ".join([])}'
f"MOAR {' '.join([])}"
r"raw string ftw"

# output

"Hello"
"Don't do that"
'Here is a "'
"What's the deal here?"
'What\'s the deal "here"?'
'And "here"?'
"""Strings with "" in them"""
"""Strings with "" in them"""
'''Here's a "'''
"""Here's a " """
"""Just a normal triple
quote"""
f"just a normal {f} string"
f"""This is a triple-quoted {f}-string"""
f'MOAR {" ".join([])}'
f"MOAR {' '.join([])}"
r"raw string ftw"
Loading

0 comments on commit f1d630e

Please sign in to comment.