Repository: hhatto/autopep8

Issue:
Inconsistently rewrites W504/3 (newline after/before binary operator)
Consider this input, which has two lines of whitespace above the function

```python
q = 1


def x(y, z):
    if (
            y and
            z
    ):
        pass
```

`autopep8` will not touch it (this is good, this is what I expect)

However if it has more line breaks at the top this happens:

```diff
 q = 1
-
-
 
 
 def x(y, z):
     if (
-            y and
-            z
+            y
+            and z
     ):
         pass
```

I don't think autopep8 should be rewriting W504 / W503 unless asked to and especially not when fixing an unrelated issue. pycodestyle and flake8 by default turns those warnings off

```
$ /home/asottile/.cache/pre-commit/repoadsj3zb8/py_env-python3/bin/autopep8 --version
autopep8 1.4.2 (pycodestyle: 2.4.0)
```

Discussion:

Patch:
diff --git a/autopep8.py b/autopep8.py
index 8fc548a..3a55a01 100755
--- a/autopep8.py
+++ b/autopep8.py
@@ -100,6 +100,9 @@ SHORTEN_OPERATOR_GROUPS = frozenset([
 
 DEFAULT_IGNORE = 'E226,E24,W50,W690'    # TODO: use pycodestyle.DEFAULT_IGNORE
 DEFAULT_INDENT_SIZE = 4
+# these fixes conflict with each other, if the `--ignore` setting causes both
+# to be enabled, disable both of them
+CONFLICTING_CODES = ('W503', 'W504')
 
 SELECTED_GLOBAL_FIXED_METHOD_CODES = ['W602', ]
 
@@ -3662,6 +3665,14 @@ def parse_args(arguments, apply_config=False):
 
     if args.ignore:
         args.ignore = _split_comma_separated(args.ignore)
+        if not all(
+                any(
+                    conflicting_code.startswith(ignore_code)
+                    for ignore_code in args.ignore
+                )
+                for conflicting_code in CONFLICTING_CODES
+        ):
+            args.ignore.update(CONFLICTING_CODES)
     elif not args.select:
         if args.aggressive:
             # Enable everything by default if aggressive.


Test patch:
diff --git a/test/test_autopep8.py b/test/test_autopep8.py
index 14981e8..a35d318 100755
--- a/test/test_autopep8.py
+++ b/test/test_autopep8.py
@@ -4475,6 +4475,24 @@ if True:
         with autopep8_context(line, options=['--select=W504', '--ignore=E']) as result:
             self.assertEqual(fixed, result)
 
+    def test_w504_not_applied_by_default_when_modifying_with_ignore(self):
+        line = """\
+q = 1
+
+
+
+
+def x(y, z):
+    if (
+            y and
+            z
+    ):
+        pass
+"""
+        fixed = line.replace('\n\n\n\n', '\n\n')
+        with autopep8_context(line, options=['--ignore=E265']) as result:
+            self.assertEqual(fixed, result)
+
     def test_w601(self):
         line = 'a = {0: 1}\na.has_key(0)\n'
         fixed = 'a = {0: 1}\n0 in a\n'