diff --git a/autopep8.py b/autopep8.py index 1cd41006..ab160188 100755 --- a/autopep8.py +++ b/autopep8.py @@ -3691,7 +3691,7 @@ def create_parser(): return parser -def _expand_codes(codes): +def _expand_codes(codes, ignore_codes): """expand to individual E/W codes""" ret = set() @@ -3705,13 +3705,27 @@ def _expand_codes(codes): ): is_conflict = True + is_ignore_w503 = "W503" in ignore_codes + is_ignore_w504 = "W504" in ignore_codes + for code in codes: if code == "W": - ret.update({"W1", "W2", "W3", "W503", "W505", "W6"}) + if is_ignore_w503 and is_ignore_w504: + ret.update({"W1", "W2", "W3", "W505", "W6"}) + elif is_ignore_w503: + ret.update({"W1", "W2", "W3", "W504", "W505", "W6"}) + else: + ret.update({"W1", "W2", "W3", "W503", "W505", "W6"}) elif code in ("W5", "W50"): - ret.update({"W503", "W505"}) + if is_ignore_w503 and is_ignore_w504: + ret.update({"W505"}) + elif is_ignore_w503: + ret.update({"W504", "W505"}) + else: + ret.update({"W503", "W505"}) elif not (code in ("W503", "W504") and is_conflict): ret.add(code) + return ret @@ -3762,7 +3776,11 @@ def parse_args(arguments, apply_config=False): parser.error('--max-line-length must be greater than 0') if args.select: - args.select = _expand_codes(_split_comma_separated(args.select)) + args.select = _expand_codes(_split_comma_separated(args.select), + ( + _split_comma_separated(args.ignore) + if args.ignore else [] + )) if args.ignore: args.ignore = _split_comma_separated(args.ignore) diff --git a/test/test_autopep8.py b/test/test_autopep8.py index ab05e53b..fff02b11 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -4597,6 +4597,70 @@ def test_w503_with_line_comments(self): with autopep8_context(line, options=['--select=W503', '--ignore=E']) as result: self.assertEqual(fixed, result) + def test_ignore_only_w503_with_select_w(self): + line = """\ +a = ( + 11 + 22 + + 33 + + 44 + + 55 +) +""" + fixed = """\ +a = ( + 11 + 22 + + 33 + + 44 + + 55 +) +""" + with autopep8_context(line, options=['--select=W', '--ignore=W503']) as result: + self.assertEqual(fixed, result) + with autopep8_context(line, options=['--select=W5', '--ignore=W503']) as result: + self.assertEqual(fixed, result) + with autopep8_context(line, options=['--select=W50', '--ignore=W503']) as result: + self.assertEqual(fixed, result) + + def test_ignore_only_w504_with_select_w(self): + line = """\ +a = ( + 11 + 22 + + 33 + + 44 + + 55 +) +""" + fixed = """\ +a = ( + 11 + 22 + + 33 + + 44 + + 55 +) +""" + with autopep8_context(line, options=['--select=W', '--ignore=W504']) as result: + self.assertEqual(fixed, result) + with autopep8_context(line, options=['--select=W5', '--ignore=W504']) as result: + self.assertEqual(fixed, result) + with autopep8_context(line, options=['--select=W50', '--ignore=W504']) as result: + self.assertEqual(fixed, result) + + def test_ignore_w503_and_w504_with_select_w(self): + line = """\ +a = ( + 11 + 22 + + 33 + + 44 + + 55 +) +""" + with autopep8_context(line, options=['--select=W', '--ignore=W503,W504']) as result: + self.assertEqual(line, result) + with autopep8_context(line, options=['--select=W5', '--ignore=W503,W504']) as result: + self.assertEqual(line, result) + with autopep8_context(line, options=['--select=W50', '--ignore=W503,W504']) as result: + self.assertEqual(line, result) + def test_w504(self): line = '(width == 0 +\n height == 0)\n' fixed = '(width == 0\n + height == 0)\n'