@@ -575,7 +575,7 @@ def permute_optional_groups(left, required, right):
575
575
return tuple(accumulator)
576
576
577
577
578
- def strip_leading_and_trailing_blank_lines(s) :
578
+ def strip_leading_and_trailing_blank_lines(s: str) -> str :
579
579
lines = s.rstrip().split('\n')
580
580
while lines:
581
581
line = lines[0]
@@ -585,7 +585,11 @@ def strip_leading_and_trailing_blank_lines(s):
585
585
return '\n'.join(lines)
586
586
587
587
@functools.lru_cache()
588
- def normalize_snippet(s, *, indent=0):
588
+ def normalize_snippet(
589
+ s: str,
590
+ *,
591
+ indent: int = 0
592
+ ) -> str:
589
593
"""
590
594
Reformats s:
591
595
* removes leading and trailing blank lines
@@ -599,7 +603,11 @@ def normalize_snippet(s, *, indent=0):
599
603
return s
600
604
601
605
602
- def declare_parser(f, *, hasformat=False):
606
+ def declare_parser(
607
+ f: Function,
608
+ *,
609
+ hasformat: bool = False
610
+ ) -> str:
603
611
"""
604
612
Generates the code template for a static local PyArg_Parser variable,
605
613
with an initializer. For core code (incl. builtin modules) the
@@ -658,7 +666,10 @@ def declare_parser(f, *, hasformat=False):
658
666
return normalize_snippet(declarations)
659
667
660
668
661
- def wrap_declarations(text, length=78):
669
+ def wrap_declarations(
670
+ text: str,
671
+ length: int = 78
672
+ ) -> str:
662
673
"""
663
674
A simple-minded text wrapper for C function declarations.
664
675
@@ -680,14 +691,14 @@ def wrap_declarations(text, length=78):
680
691
if not after_l_paren:
681
692
lines.append(line)
682
693
continue
683
- parameters , _, after_r_paren = after_l_paren.partition(')')
694
+ in_paren , _, after_r_paren = after_l_paren.partition(')')
684
695
if not _:
685
696
lines.append(line)
686
697
continue
687
- if ',' not in parameters :
698
+ if ',' not in in_paren :
688
699
lines.append(line)
689
700
continue
690
- parameters = [x.strip() + ", " for x in parameters .split(',')]
701
+ parameters = [x.strip() + ", " for x in in_paren .split(',')]
691
702
prefix += "("
692
703
if len(prefix) < length:
693
704
spaces = " " * len(prefix)
@@ -1589,7 +1600,12 @@ def OverrideStdioWith(stdout):
1589
1600
sys.stdout = saved_stdout
1590
1601
1591
1602
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]:
1593
1609
"""Create an re object for matching marker lines."""
1594
1610
group_re = r"\w+" if word else ".+"
1595
1611
pattern = r'{}({}){}'
@@ -1985,7 +2001,7 @@ def file_changed(filename: str, new_contents: str) -> bool:
1985
2001
return True
1986
2002
1987
2003
1988
- def write_file(filename: str, new_contents: str):
2004
+ def write_file(filename: str, new_contents: str) -> None :
1989
2005
# Atomic write using a temporary file and os.replace()
1990
2006
filename_new = f"{filename}.new"
1991
2007
with open(filename_new, "w", encoding="utf-8") as fp:
@@ -2602,7 +2618,10 @@ def __getattribute__(self, name: str):
2602
2618
fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__)
2603
2619
2604
2620
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]:
2606
2625
if not name:
2607
2626
name = f.__name__
2608
2627
if not name.endswith('_converter'):
@@ -2620,7 +2639,10 @@ def add_default_legacy_c_converter(cls):
2620
2639
legacy_converters[cls.format_unit] = cls
2621
2640
return cls
2622
2641
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]:
2624
2646
"""
2625
2647
Adds a legacy converter.
2626
2648
"""
@@ -3887,7 +3909,9 @@ def parse_arg(self, argname: str, displayname: str) -> str:
3887
3909
return super().parse_arg(argname, displayname)
3888
3910
3889
3911
3890
- def correct_name_for_self(f) -> tuple[str, str]:
3912
+ def correct_name_for_self(
3913
+ f: Function
3914
+ ) -> tuple[str, str]:
3891
3915
if f.kind in (CALLABLE, METHOD_INIT):
3892
3916
if f.cls:
3893
3917
return "PyObject *", "self"
@@ -3898,7 +3922,9 @@ def correct_name_for_self(f) -> tuple[str, str]:
3898
3922
return "PyTypeObject *", "type"
3899
3923
raise RuntimeError("Unhandled type of function f: " + repr(f.kind))
3900
3924
3901
- def required_type_for_self_for_parser(f):
3925
+ def required_type_for_self_for_parser(
3926
+ f: Function
3927
+ ) -> str | None:
3902
3928
type, _ = correct_name_for_self(f)
3903
3929
if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD):
3904
3930
return type
@@ -4193,7 +4219,12 @@ class float_return_converter(double_return_converter):
4193
4219
cast = '(double)'
4194
4220
4195
4221
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:
4197
4228
"""
4198
4229
Takes an ast.Expr node. Compiles and evaluates it.
4199
4230
Returns the result of the expression.
@@ -4205,8 +4236,8 @@ def eval_ast_expr(node, globals, *, filename='-'):
4205
4236
if isinstance(node, ast.Expr):
4206
4237
node = node.value
4207
4238
4208
- node = ast.Expression(node)
4209
- co = compile(node , filename, 'eval')
4239
+ expr = ast.Expression(node)
4240
+ co = compile(expr , filename, 'eval')
4210
4241
fn = FunctionType(co, globals)
4211
4242
return fn()
4212
4243
0 commit comments