Skip to content

Commit 110b97c

Browse files
gh-104050: Annotate toplevel functions in clinic.py (#106435)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 17af982 commit 110b97c

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

Tools/clinic/clinic.py

+47-16
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def permute_optional_groups(left, required, right):
575575
return tuple(accumulator)
576576

577577

578-
def strip_leading_and_trailing_blank_lines(s):
578+
def strip_leading_and_trailing_blank_lines(s: str) -> str:
579579
lines = s.rstrip().split('\n')
580580
while lines:
581581
line = lines[0]
@@ -585,7 +585,11 @@ def strip_leading_and_trailing_blank_lines(s):
585585
return '\n'.join(lines)
586586

587587
@functools.lru_cache()
588-
def normalize_snippet(s, *, indent=0):
588+
def normalize_snippet(
589+
s: str,
590+
*,
591+
indent: int = 0
592+
) -> str:
589593
"""
590594
Reformats s:
591595
* removes leading and trailing blank lines
@@ -599,7 +603,11 @@ def normalize_snippet(s, *, indent=0):
599603
return s
600604

601605

602-
def declare_parser(f, *, hasformat=False):
606+
def declare_parser(
607+
f: Function,
608+
*,
609+
hasformat: bool = False
610+
) -> str:
603611
"""
604612
Generates the code template for a static local PyArg_Parser variable,
605613
with an initializer. For core code (incl. builtin modules) the
@@ -658,7 +666,10 @@ def declare_parser(f, *, hasformat=False):
658666
return normalize_snippet(declarations)
659667

660668

661-
def wrap_declarations(text, length=78):
669+
def wrap_declarations(
670+
text: str,
671+
length: int = 78
672+
) -> str:
662673
"""
663674
A simple-minded text wrapper for C function declarations.
664675
@@ -680,14 +691,14 @@ def wrap_declarations(text, length=78):
680691
if not after_l_paren:
681692
lines.append(line)
682693
continue
683-
parameters, _, after_r_paren = after_l_paren.partition(')')
694+
in_paren, _, after_r_paren = after_l_paren.partition(')')
684695
if not _:
685696
lines.append(line)
686697
continue
687-
if ',' not in parameters:
698+
if ',' not in in_paren:
688699
lines.append(line)
689700
continue
690-
parameters = [x.strip() + ", " for x in parameters.split(',')]
701+
parameters = [x.strip() + ", " for x in in_paren.split(',')]
691702
prefix += "("
692703
if len(prefix) < length:
693704
spaces = " " * len(prefix)
@@ -1589,7 +1600,12 @@ def OverrideStdioWith(stdout):
15891600
sys.stdout = saved_stdout
15901601

15911602

1592-
def create_regex(before, after, word=True, whole_line=True):
1603+
def create_regex(
1604+
before: str,
1605+
after: str,
1606+
word: bool = True,
1607+
whole_line: bool = True
1608+
) -> re.Pattern[str]:
15931609
"""Create an re object for matching marker lines."""
15941610
group_re = r"\w+" if word else ".+"
15951611
pattern = r'{}({}){}'
@@ -1985,7 +2001,7 @@ def file_changed(filename: str, new_contents: str) -> bool:
19852001
return True
19862002

19872003

1988-
def write_file(filename: str, new_contents: str):
2004+
def write_file(filename: str, new_contents: str) -> None:
19892005
# Atomic write using a temporary file and os.replace()
19902006
filename_new = f"{filename}.new"
19912007
with open(filename_new, "w", encoding="utf-8") as fp:
@@ -2602,7 +2618,10 @@ def __getattribute__(self, name: str):
26022618
fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__)
26032619

26042620

2605-
def add_c_converter(f, name=None):
2621+
def add_c_converter(
2622+
f: type[CConverter],
2623+
name: str | None = None
2624+
) -> type[CConverter]:
26062625
if not name:
26072626
name = f.__name__
26082627
if not name.endswith('_converter'):
@@ -2620,7 +2639,10 @@ def add_default_legacy_c_converter(cls):
26202639
legacy_converters[cls.format_unit] = cls
26212640
return cls
26222641

2623-
def add_legacy_c_converter(format_unit, **kwargs):
2642+
def add_legacy_c_converter(
2643+
format_unit: str,
2644+
**kwargs
2645+
) -> Callable[[ConverterType], ConverterType]:
26242646
"""
26252647
Adds a legacy converter.
26262648
"""
@@ -3887,7 +3909,9 @@ def parse_arg(self, argname: str, displayname: str) -> str:
38873909
return super().parse_arg(argname, displayname)
38883910

38893911

3890-
def correct_name_for_self(f) -> tuple[str, str]:
3912+
def correct_name_for_self(
3913+
f: Function
3914+
) -> tuple[str, str]:
38913915
if f.kind in (CALLABLE, METHOD_INIT):
38923916
if f.cls:
38933917
return "PyObject *", "self"
@@ -3898,7 +3922,9 @@ def correct_name_for_self(f) -> tuple[str, str]:
38983922
return "PyTypeObject *", "type"
38993923
raise RuntimeError("Unhandled type of function f: " + repr(f.kind))
39003924

3901-
def required_type_for_self_for_parser(f):
3925+
def required_type_for_self_for_parser(
3926+
f: Function
3927+
) -> str | None:
39023928
type, _ = correct_name_for_self(f)
39033929
if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD):
39043930
return type
@@ -4193,7 +4219,12 @@ class float_return_converter(double_return_converter):
41934219
cast = '(double)'
41944220

41954221

4196-
def eval_ast_expr(node, globals, *, filename='-'):
4222+
def eval_ast_expr(
4223+
node: ast.expr,
4224+
globals: dict[str, Any],
4225+
*,
4226+
filename: str = '-'
4227+
) -> FunctionType:
41974228
"""
41984229
Takes an ast.Expr node. Compiles and evaluates it.
41994230
Returns the result of the expression.
@@ -4205,8 +4236,8 @@ def eval_ast_expr(node, globals, *, filename='-'):
42054236
if isinstance(node, ast.Expr):
42064237
node = node.value
42074238

4208-
node = ast.Expression(node)
4209-
co = compile(node, filename, 'eval')
4239+
expr = ast.Expression(node)
4240+
co = compile(expr, filename, 'eval')
42104241
fn = FunctionType(co, globals)
42114242
return fn()
42124243

0 commit comments

Comments
 (0)