diff options
Diffstat (limited to 'yapftests/reformatter_python3_test.py')
-rw-r--r-- | yapftests/reformatter_python3_test.py | 256 |
1 files changed, 173 insertions, 83 deletions
diff --git a/yapftests/reformatter_python3_test.py b/yapftests/reformatter_python3_test.py index 9be6528..b5d68e8 100644 --- a/yapftests/reformatter_python3_test.py +++ b/yapftests/reformatter_python3_test.py @@ -29,14 +29,14 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): """Test a few constructs that are new Python 3 syntax.""" @classmethod - def setUpClass(cls): + def setUpClass(cls): # pylint: disable=g-missing-super-call style.SetGlobalStyle(style.CreatePEP8Style()) def testTypedNames(self): unformatted_code = textwrap.dedent("""\ def x(aaaaaaaaaaaaaaa:int,bbbbbbbbbbbbbbbb:str,ccccccccccccccc:dict,eeeeeeeeeeeeee:set={1, 2, 3})->bool: pass - """) + """) # noqa expected_formatted_code = textwrap.dedent("""\ def x(aaaaaaaaaaaaaaa: int, bbbbbbbbbbbbbbbb: str, @@ -44,8 +44,21 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): eeeeeeeeeeeeee: set = {1, 2, 3}) -> bool: pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) + + def testTypedNameWithLongNamedArg(self): + unformatted_code = textwrap.dedent("""\ + def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters()) -> ReturnType: + pass + """) # noqa + expected_formatted_code = textwrap.dedent("""\ + def func(arg=long_function_call_that_pushes_the_line_over_eighty_characters() + ) -> ReturnType: + pass + """) # noqa + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testKeywordOnlyArgSpecifier(self): unformatted_code = textwrap.dedent("""\ @@ -56,8 +69,8 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): def foo(a, *, kw): return a + kw """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) @unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6') def testPEP448ParameterExpansion(self): @@ -73,8 +86,8 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): {**{**x}, **x} {'a': 1, **kw, 'b': 3, **kw2} """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testAnnotations(self): unformatted_code = textwrap.dedent("""\ @@ -85,14 +98,14 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): def foo(a: list, b: "bar") -> dict: return a + b """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testExecAsNonKeyword(self): unformatted_code = 'methods.exec( sys.modules[name])\n' expected_formatted_code = 'methods.exec(sys.modules[name])\n' - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testAsyncFunctions(self): if sys.version_info[1] < 5: @@ -113,41 +126,45 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): if (await get_html()): pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(code) - self.assertCodeEqual(code, reformatter.Reformat(uwlines, verify=False)) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines, verify=False)) def testNoSpacesAroundPowerOperator(self): + unformatted_code = textwrap.dedent("""\ + a**b + """) + expected_formatted_code = textwrap.dedent("""\ + a ** b + """) + try: style.SetGlobalStyle( style.CreateStyleFromConfig( '{based_on_style: pep8, SPACES_AROUND_POWER_OPERATOR: True}')) - unformatted_code = textwrap.dedent("""\ - a**b - """) - expected_formatted_code = textwrap.dedent("""\ - a ** b - """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, - reformatter.Reformat(uwlines)) + reformatter.Reformat(llines)) finally: style.SetGlobalStyle(style.CreatePEP8Style()) def testSpacesAroundDefaultOrNamedAssign(self): + unformatted_code = textwrap.dedent("""\ + f(a=5) + """) + expected_formatted_code = textwrap.dedent("""\ + f(a = 5) + """) + try: style.SetGlobalStyle( style.CreateStyleFromConfig( '{based_on_style: pep8, ' 'SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN: True}')) - unformatted_code = textwrap.dedent("""\ - f(a=5) - """) - expected_formatted_code = textwrap.dedent("""\ - f(a = 5) - """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, - reformatter.Reformat(uwlines)) + reformatter.Reformat(llines)) finally: style.SetGlobalStyle(style.CreatePEP8Style()) @@ -168,8 +185,8 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): def foo2(x: 'int' = 42): pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testMatrixMultiplication(self): unformatted_code = textwrap.dedent("""\ @@ -178,15 +195,15 @@ class TestsForPython3Code(yapf_test_helper.YAPFTest): expected_formatted_code = textwrap.dedent("""\ a = b @ c """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testNoneKeyword(self): code = """\ None.__ne__() """ - uwlines = yapf_test_helper.ParseAndUnwrap(code) - self.assertCodeEqual(code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) def testAsyncWithPrecedingComment(self): if sys.version_info[1] < 5: @@ -213,19 +230,20 @@ None.__ne__() async def foo(): pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testAsyncFunctionsNested(self): if sys.version_info[1] < 5: return code = textwrap.dedent("""\ async def outer(): + async def inner(): pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(code) - self.assertCodeEqual(code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) def testKeepTypesIntact(self): if sys.version_info[1] < 5: @@ -235,15 +253,15 @@ None.__ne__() self, *args: Optional[automation_converter.PyiCollectionAbc]) -> List[ automation_converter.PyiCollectionAbc]: pass - """) + """) # noqa expected_formatted_code = textwrap.dedent("""\ def _ReduceAbstractContainers( - self, *args: Optional[automation_converter.PyiCollectionAbc] + self, *args: Optional[automation_converter.PyiCollectionAbc] ) -> List[automation_converter.PyiCollectionAbc]: pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testContinuationIndentWithAsync(self): if sys.version_info[1] < 5: @@ -260,22 +278,14 @@ None.__ne__() r"ws://a_really_long_long_long_long_long_long_url") as ws: pass """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testSplittingArguments(self): if sys.version_info[1] < 5: return - try: - style.SetGlobalStyle( - style.CreateStyleFromConfig( - '{based_on_style: pep8, ' - 'dedent_closing_brackets: true, ' - 'coalesce_brackets: false, ' - 'space_between_ending_comma_and_closing_bracket: false, ' - 'split_arguments_when_comma_terminated: true, ' - 'split_before_first_argument: true}')) - unformatted_code = """\ + + unformatted_code = """\ async def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): pass @@ -287,36 +297,36 @@ def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline= def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None): pass -""" - expected_formatted_code = """\ +""" # noqa + expected_formatted_code = """\ async def open_file( - file, - mode='r', - buffering=-1, - encoding=None, - errors=None, - newline=None, - closefd=True, - opener=None + 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 + 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 + file, + mode='r', + buffering=-1, + encoding=None, + errors=None, + newline=None, + closefd=True, + opener=None ): pass @@ -324,9 +334,20 @@ def open_file( def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None): pass """ - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + + try: + style.SetGlobalStyle( + style.CreateStyleFromConfig( + '{based_on_style: pep8, ' + 'dedent_closing_brackets: true, ' + 'coalesce_brackets: false, ' + 'space_between_ending_comma_and_closing_bracket: false, ' + 'split_arguments_when_comma_terminated: true, ' + 'split_before_first_argument: true}')) + + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, - reformatter.Reformat(uwlines)) + reformatter.Reformat(llines)) finally: style.SetGlobalStyle(style.CreatePEP8Style()) @@ -345,14 +366,15 @@ class Foo: """ expected_formatted_code = """\ class Foo: + def foo(self): foofoofoofoofoofoofoofoo('foofoofoofoofoo', { 'foo': 'foo', **foofoofoo }) """ - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) def testMultilineFormatString(self): if sys.version_info[1] < 6: @@ -364,8 +386,8 @@ class Foo: # yapf: enable """ # https://github.com/google/yapf/issues/513 - uwlines = yapf_test_helper.ParseAndUnwrap(code) - self.assertCodeEqual(code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) def testEllipses(self): if sys.version_info[1] < 6: @@ -375,8 +397,76 @@ def dirichlet(x12345678901234567890123456789012345678901234567890=...) -> None: return """ # https://github.com/google/yapf/issues/533 - uwlines = yapf_test_helper.ParseAndUnwrap(code) - self.assertCodeEqual(code, reformatter.Reformat(uwlines)) + 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 +""" + 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 +""" + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) + + def testAsyncForElseNotIndentedInsideBody(self): + if sys.version_info[1] < 5: + return + code = textwrap.dedent("""\ + async def fn(): + async for message in websocket: + for i in range(10): + pass + else: + pass + else: + pass + """) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) + + def testForElseInAsyncNotMixedWithAsyncFor(self): + if sys.version_info[1] < 5: + return + code = textwrap.dedent("""\ + async def fn(): + for i in range(10): + pass + else: + pass + """) + llines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(llines)) + + def testParameterListIndentationConflicts(self): + unformatted_code = textwrap.dedent("""\ + def raw_message( # pylint: disable=too-many-arguments + self, text, user_id=1000, chat_type='private', forward_date=None, forward_from=None): + pass + """) # noqa + expected_formatted_code = textwrap.dedent("""\ + def raw_message( # pylint: disable=too-many-arguments + self, + text, + user_id=1000, + chat_type='private', + forward_date=None, + forward_from=None): + pass + """) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) if __name__ == '__main__': |