FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-104050: Add basic type hints to Argument Clinic clinic class by erlend-aasland · Pull Request #104705 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
55 changes: 38 additions & 17 deletions Tools/clinic/clinic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 = {}
Comment thread
erlend-aasland marked this conversation as 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 = {}
Comment thread
erlend-aasland marked this conversation as 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

Back | FazBrowse Home | New Git URL