]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't misindent 'else:' after 'if re.match:' in Python
authorDmitry Gutov <dgutov@yandex.ru>
Wed, 8 Mar 2023 20:41:23 +0000 (22:41 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Wed, 8 Mar 2023 20:48:33 +0000 (22:48 +0200)
* lisp/progmodes/python.el
(python-info-dedenter-opening-block-positions): Check that the
supposed block start is not a method call (bug#62031).

* test/lisp/progmodes/python-tests.el (python-indent-after-re-match):
New test.

Co-authored-by: Lele Gaifax <lele@metapensiero.it>
lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index 1f970633bfcf9e9bc2458e3fbe81264e2541d7f2..81475f31f60612ff1e58fff94c83428e50dfed58 100644 (file)
@@ -5792,7 +5792,9 @@ likely an invalid python file."
           (catch 'exit
             (while (python-nav--syntactically
                     (lambda ()
-                      (re-search-backward (python-rx block-start) nil t))
+                      (cl-loop for pt = (re-search-backward (python-rx block-start) nil t)
+                               until (memq (char-before) '(nil ?\s ?\t ?\n))
+                               finally return pt))
                     #'<)
               (let ((indentation (current-indentation)))
                 (when (and (not (memq indentation collected-indentations))
index 4f24c042c6ad73d209c0a8429dd7509558d21b20..6928e313dc4918fae6d924186f49ae8e40e9886f 100644 (file)
@@ -1982,6 +1982,18 @@ match foo:
    (should (eq (car (python-indent-context)) :after-block-start))
    (should (= (python-indent-calculate-indentation) 8))))
 
+(ert-deftest python-indent-after-re-match ()
+  "Test BUG 62031 regression."
+  (python-tests-with-temp-buffer
+   "
+def test_re(string):
+    if re.match('^[a-c]+$', string):
+        print('yes')
+    else:
+    "
+   (python-tests-look-at "else:")
+   (should (= (python-indent-calculate-indentation) 4))))
+
 \f
 ;;; Filling