From 22ef70f3c497436adf59934b2ffa84ce6f962b88 Mon Sep 17 00:00:00 2001 From: agrieve Date: Thu, 23 Apr 2020 14:23:20 -0400 Subject: [PATCH] Update chromium style to match what's used in Chromium (#789) * Rename "chromium" style to "yapf". Chromium has decided to just follow PEP-8 Relevant Chromium style discussion: https://groups.google.com/a/chromium.org/g/chromium-dev/c/RcJgJdkNIdg/discussion * Added "yapf" to the style list. Co-authored-by: Andrew Grieve Co-authored-by: Bill Wendling Co-authored-by: Bill Wendling <5993918+gwelymernans@users.noreply.github.com> --- .style.yapf | 3 +- CHANGELOG | 2 + CONTRIBUTING.rst | 5 +- README.rst | 8 +- yapf/yapflib/style.py | 9 +- yapftests/blank_line_calculator_test.py | 2 +- yapftests/format_decision_state_test.py | 2 +- yapftests/main_test.py | 6 +- yapftests/reformatter_basic_test.py | 124 ++++++++++++++------- yapftests/reformatter_buganizer_test.py | 8 +- yapftests/reformatter_style_config_test.py | 2 +- yapftests/split_penalty_test.py | 2 +- yapftests/style_test.py | 50 +++------ yapftests/yapf_test.py | 44 ++++---- 14 files changed, 147 insertions(+), 120 deletions(-) diff --git a/.style.yapf b/.style.yapf index 823a973ea..fdd07237c 100644 --- a/.style.yapf +++ b/.style.yapf @@ -1,3 +1,2 @@ [style] -# YAPF uses the chromium style -based_on_style = chromium +based_on_style = yapf diff --git a/CHANGELOG b/CHANGELOG index dd0265e93..06192982a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,8 @@ parentheses. - New knob `SPACES_AROUND_SUBSCRIPT_COLON` to add spaces around the subscript / slice operator. +### Changed +- Renamed "chromium" style to "yapf". Chromium will now use PEP-8 directly. ### Fixed - Honor a disable directive at the end of a multiline comment. - Don't require splitting before comments in a list when diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index d68dedb77..fa6cda064 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -27,9 +27,8 @@ use Github pull requests for this purpose. YAPF coding style ----------------- -YAPF follows the `Chromium Python Style Guide -`_. It's the same -as the Google Python Style guide with two exceptions: +YAPF follows the `Google Python Style Guide +`_ with two exceptions: - 2 spaces for indentation rather than 4. - CamelCase for function and method names rather than snake_case. diff --git a/README.rst b/README.rst index cc53c891a..316d8a0c6 100644 --- a/README.rst +++ b/README.rst @@ -178,8 +178,8 @@ with a ``[yapf]`` heading. For example: The ``based_on_style`` setting determines which of the predefined styles this custom style is based on (think of it like subclassing). Four -styles are predefined: ``pep8`` (default), ``chromium``, ``google`` and -``facebook`` (see ``_STYLE_NAME_TO_FACTORY`` in style.py_). +styles are predefined: ``pep8`` (default), ``google``, ``yapf``, and ``facebook`` +(see ``_STYLE_NAME_TO_FACTORY`` in style.py_). .. _style.py: https://github.com/google/yapf/blob/master/yapf/yapflib/style.py#L445 @@ -188,9 +188,9 @@ example: .. code-block:: shell - --style='{based_on_style: chromium, indent_width: 4}' + --style='{based_on_style: pep8, indent_width: 2}' -This will take the ``chromium`` base style and modify it to have four space +This will take the ``pep8`` base style and modify it to have two space indentations. YAPF will search for the formatting style in the following manner: diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py index 1cc49ad13..4e0836549 100644 --- a/yapf/yapflib/style.py +++ b/yapf/yapflib/style.py @@ -494,12 +494,11 @@ def CreateGoogleStyle(): return style -def CreateChromiumStyle(): - """Create the Chromium formatting style.""" +def CreateYapfStyle(): + """Create the YAPF formatting style.""" style = CreateGoogleStyle() style['ALLOW_MULTILINE_DICTIONARY_KEYS'] = True style['ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS'] = False - style['INDENT_DICTIONARY_VALUE'] = True style['INDENT_WIDTH'] = 2 style['SPLIT_BEFORE_BITWISE_OPERATOR'] = True style['SPLIT_BEFORE_DOT'] = True @@ -527,16 +526,16 @@ def CreateFacebookStyle(): _STYLE_NAME_TO_FACTORY = dict( pep8=CreatePEP8Style, - chromium=CreateChromiumStyle, google=CreateGoogleStyle, facebook=CreateFacebookStyle, + yapf=CreateYapfStyle, ) _DEFAULT_STYLE_TO_FACTORY = [ - (CreateChromiumStyle(), CreateChromiumStyle), (CreateFacebookStyle(), CreateFacebookStyle), (CreateGoogleStyle(), CreateGoogleStyle), (CreatePEP8Style(), CreatePEP8Style), + (CreateYapfStyle(), CreateYapfStyle), ] diff --git a/yapftests/blank_line_calculator_test.py b/yapftests/blank_line_calculator_test.py index 8738c73df..1ec0a5e59 100644 --- a/yapftests/blank_line_calculator_test.py +++ b/yapftests/blank_line_calculator_test.py @@ -27,7 +27,7 @@ class BasicBlankLineCalculatorTest(yapf_test_helper.YAPFTest): @classmethod def setUpClass(cls): - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testDecorators(self): unformatted_code = textwrap.dedent("""\ diff --git a/yapftests/format_decision_state_test.py b/yapftests/format_decision_state_test.py index 612296008..39e7e8e03 100644 --- a/yapftests/format_decision_state_test.py +++ b/yapftests/format_decision_state_test.py @@ -28,7 +28,7 @@ class FormatDecisionStateTest(yapf_test_helper.YAPFTest): @classmethod def setUpClass(cls): - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testSimpleFunctionDefWithNoSplitting(self): code = textwrap.dedent(r""" diff --git a/yapftests/main_test.py b/yapftests/main_test.py index 138853ceb..94daaaa94 100644 --- a/yapftests/main_test.py +++ b/yapftests/main_test.py @@ -113,12 +113,12 @@ def testEchoInput(self): def testEchoInputWithStyle(self): code = 'def f(a = 1):\n return 2*a\n' - chromium_code = 'def f(a=1):\n return 2 * a\n' + yapf_code = 'def f(a=1):\n return 2 * a\n' with patched_input(code): with captured_output() as (out, _): - ret = yapf.main(['-', '--style=chromium']) + ret = yapf.main(['-', '--style=yapf']) self.assertEqual(ret, 0) - self.assertEqual(out.getvalue(), chromium_code) + self.assertEqual(out.getvalue(), yapf_code) def testEchoBadInput(self): bad_syntax = ' a = 1\n' diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py index 7868d0b06..62d6e089b 100644 --- a/yapftests/reformatter_basic_test.py +++ b/yapftests/reformatter_basic_test.py @@ -26,8 +26,8 @@ class BasicReformatterTest(yapf_test_helper.YAPFTest): @classmethod - def setUpClass(cls): # pylint: disable=g-missing-super-call - style.SetGlobalStyle(style.CreateChromiumStyle()) + def setUpClass(cls): + style.SetGlobalStyle(style.CreateYapfStyle()) def testSplittingAllArgs(self): style.SetGlobalStyle( @@ -306,13 +306,13 @@ class foo(object):\n \n def foobar(self):\n \n pass\n \n def barfoo(se try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, indent_blank_lines: true}')) + '{based_on_style: yapf, indent_blank_lines: true}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) unformatted_code, expected_formatted_code = (expected_formatted_code, unformatted_code) @@ -1850,12 +1850,12 @@ def succeeded(self, dddddddddddddd): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, allow_multiline_lambdas: true}')) + '{based_on_style: yapf, allow_multiline_lambdas: true}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testMultilineDictionaryKeys(self): unformatted_code = textwrap.dedent("""\ @@ -1882,13 +1882,13 @@ def testMultilineDictionaryKeys(self): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium, ' + style.CreateStyleFromConfig('{based_on_style: yapf, ' 'allow_multiline_dictionary_keys: true}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testStableDictionaryFormatting(self): code = textwrap.dedent("""\ @@ -1919,7 +1919,34 @@ def method(self): reformatted_code = reformatter.Reformat(uwlines) self.assertCodeEqual(code, reformatted_code) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) + + def testStableInlinedDictionaryFormatting(self): + try: + style.SetGlobalStyle(style.CreatePEP8Style()) + unformatted_code = textwrap.dedent("""\ + def _(): + url = "http://{0}/axis-cgi/admin/param.cgi?{1}".format( + value, urllib.urlencode({'action': 'update', 'parameter': value})) + """) + expected_formatted_code = textwrap.dedent("""\ + def _(): + url = "http://{0}/axis-cgi/admin/param.cgi?{1}".format( + value, urllib.urlencode({ + 'action': 'update', + 'parameter': value + })) + """) + + uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + reformatted_code = reformatter.Reformat(uwlines) + self.assertCodeEqual(expected_formatted_code, reformatted_code) + + uwlines = yapf_test_helper.ParseAndUnwrap(reformatted_code) + reformatted_code = reformatter.Reformat(uwlines) + self.assertCodeEqual(expected_formatted_code, reformatted_code) + finally: + style.SetGlobalStyle(style.CreateYapfStyle()) def testDontSplitKeywordValueArguments(self): unformatted_code = textwrap.dedent("""\ @@ -1990,7 +2017,7 @@ def testNoSplittingWhenBinPacking(self): reformatted_code = reformatter.Reformat(uwlines) self.assertCodeEqual(code, reformatted_code) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testNotSplittingAfterSubscript(self): unformatted_code = textwrap.dedent("""\ @@ -2102,7 +2129,7 @@ def testSplittingArgumentsTerminatedByComma(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, ' + '{based_on_style: yapf, ' 'split_arguments_when_comma_terminated: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) @@ -2113,7 +2140,7 @@ def testSplittingArgumentsTerminatedByComma(self): reformatted_code = reformatter.Reformat(uwlines) self.assertCodeEqual(expected_formatted_code, reformatted_code) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testImportAsList(self): code = textwrap.dedent("""\ @@ -2402,14 +2429,14 @@ def __init__(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, ' + '{based_on_style: yapf, ' 'blank_line_before_class_docstring: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testBlankLineBeforeModuleDocstring(self): unformatted_code = textwrap.dedent('''\ @@ -2464,7 +2491,7 @@ def foobar(): self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testTupleCohesion(self): unformatted_code = textwrap.dedent("""\ @@ -2559,13 +2586,13 @@ def testSplittingBeforeFirstArgumentOnFunctionCall(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, split_before_first_argument: True}')) + '{based_on_style: yapf, split_before_first_argument: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testSplittingBeforeFirstArgumentOnFunctionDefinition(self): """Tests split_before_first_argument on a function definition.""" @@ -2583,13 +2610,13 @@ def _GetNumberOfSecondsFromElements( try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, split_before_first_argument: True}')) + '{based_on_style: yapf, split_before_first_argument: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testSplittingBeforeFirstArgumentOnCompoundStatement(self): """Tests split_before_first_argument on a compound statement.""" @@ -2609,13 +2636,13 @@ def testSplittingBeforeFirstArgumentOnCompoundStatement(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, split_before_first_argument: True}')) + '{based_on_style: yapf, split_before_first_argument: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testCoalesceBracketsOnDict(self): """Tests coalesce_brackets on a dictionary.""" @@ -2645,13 +2672,13 @@ def testCoalesceBracketsOnDict(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, coalesce_brackets: True}')) + '{based_on_style: yapf, coalesce_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testSplitAfterComment(self): code = textwrap.dedent("""\ @@ -2666,13 +2693,32 @@ def testSplitAfterComment(self): try: style.SetGlobalStyle( style.CreateStyleFromConfig( - '{based_on_style: chromium, coalesce_brackets: True, ' + '{based_on_style: yapf, coalesce_brackets: True, ' 'dedent_closing_brackets: true}')) + uwlines = yapf_test_helper.ParseAndUnwrap(code) + self.assertCodeEqual(code, reformatter.Reformat(uwlines)) + finally: + style.SetGlobalStyle(style.CreateYapfStyle()) + + @unittest.skipUnless(not py3compat.PY3, 'Requires Python 2.7') + def testAsyncAsNonKeyword(self): + try: + style.SetGlobalStyle(style.CreatePEP8Style()) + + # In Python 2, async may be used as a non-keyword identifier. + code = textwrap.dedent("""\ + from util import async + + + class A(object): + def foo(self): + async.run() + """) uwlines = yapf_test_helper.ParseAndUnwrap(code) self.assertCodeEqual(code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testDisableEndingCommaHeuristic(self): code = textwrap.dedent("""\ @@ -2681,13 +2727,13 @@ def testDisableEndingCommaHeuristic(self): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' disable_ending_comma_heuristic: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(code) self.assertCodeEqual(code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testDedentClosingBracketsWithTypeAnnotationExceedingLineLength(self): unformatted_code = textwrap.dedent("""\ @@ -2713,14 +2759,14 @@ def function( try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' dedent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testIndentClosingBracketsWithTypeAnnotationExceedingLineLength(self): unformatted_code = textwrap.dedent("""\ @@ -2746,14 +2792,14 @@ def function( try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' indent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testIndentClosingBracketsInFunctionCall(self): unformatted_code = textwrap.dedent("""\ @@ -2781,14 +2827,14 @@ def function( try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' indent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testIndentClosingBracketsInTuple(self): unformatted_code = textwrap.dedent("""\ @@ -2816,14 +2862,14 @@ def function(): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' indent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testIndentClosingBracketsInList(self): unformatted_code = textwrap.dedent("""\ @@ -2851,14 +2897,14 @@ def function(): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' indent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testIndentClosingBracketsInDict(self): unformatted_code = textwrap.dedent("""\ @@ -2892,14 +2938,14 @@ def function(): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium,' + style.CreateStyleFromConfig('{based_on_style: yapf,' ' indent_closing_brackets: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testMultipleDictionariesInList(self): unformatted_code = textwrap.dedent("""\ diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py index b09445513..653e88557 100644 --- a/yapftests/reformatter_buganizer_test.py +++ b/yapftests/reformatter_buganizer_test.py @@ -26,7 +26,7 @@ class BuganizerFixes(yapf_test_helper.YAPFTest): @classmethod def setUpClass(cls): - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testB137580392(self): code = """\ @@ -1720,13 +1720,13 @@ def __eq__(self, other): try: style.SetGlobalStyle( - style.CreateStyleFromConfig('{based_on_style: chromium, ' + style.CreateStyleFromConfig('{based_on_style: yapf, ' 'split_before_logical_operator: True}')) uwlines = yapf_test_helper.ParseAndUnwrap(code) self.assertCodeEqual(code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testB22527411(self): unformatted_code = textwrap.dedent("""\ @@ -1884,7 +1884,7 @@ def f(): uwlines = yapf_test_helper.ParseAndUnwrap(code) self.assertCodeEqual(code, reformatter.Reformat(uwlines)) finally: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def testB19353268(self): code = textwrap.dedent("""\ diff --git a/yapftests/reformatter_style_config_test.py b/yapftests/reformatter_style_config_test.py index 5fe9709c1..d77c19700 100644 --- a/yapftests/reformatter_style_config_test.py +++ b/yapftests/reformatter_style_config_test.py @@ -29,7 +29,7 @@ def setUp(self): def testSetGlobalStyle(self): try: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) unformatted_code = textwrap.dedent(u"""\ for i in range(5): print('bar') diff --git a/yapftests/split_penalty_test.py b/yapftests/split_penalty_test.py index 895445cfc..4d551291e 100644 --- a/yapftests/split_penalty_test.py +++ b/yapftests/split_penalty_test.py @@ -36,7 +36,7 @@ class SplitPenaltyTest(yapf_test_helper.YAPFTest): @classmethod def setUpClass(cls): - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) def _ParseAndComputePenalties(self, code, dumptree=False): """Parses the code and computes split penalties. diff --git a/yapftests/style_test.py b/yapftests/style_test.py index 3d4e1b141..a9c478daa 100644 --- a/yapftests/style_test.py +++ b/yapftests/style_test.py @@ -74,23 +74,20 @@ def testIntOrIntListConverter(self): self.assertEqual(style._IntOrIntListConverter('1, 2, 3'), [1, 2, 3]) -def _LooksLikeChromiumStyle(cfg): - return (cfg['INDENT_WIDTH'] == 2 and - cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF']) - - def _LooksLikeGoogleStyle(cfg): - return (cfg['INDENT_WIDTH'] == 4 and - cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF']) + return cfg['COLUMN_LIMIT'] == 80 and cfg['SPLIT_COMPLEX_COMPREHENSION'] def _LooksLikePEP8Style(cfg): - return (cfg['INDENT_WIDTH'] == 4 and - not cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF']) + return cfg['COLUMN_LIMIT'] == 79 def _LooksLikeFacebookStyle(cfg): - return cfg['INDENT_WIDTH'] == 4 and cfg['DEDENT_CLOSING_BRACKETS'] + return cfg['DEDENT_CLOSING_BRACKETS'] + + +def _LooksLikeYapfStyle(cfg): + return cfg['SPLIT_BEFORE_DOT'] class PredefinedStylesByNameTest(unittest.TestCase): @@ -114,10 +111,10 @@ def testGoogleByName(self): cfg = style.CreateStyleFromConfig(google_name) self.assertTrue(_LooksLikeGoogleStyle(cfg)) - def testChromiumByName(self): - for chromium_name in ('chromium', 'Chromium', 'CHROMIUM'): - cfg = style.CreateStyleFromConfig(chromium_name) - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + def testYapfByName(self): + for yapf_name in ('yapf', 'YAPF'): + cfg = style.CreateStyleFromConfig(yapf_name) + self.assertTrue(_LooksLikeYapfStyle(cfg)) def testFacebookByName(self): for fb_name in ('facebook', 'FACEBOOK', 'Facebook'): @@ -157,17 +154,6 @@ def testDefaultBasedOnPEP8Style(self): self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 40) - def testDefaultBasedOnChromiumStyle(self): - cfg = textwrap.dedent(u'''\ - [style] - based_on_style = chromium - continuation_indent_width = 30 - ''') - with utils.TempFileContents(self.test_tmpdir, cfg) as filepath: - cfg = style.CreateStyleFromConfig(filepath) - self.assertTrue(_LooksLikeChromiumStyle(cfg)) - self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 30) - def testDefaultBasedOnGoogleStyle(self): cfg = textwrap.dedent(u'''\ [style] @@ -193,25 +179,25 @@ def testDefaultBasedOnFacebookStyle(self): def testBoolOptionValue(self): cfg = textwrap.dedent(u'''\ [style] - based_on_style = chromium + based_on_style = pep8 SPLIT_BEFORE_NAMED_ASSIGNS=False split_before_logical_operator = true ''') with utils.TempFileContents(self.test_tmpdir, cfg) as filepath: cfg = style.CreateStyleFromConfig(filepath) - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['SPLIT_BEFORE_NAMED_ASSIGNS'], False) self.assertEqual(cfg['SPLIT_BEFORE_LOGICAL_OPERATOR'], True) def testStringListOptionValue(self): cfg = textwrap.dedent(u'''\ [style] - based_on_style = chromium + based_on_style = pep8 I18N_FUNCTION_CALL = N_, V_, T_ ''') with utils.TempFileContents(self.test_tmpdir, cfg) as filepath: cfg = style.CreateStyleFromConfig(filepath) - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['I18N_FUNCTION_CALL'], ['N_', 'V_', 'T_']) def testErrorNoStyleFile(self): @@ -254,7 +240,7 @@ def testDefaultBasedOnStyle(self): 'blank_line_before_nested_class_or_def': True } cfg = style.CreateStyleFromConfig(config_dict) - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['INDENT_WIDTH'], 2) def testDefaultBasedOnStyleBadDict(self): @@ -277,7 +263,7 @@ def testDefaultBasedOnStyle(self): '{based_on_style: pep8,' ' indent_width: 2,' ' blank_line_before_nested_class_or_def: True}') - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['INDENT_WIDTH'], 2) def testDefaultBasedOnStyleNotStrict(self): @@ -285,7 +271,7 @@ def testDefaultBasedOnStyleNotStrict(self): '{based_on_style : pep8' ' ,indent_width=2' ' blank_line_before_nested_class_or_def:True}') - self.assertTrue(_LooksLikeChromiumStyle(cfg)) + self.assertTrue(_LooksLikePEP8Style(cfg)) self.assertEqual(cfg['INDENT_WIDTH'], 2) def testDefaultBasedOnExplicitlyUnicodeTypeString(self): diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py index 46fde99d2..9bd09ac43 100644 --- a/yapftests/yapf_test.py +++ b/yapftests/yapf_test.py @@ -43,7 +43,7 @@ class FormatCodeTest(yapf_test_helper.YAPFTest): def _Check(self, unformatted_code, expected_formatted_code): formatted_code, _ = yapf_api.FormatCode( - unformatted_code, style_config='chromium') + unformatted_code, style_config='yapf') self.assertCodeEqual(expected_formatted_code, formatted_code) def testSimple(self): @@ -95,7 +95,7 @@ def testFormatFile(self): if True: pass """) - expected_formatted_code_chromium = textwrap.dedent(u"""\ + expected_formatted_code_yapf = textwrap.dedent(u"""\ if True: pass """) @@ -103,9 +103,8 @@ def testFormatFile(self): formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8') self.assertCodeEqual(expected_formatted_code_pep8, formatted_code) - formatted_code, _, _ = yapf_api.FormatFile( - filepath, style_config='chromium') - self.assertCodeEqual(expected_formatted_code_chromium, formatted_code) + formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf') + self.assertCodeEqual(expected_formatted_code_yapf, formatted_code) def testDisableLinesPattern(self): unformatted_code = textwrap.dedent(u"""\ @@ -361,8 +360,7 @@ def testDisabledMultilineStringInDictionary(self): ] """) with utils.TempFileContents(self.test_tmpdir, code) as filepath: - formatted_code, _, _ = yapf_api.FormatFile( - filepath, style_config='chromium') + formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf') self.assertCodeEqual(code, formatted_code) def testDisabledWithPrecedingText(self): @@ -381,15 +379,13 @@ def testDisabledWithPrecedingText(self): ] """) with utils.TempFileContents(self.test_tmpdir, code) as filepath: - formatted_code, _, _ = yapf_api.FormatFile( - filepath, style_config='chromium') + formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf') self.assertCodeEqual(code, formatted_code) def testCRLFLineEnding(self): code = u'class _():\r\n pass\r\n' with utils.TempFileContents(self.test_tmpdir, code) as filepath: - formatted_code, _, _ = yapf_api.FormatFile( - filepath, style_config='chromium') + formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf') self.assertCodeEqual(code, formatted_code) @@ -502,7 +498,7 @@ def testReadFromStdinWithEscapedStrings(self): """) self.assertYapfReformats(unformatted_code, expected_formatted_code) - def testSetChromiumStyle(self): + def testSetYapfStyle(self): unformatted_code = textwrap.dedent("""\ def foo(): # trail x = 37 @@ -514,9 +510,9 @@ def foo(): # trail self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--style=chromium']) + extra_options=['--style=yapf']) - def testSetCustomStyleBasedOnChromium(self): + def testSetCustomStyleBasedOnYapf(self): unformatted_code = textwrap.dedent("""\ def foo(): # trail x = 37 @@ -527,7 +523,7 @@ def foo(): # trail """) style_file = textwrap.dedent(u'''\ [style] - based_on_style = chromium + based_on_style = yapf spaces_before_comment = 4 ''') with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath: @@ -1164,7 +1160,7 @@ def bar(): self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--lines', '1-1', '--style', 'chromium']) + extra_options=['--lines', '1-1', '--style', 'yapf']) def testMultilineCommentFormattingDisabled(self): unformatted_code = textwrap.dedent("""\ @@ -1198,7 +1194,7 @@ def testMultilineCommentFormattingDisabled(self): self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--lines', '1-1', '--style', 'chromium']) + extra_options=['--lines', '1-1', '--style', 'yapf']) def testTrailingCommentsWithDisabledFormatting(self): unformatted_code = textwrap.dedent("""\ @@ -1218,7 +1214,7 @@ def testTrailingCommentsWithDisabledFormatting(self): self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--lines', '1-1', '--style', 'chromium']) + extra_options=['--lines', '1-1', '--style', 'yapf']) def testUseTabs(self): unformatted_code = """\ @@ -1233,7 +1229,7 @@ def foo_function(): """ style_contents = u"""\ [style] -based_on_style = chromium +based_on_style = yapf USE_TABS = true INDENT_WIDTH=1 """ @@ -1257,7 +1253,7 @@ def f(): """ style_contents = u"""\ [style] -based_on_style = chromium +based_on_style = yapf USE_TABS = true INDENT_WIDTH=1 """ @@ -1282,7 +1278,7 @@ def foo_function(arg1, arg2, """ style_contents = u"""\ [style] -based_on_style = chromium +based_on_style = yapf USE_TABS = true COLUMN_LIMIT=32 INDENT_WIDTH=4 @@ -1310,7 +1306,7 @@ def foo_function(arg1, arg2, """ style_contents = u"""\ [style] -based_on_style = chromium +based_on_style = yapf USE_TABS = true COLUMN_LIMIT=32 INDENT_WIDTH=4 @@ -1407,7 +1403,7 @@ def testSpacingBeforeCommentsInDicts(self): self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--style', 'chromium', '--lines', '1-1']) + extra_options=['--style', 'yapf', '--lines', '1-1']) @unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6') def testNoSpacesAroundBinaryOperators(self): @@ -1457,7 +1453,7 @@ def testDisableWithLineRanges(self): self.assertYapfReformats( unformatted_code, expected_formatted_code, - extra_options=['--style', 'chromium', '--lines', '1-100']) + extra_options=['--style', 'yapf', '--lines', '1-100']) class BadInputTest(unittest.TestCase):