[Python-checkins] gh-104050: Add basic type hints to Argument Clinic clinic class (#104705)

erlend-aasland webhook-mailer at python.org
Sun May 21 16:49:40 EDT 2023


https://github.com/python/cpython/commit/bf6dd8f5fd98a1833646eb22269cad076836ebba
commit: bf6dd8f5fd98a1833646eb22269cad076836ebba
branch: main
author: Erlend E. Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-05-21T20:49:34Z
summary:

gh-104050: Add basic type hints to Argument Clinic clinic class (#104705)

Co-authored-by: Alex Waygood <Alex.Waygood at Gmail.com>

files:
M Tools/clinic/clinic.py

diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index a1e8947529a0..513586ee0191 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -1989,6 +1989,11 @@ def write_file(filename: str, new_contents: str):
         raise
 
 
+ClassDict = dict[str, "Class"]
+DestinationDict = dict[str, Destination]
+ModuleDict = dict[str, "Module"]
+ParserDict = dict[str, "DSLParser"]
+
 clinic = None
 class Clinic:
 
@@ -2035,23 +2040,30 @@ class Clinic:
 
 """
 
-    def __init__(self, language, printer=None, *, verify=True, filename=None):
+    def __init__(
+            self,
+            language: CLanguage,
+            printer: BlockPrinter | None = None,
+            *,
+            verify: bool = True,
+            filename: str | None = None
+    ) -> None:
         # maps strings to Parser objects.
         # (instantiated from the "parsers" global.)
-        self.parsers = {}
-        self.language = language
+        self.parsers: ParserDict = {}
+        self.language: CLanguage = language
         if printer:
             fail("Custom printers are broken right now")
         self.printer = printer or BlockPrinter(language)
         self.verify = verify
         self.filename = filename
-        self.modules = {}
-        self.classes = {}
-        self.functions = []
+        self.modules: ModuleDict = {}
+        self.classes: ClassDict = {}
+        self.functions: list[Function] = []
 
         self.line_prefix = self.line_suffix = ''
 
-        self.destinations = {}
+        self.destinations: DestinationDict = {}
         self.add_destination("block", "buffer")
         self.add_destination("suppress", "suppress")
         self.add_destination("buffer", "buffer")
@@ -2072,10 +2084,13 @@ def __init__(self, language, printer=None, *, verify=True, filename=None):
             'impl_definition': d('block'),
         }
 
-        self.destination_buffers_stack = []
-        self.ifndef_symbols = set()
+        DestBufferType = dict[str, Callable[..., Any]]
+        DestBufferList = list[DestBufferType]
+
+        self.destination_buffers_stack: DestBufferList = []
+        self.ifndef_symbols: set[str] = set()
 
-        self.presets = {}
+        self.presets: dict[str, dict[Any, Any]] = {}
         preset = None
         for line in self.presets_text.strip().split('\n'):
             line = line.strip()
@@ -2103,18 +2118,27 @@ def __init__(self, language, printer=None, *, verify=True, filename=None):
         global clinic
         clinic = self
 
-    def add_destination(self, name, type, *args):
+    def add_destination(
+            self,
+            name: str,
+            type: str,
+            *args
+    ) -> None:
         if name in self.destinations:
             fail("Destination already exists: " + repr(name))
         self.destinations[name] = Destination(name, type, self, *args)
 
-    def get_destination(self, name):
+    def get_destination(self, name: str) -> Destination:
         d = self.destinations.get(name)
         if not d:
             fail("Destination does not exist: " + repr(name))
         return d
 
-    def get_destination_buffer(self, name, item=0):
+    def get_destination_buffer(
+            self,
+            name: str,
+            item: int = 0
+    ):
         d = self.get_destination(name)
         return d.buffers[item]
 
@@ -2240,6 +2264,7 @@ def parse_file(
     if not find_start_re.search(raw):
         return
 
+    assert isinstance(language, CLanguage)
     clinic = Clinic(language, verify=verify, filename=filename)
     src_out, clinic_out = clinic.parse(raw)
 
@@ -2275,8 +2300,6 @@ def parse(self, block: Block) -> None:
         block.output = s.getvalue()
 
 
-ModuleDict = dict[str, "Module"]
-
 class Module:
     def __init__(
             self,
@@ -2294,8 +2317,6 @@ def __repr__(self) -> str:
         return "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">"
 
 
-ClassDict = dict[str, "Class"]
-
 class Class:
     def __init__(
             self,



More information about the Python-checkins mailing list