From 5377067ff9f141df4403327b4702bd23b6a3410a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 20 May 2023 23:33:53 +0200 Subject: [PATCH 1/3] gh-104050: Add basic type hints to Argument Clinic clinic class --- Tools/clinic/clinic.py | 46 ++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 41e08d15436b11..92263265c081c5 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1913,6 +1913,8 @@ def dump(self): return "".join(texts) +DestinationDict = dict[str, "Destination"] + class Destination: def __init__(self, name, type, clinic, *args): self.name = name @@ -2044,23 +2046,30 @@ class Clinic: """ - def __init__(self, language, printer=None, *, verify=True, filename=None): + def __init__( + self, + language: Language, + 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: Language = 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") @@ -2081,10 +2090,10 @@ def __init__(self, language, printer=None, *, verify=True, filename=None): 'impl_definition': d('block'), } - self.destination_buffers_stack = [] - self.ifndef_symbols = set() + self.destination_buffers_stack: list[BufferSeries] = [] + self.ifndef_symbols: set[str] = set() - self.presets = {} + self.presets: dict[str, Any] = {} preset = None for line in self.presets_text.strip().split('\n'): line = line.strip() @@ -2112,18 +2121,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] @@ -4201,6 +4219,8 @@ def dedent(self, line): return line[indent:] +ParserDict = dict[str, "DSLParser"] + class DSLParser: def __init__(self, clinic): self.clinic = clinic From ddf973e6c6ef179f412806dc190b0b2f57edd1eb Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 21 May 2023 22:12:59 +0200 Subject: [PATCH 2/3] Address most of the review remarks --- Tools/clinic/clinic.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index db95ac10184ad5..0b753ba4a92efc 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1913,8 +1913,6 @@ def dump(self): return "".join(texts) -DestinationDict = dict[str, "Destination"] - class Destination: def __init__(self, name, type, clinic, *args): self.name = name @@ -2000,6 +1998,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: @@ -2090,10 +2093,13 @@ def __init__( 'impl_definition': d('block'), } - self.destination_buffers_stack: list[Any] = [] + DestBufferType = dict[str, Callable[..., Any]] + DestBufferList = list[DestBufferType] + + self.destination_buffers_stack: DestBufferList = [] self.ifndef_symbols: set[str] = set() - self.presets: dict[str, Any] = {} + self.presets: dict[str, dict[Any, Any]] = {} preset = None for line in self.presets_text.strip().split('\n'): line = line.strip() @@ -2302,8 +2308,6 @@ def parse(self, block: Block) -> None: block.output = s.getvalue() -ModuleDict = dict[str, "Module"] - class Module: def __init__( self, @@ -2321,8 +2325,6 @@ def __repr__(self) -> str: return "" -ClassDict = dict[str, "Class"] - class Class: def __init__( self, @@ -4219,7 +4221,6 @@ def dedent(self, line): return line[indent:] -ParserDict = dict[str, "DSLParser"] StateKeeper = Callable[[str | None], None] class DSLParser: From f84c3d312534f62471fec280770cddb63c2cb4d7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 21 May 2023 22:23:32 +0200 Subject: [PATCH 3/3] Clinic only accepts CLanguage --- Tools/clinic/clinic.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 0b753ba4a92efc..6b9d7b68209ce9 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2051,7 +2051,7 @@ class Clinic: def __init__( self, - language: Language, + language: CLanguage, printer: BlockPrinter | None = None, *, verify: bool = True, @@ -2060,7 +2060,7 @@ def __init__( # maps strings to Parser objects. # (instantiated from the "parsers" global.) self.parsers: ParserDict = {} - self.language: Language = language + self.language: CLanguage = language if printer: fail("Custom printers are broken right now") self.printer = printer or BlockPrinter(language) @@ -2273,6 +2273,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)