Skip to content

Commit 569887a

Browse files
gh-104050: Add more type hints to Argument Clinic DSLParser() (#106343)
Annotate the following method signatures: - state_dsl_start() - state_parameter_docstring_start() - state_parameters_start() Inverting ignore_line() logic, add type hints (including type guard) to it, and rename to valid_line().
1 parent 4e3aa7c commit 569887a

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Tools/clinic/clinic.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929

3030
from collections.abc import Callable
3131
from types import FunctionType, NoneType
32-
from typing import Any, Final, NamedTuple, NoReturn, Literal, overload
32+
from typing import (
33+
Any,
34+
Final,
35+
Literal,
36+
NamedTuple,
37+
NoReturn,
38+
TypeGuard,
39+
overload,
40+
)
3341

3442
# TODO:
3543
#
@@ -4471,17 +4479,20 @@ def parse(self, block: Block) -> None:
44714479
block.output = self.saved_output
44724480

44734481
@staticmethod
4474-
def ignore_line(line):
4482+
def valid_line(line: str | None) -> TypeGuard[str]:
4483+
if line is None:
4484+
return False
4485+
44754486
# ignore comment-only lines
44764487
if line.lstrip().startswith('#'):
4477-
return True
4488+
return False
44784489

44794490
# Ignore empty lines too
44804491
# (but not in docstring sections!)
44814492
if not line.strip():
4482-
return True
4493+
return False
44834494

4484-
return False
4495+
return True
44854496

44864497
@staticmethod
44874498
def calculate_indent(line: str) -> int:
@@ -4497,9 +4508,9 @@ def next(
44974508
if line is not None:
44984509
self.state(line)
44994510

4500-
def state_dsl_start(self, line):
4511+
def state_dsl_start(self, line: str | None) -> None:
45014512
# self.block = self.ClinicOutputBlock(self)
4502-
if self.ignore_line(line):
4513+
if not self.valid_line(line):
45034514
return
45044515

45054516
# is it a directive?
@@ -4716,8 +4727,8 @@ def state_modulename_name(self, line):
47164727
ps_start, ps_left_square_before, ps_group_before, ps_required, \
47174728
ps_optional, ps_group_after, ps_right_square_after = range(7)
47184729

4719-
def state_parameters_start(self, line):
4720-
if self.ignore_line(line):
4730+
def state_parameters_start(self, line: str) -> None:
4731+
if not self.valid_line(line):
47214732
return
47224733

47234734
# if this line is not indented, we have no parameters
@@ -4742,7 +4753,7 @@ def state_parameter(self, line):
47424753
line = self.parameter_continuation + ' ' + line.lstrip()
47434754
self.parameter_continuation = ''
47444755

4745-
if self.ignore_line(line):
4756+
if not self.valid_line(line):
47464757
return
47474758

47484759
assert self.indent.depth == 2
@@ -5075,7 +5086,7 @@ def parse_special_symbol(self, symbol):
50755086
fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.")
50765087
p.kind = inspect.Parameter.POSITIONAL_ONLY
50775088

5078-
def state_parameter_docstring_start(self, line):
5089+
def state_parameter_docstring_start(self, line: str) -> None:
50795090
self.parameter_docstring_indent = len(self.indent.margin)
50805091
assert self.indent.depth == 3
50815092
return self.next(self.state_parameter_docstring, line)

0 commit comments

Comments
 (0)