Skip to contentSkip to conversation

userdiff: support Python async functions

PATCH Superseded Josh Holland 3 participants 4 messages Nov 11, 2019 – Nov 12, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 userdiff: support Python async functions

Python's async functions (declared with "async def" rather than "def") were not being displayed in hunk headers. This commit teaches git about the async function syntax, and adds tests for the Python userdiff regex.

---
 t/t4018/python-async-def | 4 ++++
 t/t4018/python-class     | 4 ++++
 t/t4018/python-def       | 4 ++++
 userdiff.c               | 2 +-
 4 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/python-async-def
 create mode 100644 t/t4018/python-class
 create mode 100644 t/t4018/python-def
t/t4018/python-async-def new file +4 -0
new file mode 100644
index 000000000..87640e03d
@@ -0,0 +1,4 @@
+async def RIGHT(pi: int = 3.14):
+    while True:
+        break
+    return ChangeMe()
t/t4018/python-class new file +4 -0
new file mode 100644
index 000000000..ba9e74143
@@ -0,0 +1,4 @@
+class RIGHT(int, str):
+    # comment
+    # another comment
+    # ChangeMe
t/t4018/python-def new file +4 -0
new file mode 100644
index 000000000..e50b31b0a
@@ -0,0 +1,4 @@
+def RIGHT(pi: int = 3.14):
+    while True:
+        break
+    return ChangeMe()
userdiff.c +1 -1
index e74a6d402..057fdcc55 100644
@@ -124,7 +124,7 @@ PATTERNS("php",
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
 	 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
-PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
+PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Signature · 1 line
2.24.0
#1 lore raw
Quoted 5 lines · Josh Holland

Python's async functions (declared with "async def" rather than "def") were not being displayed in hunk headers. This commit teaches git about the async function syntax, and adds tests for the Python userdiff regex.

Quoted 9 lines
---
 t/t4018/python-async-def | 4 ++++
 t/t4018/python-class     | 4 ++++
 t/t4018/python-def       | 4 ++++
 userdiff.c               | 2 +-
 4 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/python-async-def
 create mode 100644 t/t4018/python-class
 create mode 100644 t/t4018/python-def

It seems that there were no test patterns for Python ;-) The change to userdiff.c part (i.e. "where we used to expect 'def', we now allow it to be prefixed with an optional 'async' plus whitespace") makes sense.

Thanks, will queue.

#2 lore raw
In reply to Josh Holland

Am 11.11.19 um 02:01 schrieb Josh Holland:

Quoted 41 lines

Python's async functions (declared with "async def" rather than "def") were not being displayed in hunk headers. This commit teaches git about the async function syntax, and adds tests for the Python userdiff regex.

---
 t/t4018/python-async-def | 4 ++++
 t/t4018/python-class     | 4 ++++
 t/t4018/python-def       | 4 ++++
 userdiff.c               | 2 +-
 4 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 t/t4018/python-async-def
 create mode 100644 t/t4018/python-class
 create mode 100644 t/t4018/python-def
t/t4018/python-async-def new file +4 -0
new file mode 100644
index 000000000..87640e03d
@@ -0,0 +1,4 @@
+async def RIGHT(pi: int = 3.14):
+    while True:
+        break
+    return ChangeMe()
t/t4018/python-class new file +4 -0
new file mode 100644
index 000000000..ba9e74143
@@ -0,0 +1,4 @@
+class RIGHT(int, str):
+    # comment
+    # another comment
+    # ChangeMe
t/t4018/python-def new file +0 -0
new file mode 100644
index 000000000..e50b31b0a

t/t4018/python-def line 4

Quoted 4 lines
t/t4018/python-def new file +4 -0
@@ -0,0 +1,4 @@
+def RIGHT(pi: int = 3.14):
+    while True:
+        break
+    return ChangeMe()

Thank you for providing test cases for Python.

I have one gripe with this set of test, though: They do not demonstrate that the Python-specific pattern is better than the default pattern. In fact, if you remove the Python patterns from userdiff.c, you will observe that these tests still pass.

The one thing that the language specific pattern would do better than the default is that it picks up indented text. Could we have a test case or two that show that it indeed does?

Quoted 6 lines
userdiff.c +0 -0
index e74a6d402..057fdcc55 100644

userdiff.c PATTERNS("php", · line 130

Quoted 8 lines
userdiff.c +1 -1
@@ -124,7 +124,7 @@ PATTERNS("php",
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
 	 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
-PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
+PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
 	 /* -- */
 	 "[a-zA-Z_][a-zA-Z0-9_]*"
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"

-- Hannes

#3 lore raw
Quoted 9 lines · Johannes Sixt

t/t4018/python-def line 4

Quoted 6 lines

...

t/t4018/python-def new file +4 -0
@@ -0,0 +1,4 @@
+def RIGHT(pi: int = 3.14):
+    while True:
+        break
+    return ChangeMe()

Thank you for providing test cases for Python.

Quoted 8 lines

I have one gripe with this set of test, though: They do not demonstrate that the Python-specific pattern is better than the default pattern. In fact, if you remove the Python patterns from userdiff.c, you will observe that these tests still pass.

The one thing that the language specific pattern would do better than the default is that it picks up indented text. Could we have a test case or two that show that it indeed does?

Good point. A method "def" inside a "class" would be one level indented; I am not sure if it is a kosher style to have "if" at the unindented top-level, whose body conditionally defines a "class", but such a class definition would also be indented, I guess.

Thanks.