Skip to content

Commit b24479d

Browse files
gh-104050: Annotate more Argument Clinic DSLParser state methods (#106376)
Annotate the following methods: - state_parameter() - state_parameter_docstring_start() Co-authored-by: Alex Waygood <[email protected]>
1 parent 3ee8dac commit b24479d

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

Tools/clinic/clinic.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4781,14 +4781,16 @@ def to_required(self):
47814781
for p in self.function.parameters.values():
47824782
p.group = -p.group
47834783

4784-
def state_parameter(self, line):
4785-
if self.parameter_continuation:
4786-
line = self.parameter_continuation + ' ' + line.lstrip()
4787-
self.parameter_continuation = ''
4784+
def state_parameter(self, line: str | None) -> None:
4785+
assert isinstance(self.function, Function)
47884786

47894787
if not self.valid_line(line):
47904788
return
47914789

4790+
if self.parameter_continuation:
4791+
line = self.parameter_continuation + ' ' + line.lstrip()
4792+
self.parameter_continuation = ''
4793+
47924794
assert self.indent.depth == 2
47934795
indent = self.indent.infer(line)
47944796
if indent == -1:
@@ -4839,6 +4841,7 @@ def state_parameter(self, line):
48394841
fields[0] = name
48404842
line = ' '.join(fields)
48414843

4844+
default: str | None
48424845
base, equals, default = line.rpartition('=')
48434846
if not equals:
48444847
base = default
@@ -4861,7 +4864,9 @@ def state_parameter(self, line):
48614864
if not module:
48624865
fail("Function " + self.function.name + " has an invalid parameter declaration:\n\t" + line)
48634866

4864-
function_args = module.body[0].args
4867+
function = module.body[0]
4868+
assert isinstance(function, ast.FunctionDef)
4869+
function_args = function.args
48654870

48664871
if len(function_args.args) > 1:
48674872
fail("Function " + self.function.name + " has an invalid parameter declaration (comma?):\n\t" + line)
@@ -4884,6 +4889,7 @@ def state_parameter(self, line):
48844889
if self.parameter_state is ParamState.OPTIONAL:
48854890
fail(f"Can't have a parameter without a default ({parameter_name!r})\n"
48864891
"after a parameter with a default!")
4892+
value: Sentinels | Null
48874893
if is_vararg:
48884894
value = NULL
48894895
kwargs.setdefault('c_default', "NULL")
@@ -4946,8 +4952,11 @@ def bad_node(self, node):
49464952
if bad:
49474953
fail("Unsupported expression as default value: " + repr(default))
49484954

4949-
expr = module.body[0].value
4955+
assignment = module.body[0]
4956+
assert isinstance(assignment, ast.Assign)
4957+
expr = assignment.value
49504958
# mild hack: explicitly support NULL as a default value
4959+
c_default: str | None
49514960
if isinstance(expr, ast.Name) and expr.id == 'NULL':
49524961
value = NULL
49534962
py_default = '<unrepresentable>'
@@ -4964,7 +4973,7 @@ def bad_node(self, node):
49644973
value = unknown
49654974
elif isinstance(expr, ast.Attribute):
49664975
a = []
4967-
n = expr
4976+
n: ast.expr | ast.Attribute = expr
49684977
while isinstance(n, ast.Attribute):
49694978
a.append(n.attr)
49704979
n = n.value
@@ -4984,7 +4993,7 @@ def bad_node(self, node):
49844993
else:
49854994
value = ast.literal_eval(expr)
49864995
py_default = repr(value)
4987-
if isinstance(value, (bool, None.__class__)):
4996+
if isinstance(value, (bool, NoneType)):
49884997
c_default = "Py_" + py_default
49894998
elif isinstance(value, str):
49904999
c_default = c_repr(value)
@@ -5011,6 +5020,7 @@ def bad_node(self, node):
50115020
# but the parameter object gets the python name
50125021
converter = dict[name](c_name or parameter_name, parameter_name, self.function, value, **kwargs)
50135022

5023+
kind: inspect._ParameterKind
50145024
if is_vararg:
50155025
kind = inspect.Parameter.VAR_POSITIONAL
50165026
elif self.keyword_only:
@@ -5130,7 +5140,7 @@ def parse_special_symbol(self, symbol):
51305140
fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.")
51315141
p.kind = inspect.Parameter.POSITIONAL_ONLY
51325142

5133-
def state_parameter_docstring_start(self, line: str) -> None:
5143+
def state_parameter_docstring_start(self, line: str | None) -> None:
51345144
self.parameter_docstring_indent = len(self.indent.margin)
51355145
assert self.indent.depth == 3
51365146
return self.next(self.state_parameter_docstring, line)

0 commit comments

Comments
 (0)