]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104050: Argument Clinic: Annotate Clinic.parse() (#106760)
authorErlend E. Aasland <erlend@python.org>
Mon, 17 Jul 2023 00:04:10 +0000 (02:04 +0200)
committerGitHub <noreply@github.com>
Mon, 17 Jul 2023 00:04:10 +0000 (00:04 +0000)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Tools/clinic/clinic.py

index 861a6507eae753c9d723a6982556af7c20aa92f2..9b7069e9b8fcb0279347436600f9326d3f19f4c9 100755 (executable)
@@ -42,6 +42,7 @@ from typing import (
     Literal,
     NamedTuple,
     NoReturn,
+    Protocol,
     TypeGuard,
     overload,
 )
@@ -2055,7 +2056,12 @@ def write_file(filename: str, new_contents: str) -> None:
 ClassDict = dict[str, "Class"]
 DestinationDict = dict[str, Destination]
 ModuleDict = dict[str, "Module"]
-ParserDict = dict[str, "DSLParser"]
+
+
+class Parser(Protocol):
+    def __init__(self, clinic: Clinic) -> None: ...
+    def parse(self, block: Block) -> None: ...
+
 
 clinic = None
 class Clinic:
@@ -2113,7 +2119,7 @@ impl_definition block
     ) -> None:
         # maps strings to Parser objects.
         # (instantiated from the "parsers" global.)
-        self.parsers: ParserDict = {}
+        self.parsers: dict[str, Parser] = {}
         self.language: CLanguage = language
         if printer:
             fail("Custom printers are broken right now")
@@ -2205,7 +2211,7 @@ impl_definition block
         d = self.get_destination(name)
         return d.buffers[item]
 
-    def parse(self, input):
+    def parse(self, input: str) -> str:
         printer = self.printer
         self.block_parser = BlockParser(input, self.language, verify=self.verify)
         for block in self.block_parser:
@@ -5521,7 +5527,10 @@ class DSLParser:
 #   "clinic", handles the Clinic DSL
 #   "python", handles running Python code
 #
-parsers = {'clinic' : DSLParser, 'python': PythonParser}
+parsers: dict[str, Callable[[Clinic], Parser]] = {
+    'clinic': DSLParser,
+    'python': PythonParser,
+}
 
 
 clinic = None