Skip to content

Commit 383dcbe

Browse files
gh-104050: Argument Clinic: Annotate Clinic.parse() (#106760)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent c413207 commit 383dcbe

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Diff for: Tools/clinic/clinic.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
Literal,
4343
NamedTuple,
4444
NoReturn,
45+
Protocol,
4546
TypeGuard,
4647
overload,
4748
)
@@ -2055,7 +2056,12 @@ def write_file(filename: str, new_contents: str) -> None:
20552056
ClassDict = dict[str, "Class"]
20562057
DestinationDict = dict[str, Destination]
20572058
ModuleDict = dict[str, "Module"]
2058-
ParserDict = dict[str, "DSLParser"]
2059+
2060+
2061+
class Parser(Protocol):
2062+
def __init__(self, clinic: Clinic) -> None: ...
2063+
def parse(self, block: Block) -> None: ...
2064+
20592065

20602066
clinic = None
20612067
class Clinic:
@@ -2113,7 +2119,7 @@ def __init__(
21132119
) -> None:
21142120
# maps strings to Parser objects.
21152121
# (instantiated from the "parsers" global.)
2116-
self.parsers: ParserDict = {}
2122+
self.parsers: dict[str, Parser] = {}
21172123
self.language: CLanguage = language
21182124
if printer:
21192125
fail("Custom printers are broken right now")
@@ -2205,7 +2211,7 @@ def get_destination_buffer(
22052211
d = self.get_destination(name)
22062212
return d.buffers[item]
22072213

2208-
def parse(self, input):
2214+
def parse(self, input: str) -> str:
22092215
printer = self.printer
22102216
self.block_parser = BlockParser(input, self.language, verify=self.verify)
22112217
for block in self.block_parser:
@@ -5521,7 +5527,10 @@ def state_terminal(self, line):
55215527
# "clinic", handles the Clinic DSL
55225528
# "python", handles running Python code
55235529
#
5524-
parsers = {'clinic' : DSLParser, 'python': PythonParser}
5530+
parsers: dict[str, Callable[[Clinic], Parser]] = {
5531+
'clinic': DSLParser,
5532+
'python': PythonParser,
5533+
}
55255534

55265535

55275536
clinic = None

0 commit comments

Comments
 (0)