Subject: 24.5; [PATCH] python.el: fix close paren indentation to match pep8

From: Tommi Komulainen <[email-masked]>
Date: Sat, 20 Jun 2015 12:56:40 +0200

>From da684af7265bf40e4140328c36011fea6b040373 Mon Sep 17 00:00:00 2001
From: Tommi Komulainen <[email-masked]>
Date: Fri, 19 Jun 2015 18:53:52 +0200
Subject: [PATCH] python.el: fix close paren indentation to match pep8

When opening paren is followed by newline the closing paren should follow
the current indentation. Otherwise the closing paren should be aligned
with the opening paren. This fixes the latter case. #20742
---
 lisp/progmodes/python.el       | 12 +++++++++---
 test/automated/python-tests.el | 42 +++++++++++++++++++++++++++++++++---------
 2 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index de2d2d1..013a565 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -952,12 +952,18 @@ possibilities can be narrowed to specific
indentation points."
         (`(,(or :after-line
                 :after-comment
                 :inside-string
-                :after-backslash
-                :inside-paren-at-closing-paren
-                :inside-paren-at-closing-nested-paren) . ,start)
+                :after-backslash) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
+        (`(,(or :inside-paren-at-closing-paren
+                :inside-paren-at-closing-nested-paren) . ,start)
+         (goto-char (+ 1 start))
+         (if (looking-at "[ \t]*\\(?:#\\|$\\)")
+             ;; Copy previous indentation.
+             (current-indentation)
+           ;; Align with opening paren.
+           (current-column)))
         (`(,(or :after-block-start
                 :after-backslash-first-line
                 :inside-paren-newline-start) . ,start)
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index 42c26fc..f35f188 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -171,16 +171,28 @@ aliqua."
   "First pep8 case."
   (python-tests-with-temp-buffer
    "# Aligned with opening delimiter
-foo = long_function_name(var_one, var_two,
-                         var_three, var_four)
+foo = long_function_name(var_one=[var_two,  # comment
+                                  var_three
+                                  ],
+                         var_four=0,
+                         )
 "
    (should (eq (car (python-indent-context)) :no-indent))
    (should (= (python-indent-calculate-indentation) 0))
-   (python-tests-look-at "foo = long_function_name(var_one, var_two,")
+   (python-tests-look-at "foo = long_function_name(var_one")
    (should (eq (car (python-indent-context)) :after-comment))
    (should (= (python-indent-calculate-indentation) 0))
-   (python-tests-look-at "var_three, var_four)")
+   (python-tests-look-at "var_three")
    (should (eq (car (python-indent-context)) :inside-paren))
+   (should (= (python-indent-calculate-indentation) 34))
+   (python-tests-look-at "]")
+   (should (eq (car (python-indent-context))
:inside-paren-at-closing-nested-paren))
+   (should (= (python-indent-calculate-indentation) 34))
+   (python-tests-look-at "var_four")
+   (should (eq (car (python-indent-context)) :inside-paren))
+   (should (= (python-indent-calculate-indentation) 25))
+   (python-tests-look-at ")")
+   (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
    (should (= (python-indent-calculate-indentation) 25))))

 (ert-deftest python-indent-pep8-2 ()
@@ -215,20 +227,32 @@ def long_function_name(
   (python-tests-with-temp-buffer
    "# Extra indentation is not necessary.
 foo = long_function_name(
-  var_one, var_two,
-  var_three, var_four)
+    var_one, var_two=[  # comment
+        var_three,
+    ],
+    var_four=0,
+)
 "
    (should (eq (car (python-indent-context)) :no-indent))
    (should (= (python-indent-calculate-indentation) 0))
    (python-tests-look-at "foo = long_function_name(")
    (should (eq (car (python-indent-context)) :after-comment))
    (should (= (python-indent-calculate-indentation) 0))
-   (python-tests-look-at "var_one, var_two,")
+   (python-tests-look-at "var_one")
    (should (eq (car (python-indent-context)) :inside-paren-newline-start))
    (should (= (python-indent-calculate-indentation) 4))
-   (python-tests-look-at "var_three, var_four)")
+   (python-tests-look-at "var_three")
    (should (eq (car (python-indent-context)) :inside-paren-newline-start))
-   (should (= (python-indent-calculate-indentation) 4))))
+   (should (= (python-indent-calculate-indentation) 8))
+   (python-tests-look-at "]")
+   (should (eq (car (python-indent-context))
:inside-paren-at-closing-nested-paren))
+   (should (= (python-indent-calculate-indentation) 4))
+   (python-tests-look-at "var_four")
+   (should (eq (car (python-indent-context)) :inside-paren-newline-start))
+   (should (= (python-indent-calculate-indentation) 4))
+   (python-tests-look-at ")")
+   (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
+   (should (= (python-indent-calculate-indentation) 0))))

 (ert-deftest python-indent-base-case ()
   "Check base case does not trigger errors."
-- 
2.4.4