From bf6dd8f5fd98a1833646eb22269cad076836ebba Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 21 May 2023 22:49:34 +0200 Subject: gh-104050: Add basic type hints to Argument Clinic clinic class (#104705) Co-authored-by: Alex Waygood --- Tools/clinic/clinic.py | 55 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a1e8947..513586e 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 @@ impl_definition block """ - 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 @@ impl_definition block '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 @@ impl_definition block 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 @@ class PythonParser: block.output = s.getvalue() -ModuleDict = dict[str, "Module"] - class Module: def __init__( self, @@ -2294,8 +2317,6 @@ class Module: return "" -ClassDict = dict[str, "Class"] - class Class: def __init__( self, -- cgit v0.12