]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104683: Argument Clinic: Params now render their own docstrings (#107790)
authorErlend E. Aasland <erlend@python.org>
Wed, 9 Aug 2023 12:44:26 +0000 (14:44 +0200)
committerGitHub <noreply@github.com>
Wed, 9 Aug 2023 12:44:26 +0000 (12:44 +0000)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/test/test_clinic.py
Tools/clinic/clinic.py

index 026cd18d646ad7f5e7a6134fcd894f6482853697..3921523af067f8f4e50edfb188dcf4f4300a91f3 100644 (file)
@@ -833,8 +833,8 @@ class ClinicParserTest(TestCase):
 
     def checkDocstring(self, fn, expected):
         self.assertTrue(hasattr(fn, "docstring"))
-        self.assertEqual(fn.docstring.strip(),
-                         dedent(expected).strip())
+        self.assertEqual(dedent(expected).strip(),
+                         fn.docstring.strip())
 
     def test_trivial(self):
         parser = DSLParser(_make_clinic())
@@ -997,8 +997,12 @@ class ClinicParserTest(TestCase):
 
                path: str
                    Path to be examined
+                   Ensure that multiple lines are indented correctly.
 
             Perform a stat system call on the given path.
+
+            Ensure that multiple lines are indented correctly.
+            Ensure that multiple lines are indented correctly.
         """)
         self.checkDocstring(function, """
             stat($module, /, path)
@@ -1008,6 +1012,10 @@ class ClinicParserTest(TestCase):
 
               path
                 Path to be examined
+                Ensure that multiple lines are indented correctly.
+
+            Ensure that multiple lines are indented correctly.
+            Ensure that multiple lines are indented correctly.
         """)
 
     def test_docstring_trailing_whitespace(self):
index 059c2db27288dbbbd62d0304b8eaed668e0237a2..3490d4871b891266547aa80908d5ac31b5a4194f 100755 (executable)
@@ -2775,6 +2775,13 @@ class Parameter:
         else:
             return f'"argument {i}"'
 
+    def render_docstring(self) -> str:
+        add, out = text_accumulator()
+        add(f"  {self.name}\n")
+        for line in self.docstring.split("\n"):
+            add(f"    {line}\n")
+        return out().rstrip()
+
 
 CConverterClassT = TypeVar("CConverterClassT", bound=type["CConverter"])
 
@@ -5686,23 +5693,11 @@ class DSLParser:
     @staticmethod
     def format_docstring_parameters(params: list[Parameter]) -> str:
         """Create substitution text for {parameters}"""
-        text, add, output = _text_accumulator()
-        spacer_line = False
-        for param in params:
-            docstring = param.docstring.strip()
-            if not docstring:
-                continue
-            if spacer_line:
+        add, output = text_accumulator()
+        for p in params:
+            if p.docstring:
+                add(p.render_docstring())
                 add('\n')
-            else:
-                spacer_line = True
-            add("  ")
-            add(param.name)
-            add('\n')
-            stripped = rstrip_lines(docstring)
-            add(textwrap.indent(stripped, "    "))
-        if text:
-            add('\n')
         return output()
 
     def format_docstring(self) -> str: