Add tests from resolved issues
Resolves #233
Resolves #402
Resolves #412
Resolves #830
Resolves #856
Resolves #894
Resolves #1045
Resolves #1058
Resolves #1060
Resolves #1064
Resolves #1085
Resolves #1092
diff --git a/yapftests/reformatter_python3_test.py b/yapftests/reformatter_python3_test.py
index 221859a..17247cd 100644
--- a/yapftests/reformatter_python3_test.py
+++ b/yapftests/reformatter_python3_test.py
@@ -34,14 +34,14 @@
unformatted_code = textwrap.dedent("""\
def x(aaaaaaaaaaaaaaa:int,bbbbbbbbbbbbbbbb:str,ccccccccccccccc:dict,eeeeeeeeeeeeee:set={1, 2, 3})->bool:
pass
- """) # noqa
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def x(aaaaaaaaaaaaaaa: int,
bbbbbbbbbbbbbbbb: str,
ccccccccccccccc: dict,
eeeeeeeeeeeeee: set = {1, 2, 3}) -> bool:
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -49,12 +49,12 @@
unformatted_code = textwrap.dedent("""\
def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters()) -> ReturnType:
pass
- """) # noqa
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters()
) -> ReturnType:
pass
- """) # noqa
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -62,11 +62,11 @@
unformatted_code = textwrap.dedent("""\
def foo(a, *, kw):
return a+kw
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def foo(a, *, kw):
return a + kw
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -74,11 +74,11 @@
unformatted_code = textwrap.dedent("""\
def foo(a: list, b: "bar") -> dict:
return a+b
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def foo(a: list, b: "bar") -> dict:
return a + b
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -104,17 +104,17 @@
start = time.time()
if (await get_html()):
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines, verify=False))
def testNoSpacesAroundPowerOperator(self):
unformatted_code = textwrap.dedent("""\
a**b
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
a ** b
- """)
+ """) # noqa
try:
style.SetGlobalStyle(
@@ -130,10 +130,10 @@
def testSpacesAroundDefaultOrNamedAssign(self):
unformatted_code = textwrap.dedent("""\
f(a=5)
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
f(a = 5)
- """)
+ """) # noqa
try:
style.SetGlobalStyle(
@@ -155,7 +155,7 @@
def foo2(x: 'int' =42):
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def foo(x: int = 42):
pass
@@ -163,24 +163,24 @@
def foo2(x: 'int' = 42):
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
def testMatrixMultiplication(self):
unformatted_code = textwrap.dedent("""\
a=b@c
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
a = b @ c
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
def testNoneKeyword(self):
- code = """\
-None.__ne__()
-"""
+ code = textwrap.dedent("""\
+ None.__ne__()
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
@@ -194,7 +194,7 @@
async def foo():
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
import asyncio
@@ -206,7 +206,7 @@
async def foo():
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -216,7 +216,7 @@
async def inner():
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
@@ -226,13 +226,13 @@
self, *args: Optional[automation_converter.PyiCollectionAbc]) -> List[
automation_converter.PyiCollectionAbc]:
pass
- """) # noqa
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def _ReduceAbstractContainers(
self, *args: Optional[automation_converter.PyiCollectionAbc]
) -> List[automation_converter.PyiCollectionAbc]:
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
@@ -242,66 +242,66 @@
async with session.ws_connect(
r"ws://a_really_long_long_long_long_long_long_url") as ws:
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
async def start_websocket():
async with session.ws_connect(
r"ws://a_really_long_long_long_long_long_long_url") as ws:
pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
def testSplittingArguments(self):
- unformatted_code = """\
-async def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
- pass
+ unformatted_code = textwrap.dedent("""\
+ async def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
+ pass
-async def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
- pass
+ async def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
+ pass
-def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
- pass
+ def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
+ pass
-def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
- pass
-""" # noqa
- expected_formatted_code = """\
-async def open_file(
- file,
- mode='r',
- buffering=-1,
- encoding=None,
- errors=None,
- newline=None,
- closefd=True,
- opener=None
-):
- pass
+ def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
+ pass
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ async def open_file(
+ file,
+ mode='r',
+ buffering=-1,
+ encoding=None,
+ errors=None,
+ newline=None,
+ closefd=True,
+ opener=None
+ ):
+ pass
-async def run_sync_in_worker_thread(
- sync_fn, *args, cancellable=False, limiter=None
-):
- pass
+ async def run_sync_in_worker_thread(
+ sync_fn, *args, cancellable=False, limiter=None
+ ):
+ pass
-def open_file(
- file,
- mode='r',
- buffering=-1,
- encoding=None,
- errors=None,
- newline=None,
- closefd=True,
- opener=None
-):
- pass
+ def open_file(
+ file,
+ mode='r',
+ buffering=-1,
+ encoding=None,
+ errors=None,
+ newline=None,
+ closefd=True,
+ opener=None
+ ):
+ pass
-def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
- pass
-"""
+ def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
+ pass
+ """) # noqa
try:
style.SetGlobalStyle(
@@ -320,90 +320,90 @@
style.SetGlobalStyle(style.CreatePEP8Style())
def testDictUnpacking(self):
- unformatted_code = """\
-class Foo:
- def foo(self):
- foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
+ unformatted_code = textwrap.dedent("""\
+ class Foo:
+ def foo(self):
+ foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
- 'foo': 'foo',
+ 'foo': 'foo',
- **foofoofoo
- })
-"""
- expected_formatted_code = """\
-class Foo:
+ **foofoofoo
+ })
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ class Foo:
- def foo(self):
- foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
- 'foo': 'foo',
- **foofoofoo
- })
-"""
+ def foo(self):
+ foofoofoofoofoofoofoofoo('foofoofoofoofoo', {
+ 'foo': 'foo',
+ **foofoofoo
+ })
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
def testMultilineFormatString(self):
- code = """\
-# yapf: disable
-(f'''
- ''')
-# yapf: enable
-"""
# https://github.com/google/yapf/issues/513
+ code = textwrap.dedent("""\
+ # yapf: disable
+ (f'''
+ ''')
+ # yapf: enable
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
def testEllipses(self):
- code = """\
-def dirichlet(x12345678901234567890123456789012345678901234567890=...) -> None:
- return
-"""
# https://github.com/google/yapf/issues/533
+ code = textwrap.dedent("""\
+ def dirichlet(x12345678901234567890123456789012345678901234567890=...) -> None:
+ return
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
def testFunctionTypedReturnNextLine(self):
- code = """\
-def _GenerateStatsEntries(
- process_id: Text,
- timestamp: Optional[ffffffff.FFFFFFFFFFF] = None
-) -> Sequence[ssssssssssss.SSSSSSSSSSSSSSS]:
- pass
-"""
+ code = textwrap.dedent("""\
+ def _GenerateStatsEntries(
+ process_id: Text,
+ timestamp: Optional[ffffffff.FFFFFFFFFFF] = None
+ ) -> Sequence[ssssssssssss.SSSSSSSSSSSSSSS]:
+ pass
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
def testFunctionTypedReturnSameLine(self):
- code = """\
-def rrrrrrrrrrrrrrrrrrrrrr(
- ccccccccccccccccccccccc: Tuple[Text, Text]) -> List[Tuple[Text, Text]]:
- pass
-"""
+ code = textwrap.dedent("""\
+ def rrrrrrrrrrrrrrrrrrrrrr(
+ ccccccccccccccccccccccc: Tuple[Text, Text]) -> List[Tuple[Text, Text]]:
+ pass
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
def testAsyncForElseNotIndentedInsideBody(self):
code = textwrap.dedent("""\
- async def fn():
- async for message in websocket:
- for i in range(10):
- pass
+ async def fn():
+ async for message in websocket:
+ for i in range(10):
+ pass
+ else:
+ pass
else:
pass
- else:
- pass
- """)
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
def testForElseInAsyncNotMixedWithAsyncFor(self):
code = textwrap.dedent("""\
- async def fn():
- for i in range(10):
- pass
- else:
- pass
- """)
+ async def fn():
+ for i in range(10):
+ pass
+ else:
+ pass
+ """) # noqa
llines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(llines))
@@ -412,7 +412,7 @@
def raw_message( # pylint: disable=too-many-arguments
self, text, user_id=1000, chat_type='private', forward_date=None, forward_from=None):
pass
- """) # noqa
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def raw_message( # pylint: disable=too-many-arguments
self,
@@ -422,10 +422,148 @@
forward_date=None,
forward_from=None):
pass
- """)
+ """)
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+ def testTypeHintedYieldExpression(self):
+ # https://github.com/google/yapf/issues/1092
+ code = textwrap.dedent("""\
+ def my_coroutine():
+ x: int = yield
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(code)
+ self.assertCodeEqual(code, reformatter.Reformat(llines))
+
+ def testSyntaxMatch(self):
+ # https://github.com/google/yapf/issues/1045
+ # https://github.com/google/yapf/issues/1085
+ unformatted_code = textwrap.dedent("""\
+ a=3
+ b=0
+ match a :
+ case 0 :
+ b=1
+ case _ :
+ b=2
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ a = 3
+ b = 0
+ match a:
+ case 0:
+ b = 1
+ case _:
+ b = 2
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+
+ def testParenthsizedContextManager(self):
+ # https://github.com/google/yapf/issues/1064
+ unformatted_code = textwrap.dedent("""\
+ def test_copy_dimension(self):
+ with (Dataset() as target_ds,
+ Dataset() as source_ds):
+ do_something
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ def test_copy_dimension(self):
+ with (Dataset() as target_ds, Dataset() as source_ds):
+ do_something
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+
+ def testUnpackedTuple(self):
+ # https://github.com/google/yapf/issues/830
+ # https://github.com/google/yapf/issues/1060
+ unformatted_code = textwrap.dedent("""\
+ def a():
+ t = (2,3)
+ for i in range(5):
+ yield i,*t
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ def a():
+ t = (2, 3)
+ for i in range(5):
+ yield i, *t
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+
+ def testTypedTuple(self):
+ # https://github.com/google/yapf/issues/412
+ # https://github.com/google/yapf/issues/1058
+ code = textwrap.dedent("""\
+ t: tuple = 1, 2
+ args = tuple(x for x in [2], )
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(code)
+ self.assertCodeEqual(code, reformatter.Reformat(llines))
+
+ def testWalrusOperator(self):
+ # https://github.com/google/yapf/issues/894
+ unformatted_code = textwrap.dedent("""\
+ import os
+ a=[1,2,3,4]
+ if (n:=len(a))>2:
+ print()
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ import os
+
+ a = [1, 2, 3, 4]
+ if (n := len(a)) > 2:
+ print()
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+
+ def testCondAssign(self):
+ # https://github.com/google/yapf/issues/856
+ unformatted_code = textwrap.dedent("""\
+ def json(self) -> JSONTask:
+ result: JSONTask = {
+ "id": self.id,
+ "text": self.text,
+ "status": self.status,
+ "last_mod": self.last_mod_time
+ }
+ for i in "parent_id", "deadline", "reminder":
+ if x := getattr(self , i):
+ result[i] = x # type: ignore
+ return result
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ def json(self) -> JSONTask:
+ result: JSONTask = {
+ "id": self.id,
+ "text": self.text,
+ "status": self.status,
+ "last_mod": self.last_mod_time
+ }
+ for i in "parent_id", "deadline", "reminder":
+ if x := getattr(self, i):
+ result[i] = x # type: ignore
+ return result
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
+
+ def testCopyDictionary(self):
+ # https://github.com/google/yapf/issues/233
+ # https://github.com/google/yapf/issues/402
+ code = textwrap.dedent("""\
+ a_dict = {'key': 'value'}
+ a_dict_copy = {**a_dict}
+ print('a_dict:', a_dict)
+ print('a_dict_copy:', a_dict_copy)
+ """) # noqa
+ llines = yapf_test_helper.ParseAndUnwrap(code)
+ self.assertCodeEqual(code, reformatter.Reformat(llines))
+
if __name__ == '__main__':
unittest.main()