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

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Erlend E. Aasland 2023-05-21 22:49:34 +02:00 • committed by GitHub
parent c482e9a4e6
commit bf6dd8f5fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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.presets = {}
self.destination_buffers_stack: DestBufferList = []
self.ifndef_symbols: set[str] = set()
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 "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">"
ClassDict = dict[str, "Class"]
class Class:
def __init__(
self,