Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,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:

Expand Down Expand Up @@ -2044,23 +2049,30 @@ class Clinic:

"""

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 = {}
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
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 = {}
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
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")
Expand All @@ -2081,10 +2093,13 @@ def __init__(self, language, printer=None, *, verify=True, filename=None):
'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()
Expand Down Expand Up @@ -2112,18 +2127,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]

Expand Down Expand Up @@ -2249,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)

Expand Down Expand Up @@ -2284,8 +2309,6 @@ def parse(self, block: Block) -> None:
block.output = s.getvalue()


ModuleDict = dict[str, "Module"]

class Module:
def __init__(
self,
Expand All @@ -2303,8 +2326,6 @@ def __repr__(self) -> str:
return "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">"


ClassDict = dict[str, "Class"]

class Class:
def __init__(
self,
Expand Down