From dd434fb32b7f419dd0abca655fa5afb8a20a9b1e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 15 Jul 2023 00:13:03 +0200 Subject: [PATCH 1/5] gh-104050: Argument Clinic: Annotate Clinic.parse() As a consequence, also add a generic Parser ABC --- Tools/clinic/clinic.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 726ebc04f55bd5..f38e68158e5a62 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2044,10 +2044,16 @@ def write_file(filename: str, new_contents: str) -> None: raise +class Parser: + + @abc.abstractmethod + def parse(self, block: Block) -> None: ... + + ClassDict = dict[str, "Class"] DestinationDict = dict[str, Destination] ModuleDict = dict[str, "Module"] -ParserDict = dict[str, "DSLParser"] +ParserDict = dict[str, Parser] clinic = None class Clinic: @@ -2197,7 +2203,7 @@ def get_destination_buffer( 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: @@ -2333,7 +2339,7 @@ def compute_checksum( return s -class PythonParser: +class PythonParser(Parser): def __init__(self, clinic: Clinic) -> None: pass @@ -4353,7 +4359,7 @@ class ParamState(enum.IntEnum): RIGHT_SQUARE_AFTER = 6 -class DSLParser: +class DSLParser(Parser): function: Function | None state: StateKeeper keyword_only: bool From edcd854be4af147812cd5ace947f9ba490742c40 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 15 Jul 2023 21:42:18 +0200 Subject: [PATCH 2/5] Make it protocol! --- Tools/clinic/clinic.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a642b00e8dc48b..66ff23ca2a3082 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -42,6 +42,7 @@ Literal, NamedTuple, NoReturn, + Protocol, TypeGuard, overload, ) @@ -2052,16 +2053,19 @@ def write_file(filename: str, new_contents: str) -> None: raise -class Parser: +ClassDict = dict[str, "Class"] +DestinationDict = dict[str, Destination] +ModuleDict = dict[str, "Module"] + + +class Parser(Protocol): @abc.abstractmethod - def parse(self, block: Block) -> None: ... + def __init__(self, *args, **kwargs): ... + @abc.abstractmethod + def parse(self, block: Block) -> None: ... -ClassDict = dict[str, "Class"] -DestinationDict = dict[str, Destination] -ModuleDict = dict[str, "Module"] -ParserDict = dict[str, Parser] clinic = None class Clinic: @@ -2119,7 +2123,7 @@ def __init__( ) -> 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") @@ -2347,7 +2351,7 @@ def compute_checksum( return s -class PythonParser(Parser): +class PythonParser: def __init__(self, clinic: Clinic) -> None: pass @@ -4367,7 +4371,7 @@ class ParamState(enum.IntEnum): RIGHT_SQUARE_AFTER = 6 -class DSLParser(Parser): +class DSLParser: function: Function | None state: StateKeeper keyword_only: bool @@ -5527,7 +5531,7 @@ def state_terminal(self, line): # "clinic", handles the Clinic DSL # "python", handles running Python code # -parsers = {'clinic' : DSLParser, 'python': PythonParser} +parsers: dict[str, type[Parser]] = {'clinic' : DSLParser, 'python': PythonParser} clinic = None From 57ed0a13edf771a7920f44b254257680abaa2bc6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 15 Jul 2023 21:54:24 +0200 Subject: [PATCH 3/5] Readability --- Tools/clinic/clinic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 66ff23ca2a3082..bbd3e304d8da20 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -5531,7 +5531,10 @@ def state_terminal(self, line): # "clinic", handles the Clinic DSL # "python", handles running Python code # -parsers: dict[str, type[Parser]] = {'clinic' : DSLParser, 'python': PythonParser} +parsers: dict[str, type[Parser]] = { + 'clinic': DSLParser, + 'python': PythonParser, +} clinic = None From 2c9075da040dc298791b28fa87480c3de4f2a07c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 17 Jul 2023 01:25:09 +0200 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Alex Waygood --- Tools/clinic/clinic.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index bbd3e304d8da20..d6f74cffa17232 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2059,11 +2059,7 @@ def write_file(filename: str, new_contents: str) -> None: class Parser(Protocol): - - @abc.abstractmethod - def __init__(self, *args, **kwargs): ... - - @abc.abstractmethod + def __init__(self, clinic: Clinic) -> None: ... def parse(self, block: Block) -> None: ... From 96dae0c37eddbded55601e79dfd49e81ffa7ccf3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 17 Jul 2023 01:26:52 +0200 Subject: [PATCH 5/5] Update Tools/clinic/clinic.py Co-authored-by: Alex Waygood --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index d6f74cffa17232..9b7069e9b8fcb0 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -5527,7 +5527,7 @@ def state_terminal(self, line): # "clinic", handles the Clinic DSL # "python", handles running Python code # -parsers: dict[str, type[Parser]] = { +parsers: dict[str, Callable[[Clinic], Parser]] = { 'clinic': DSLParser, 'python': PythonParser, }