From 0f5317b5511e5b1a0e7e2a1c3e02929833b1b8fc Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Jul 2023 21:25:08 +0200 Subject: [PATCH 1/5] Annotate BlockParser --- Tools/clinic/clinic.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 726ebc04f55bd5..e3c30ee4193464 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1712,7 +1712,13 @@ class BlockParser: Iterator, yields Block objects. """ - def __init__(self, input, language, *, verify=True): + def __init__( + self, + input: str, + language: Language, + *, + verify: bool = True + ) -> None: """ "input" should be a str object with embedded \n characters. @@ -1738,7 +1744,7 @@ def __init__(self, input, language, *, verify=True): def __iter__(self): return self - def __next__(self): + def __next__(self) -> Block: while True: if not self.input: raise StopIteration @@ -1755,18 +1761,18 @@ def __next__(self): return block - def is_start_line(self, line): + def is_start_line(self, line: str) -> bool: match = self.start_re.match(line.lstrip()) return match.group(1) if match else None - def _line(self, lookahead=False): + def _line(self, lookahead: bool = False) -> str: self.line_number += 1 line = self.input.pop() if not lookahead: self.language.parse_line(line) return line - def parse_verbatim_block(self): + def parse_verbatim_block(self) -> Block: add, output = text_accumulator() self.block_start_line_number = self.line_number @@ -1780,13 +1786,13 @@ def parse_verbatim_block(self): return Block(output()) - def parse_clinic_block(self, dsl_name): + def parse_clinic_block(self, dsl_name: str) -> Block: input_add, input_output = text_accumulator() self.block_start_line_number = self.line_number + 1 stop_line = self.language.stop_line.format(dsl_name=dsl_name) body_prefix = self.language.body_prefix.format(dsl_name=dsl_name) - def is_stop_line(line): + def is_stop_line(line: str) -> bool: # make sure to recognize stop line even if it # doesn't end with EOL (it could be the very end of the file) if line.startswith(stop_line): From 019edbf256ebce5b2ed414d0a072194afec3ffb0 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Jul 2023 21:34:55 +0200 Subject: [PATCH 2/5] Fix warnings --- Tools/clinic/clinic.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index e3c30ee4193464..55001a1318bc09 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1736,9 +1736,9 @@ def __init__( self.find_start_re = create_regex(before, after, whole_line=False) self.start_re = create_regex(before, after) self.verify = verify - self.last_checksum_re = None - self.last_dsl_name = None - self.dsl_name = None + self.last_checksum_re: Any = None + self.last_dsl_name: str | None = None + self.dsl_name: str | None = None self.first_block = True def __iter__(self): @@ -1761,7 +1761,7 @@ def __next__(self) -> Block: return block - def is_start_line(self, line: str) -> bool: + def is_start_line(self, line: str) -> str | None: match = self.start_re.match(line.lstrip()) return match.group(1) if match else None @@ -1840,7 +1840,8 @@ def is_stop_line(line: str) -> bool: if self.is_start_line(line): break - output = output_output() + output: str | None = output_output() + assert isinstance(output, str) if arguments: d = {} for field in shlex.split(arguments): From 0507ebb7d5b97dfe0f4fee906abbaa174bc73207 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Jul 2023 21:40:43 +0200 Subject: [PATCH 3/5] Better type hint for the regex --- Tools/clinic/clinic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 55001a1318bc09..bd1ef3b725a0d9 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1736,7 +1736,7 @@ def __init__( self.find_start_re = create_regex(before, after, whole_line=False) self.start_re = create_regex(before, after) self.verify = verify - self.last_checksum_re: Any = None + self.last_checksum_re: re.Pattern[str] | None = None self.last_dsl_name: str | None = None self.dsl_name: str | None = None self.first_block = True @@ -1826,6 +1826,7 @@ def is_stop_line(line: str) -> bool: checksum_re = create_regex(before, after, word=False) self.last_dsl_name = dsl_name self.last_checksum_re = checksum_re + assert checksum_re is not None # scan forward for checksum line output_add, output_output = text_accumulator() From e61d231f5b6f92cbffa20d0a125a52e307f3ae9b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Jul 2023 21:42:12 +0200 Subject: [PATCH 4/5] Annotate __iter__ --- 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 bd1ef3b725a0d9..4396d9a8ee53fc 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1741,7 +1741,7 @@ def __init__( self.dsl_name: str | None = None self.first_block = True - def __iter__(self): + def __iter__(self) -> BlockParser: return self def __next__(self) -> Block: From 924f2cf4cf8084d08141ebcd032a488a985c5fad Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Jul 2023 23:51:54 +0200 Subject: [PATCH 5/5] Address review --- Tools/clinic/clinic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 4396d9a8ee53fc..861a6507eae753 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1841,8 +1841,8 @@ def is_stop_line(line: str) -> bool: if self.is_start_line(line): break - output: str | None = output_output() - assert isinstance(output, str) + output: str | None + output = output_output() if arguments: d = {} for field in shlex.split(arguments):