]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix Python font lock of chained assignment statement
authorkobarity <kobarity@gmail.com>
Fri, 24 May 2024 13:50:12 +0000 (22:50 +0900)
committerEshel Yaron <me@eshelyaron.com>
Sun, 26 May 2024 05:51:00 +0000 (07:51 +0200)
* lisp/progmodes/python.el
(python-font-lock-keywords-maximum-decoration): Allow chaining
of single assignment statements.
* test/lisp/progmodes/python-tests.el
(python-font-lock-assignment-statement-20): New test.
(Bug#71093)

(cherry picked from commit 3647645e94856344696768bef590b49a24c7ac52)

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index 360936c9efdf55a696c52a5bc43f80be29dc8012..79e383a1c1a018ecf9ea6347997efa4a03d06b7c 100644 (file)
@@ -845,6 +845,17 @@ sign in chained assignment."
         (match-beginning 2))            ; limit the search until the assignment
       nil
       (1 font-lock-variable-name-face)))
+    ;; single assignment with type hints, e.g.
+    ;;   a: int = 5
+    ;;   b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo')
+    ;;   c: Collection = {1, 2, 3}
+    ;;   d: Mapping[int, str] = {1: 'bar', 2: 'baz'}
+    (,(python-font-lock-assignment-matcher
+       (python-rx grouped-assignment-target (* space)
+                  (? ?: (* space) (+ not-simple-operator) (* space))
+                  (group assignment-operator)))
+     (1 font-lock-variable-name-face)
+     (2 'font-lock-operator-face))
     ;; special cases
     ;;   (a) = 5
     ;;   [a] = 5,
index b19c5c31f16e7fc493ab31ce6a389598a0fb1c44..0aee5a2212bf0bd99bb00ebce02659b00875e592 100644 (file)
@@ -491,38 +491,15 @@ def f(x: CustomInt) -> CustomInt:
      (136 . font-lock-operator-face) (137)
      (144 . font-lock-keyword-face) (150))))
 
-(ert-deftest python-font-lock-assignment-statement-19 ()
+(ert-deftest python-font-lock-assignment-statement-20 ()
   (python-tests-assert-faces
-   "a: List[List[CustomInt], List[CustomInt]] = []"
+   "a = b = c = 1"
    '((1 . font-lock-variable-name-face) (2)
-     (4 . font-lock-type-face) (8)
-     (9 . font-lock-type-face) (13)
-     (14 . font-lock-type-face) (23)
-     (26 . font-lock-type-face) (30)
-     (31 . font-lock-type-face) (40)
-     (43 . font-lock-operator-face) (44))))
-
-(ert-deftest python-font-lock-operator-1 ()
-  (python-tests-assert-faces
-   "1 << 2 ** 3 == +4%-5|~6&7^8%9"
-   '((1)
-     (3 . font-lock-operator-face) (5)
-     (8 . font-lock-operator-face) (10)
-     (13 . font-lock-operator-face) (15)
-     (16 . font-lock-operator-face) (17)
-     (18 . font-lock-operator-face) (20)
-     (21 . font-lock-operator-face) (23)
-     (24 . font-lock-operator-face) (25)
-     (26 . font-lock-operator-face) (27)
-     (28 . font-lock-operator-face) (29))))
-
-(ert-deftest python-font-lock-operator-2 ()
-  "Keyword operators are font-locked as keywords."
-  (python-tests-assert-faces
-   "is_ is None"
-   '((1)
-     (5 . font-lock-keyword-face) (7)
-     (8 . font-lock-constant-face))))
+     (3 . font-lock-operator-face) (4)
+     (5 . font-lock-variable-name-face) (6)
+     (7 . font-lock-operator-face) (8)
+     (9 . font-lock-variable-name-face) (10)
+     (11 . font-lock-operator-face) (12))))
 
 (ert-deftest python-font-lock-escape-sequence-string-newline ()
   (python-tests-assert-faces