Skip to content

Commit fdbccdc

Browse files
erlend-aaslandAlexWaygood
authored andcommitted
pythongh-104050: Argument Clinic: Increase typing coverage (python#107074)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 5d1b677 commit fdbccdc

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

Tools/clinic/clinic.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,12 +2950,18 @@ def parse_argument(self, args: list[str]) -> None:
29502950
# All the functions after here are intended as extension points.
29512951
#
29522952

2953-
def simple_declaration(self, by_reference=False, *, in_parser=False):
2953+
def simple_declaration(
2954+
self,
2955+
by_reference: bool = False,
2956+
*,
2957+
in_parser: bool = False
2958+
) -> str:
29542959
"""
29552960
Computes the basic declaration of the variable.
29562961
Used in computing the prototype declaration and the
29572962
variable declaration.
29582963
"""
2964+
assert isinstance(self.type, str)
29592965
prototype = [self.type]
29602966
if by_reference or not self.type.endswith('*'):
29612967
prototype.append(" ")
@@ -2970,7 +2976,7 @@ def simple_declaration(self, by_reference=False, *, in_parser=False):
29702976
prototype.append(name)
29712977
return "".join(prototype)
29722978

2973-
def declaration(self, *, in_parser=False) -> str:
2979+
def declaration(self, *, in_parser: bool = False) -> str:
29742980
"""
29752981
The C statement to declare this variable.
29762982
"""
@@ -3579,9 +3585,9 @@ class object_converter(CConverter):
35793585

35803586
def converter_init(
35813587
self, *,
3582-
converter=None,
3583-
type=None,
3584-
subclass_of=None
3588+
converter: str | None = None,
3589+
type: str | None = None,
3590+
subclass_of: str | None = None
35853591
) -> None:
35863592
if converter:
35873593
if subclass_of:
@@ -3973,7 +3979,7 @@ class self_converter(CConverter):
39733979
type = None
39743980
format_unit = ''
39753981

3976-
def converter_init(self, *, type=None) -> None:
3982+
def converter_init(self, *, type: str | None = None) -> None:
39773983
self.specified_type = type
39783984

39793985
def pre_render(self):
@@ -4047,7 +4053,7 @@ def render(self, parameter, data):
40474053
assert data.impl_arguments[0] == self.name
40484054
data.impl_arguments[0] = '(' + self.type + ")" + data.impl_arguments[0]
40494055

4050-
def set_template_dict(self, template_dict):
4056+
def set_template_dict(self, template_dict: TemplateDict) -> None:
40514057
template_dict['self_name'] = self.name
40524058
template_dict['self_type'] = self.parser_type
40534059
kind = self.function.kind
@@ -4066,7 +4072,7 @@ def set_template_dict(self, template_dict):
40664072
line = f'{type_check} &&\n '
40674073
template_dict['self_type_check'] = line
40684074

4069-
type_object = self.function.cls.type_object
4075+
type_object = cls.type_object
40704076
type_ptr = f'PyTypeObject *base_tp = {type_object};'
40714077
template_dict['base_type_ptr'] = type_ptr
40724078

@@ -4276,11 +4282,11 @@ def eval_ast_expr(
42764282

42774283

42784284
class IndentStack:
4279-
def __init__(self):
4280-
self.indents = []
4281-
self.margin = None
4285+
def __init__(self) -> None:
4286+
self.indents: list[int] = []
4287+
self.margin: str | None = None
42824288

4283-
def _ensure(self):
4289+
def _ensure(self) -> None:
42844290
if not self.indents:
42854291
fail('IndentStack expected indents, but none are defined.')
42864292

@@ -4341,13 +4347,15 @@ def indent(self, line: str) -> str:
43414347
"""
43424348
Indents a line by the currently defined margin.
43434349
"""
4350+
assert self.margin is not None, "Cannot call .indent() before calling .infer()"
43444351
return self.margin + line
43454352

43464353
def dedent(self, line: str) -> str:
43474354
"""
43484355
Dedents a line by the currently defined margin.
43494356
(The inverse of 'indent'.)
43504357
"""
4358+
assert self.margin is not None, "Cannot call .indent() before calling .infer()"
43514359
margin = self.margin
43524360
indent = self.indents[-1]
43534361
if not line.startswith(margin):
@@ -5232,6 +5240,7 @@ def parse_slash(self, function: Function) -> None:
52325240
p.kind = inspect.Parameter.POSITIONAL_ONLY
52335241

52345242
def state_parameter_docstring_start(self, line: str | None) -> None:
5243+
assert self.indent.margin is not None, "self.margin.infer() has not yet been called to set the margin"
52355244
self.parameter_docstring_indent = len(self.indent.margin)
52365245
assert self.indent.depth == 3
52375246
return self.next(self.state_parameter_docstring, line)
@@ -5534,7 +5543,7 @@ def add_parameter(text):
55345543

55355544
return docstring
55365545

5537-
def state_terminal(self, line):
5546+
def state_terminal(self, line: str | None) -> None:
55385547
"""
55395548
Called when processing the block is done.
55405549
"""

0 commit comments

Comments
 (0)