@@ -575,7 +575,7 @@ def permute_optional_groups(left, required, right):
575575return tuple (accumulator )
576576
577577
578- def strip_leading_and_trailing_blank_lines (s ) :
578+ def strip_leading_and_trailing_blank_lines (s : str ) -> str :
579579lines = s .rstrip ().split ('\n ' )
580580while lines :
581581line = lines [0 ]
@@ -585,7 +585,11 @@ def strip_leading_and_trailing_blank_lines(s):
585585return '\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):
599603return 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):
658666return 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):
680691if not after_l_paren :
681692lines .append (line )
682693continue
683- parameters ,_ ,after_r_paren = after_l_paren .partition (')' )
694+ in_paren ,_ ,after_r_paren = after_l_paren .partition (')' )
684695if not _ :
685696lines .append (line )
686697continue
687- if ',' not in parameters :
698+ if ',' not in in_paren :
688699lines .append (line )
689700continue
690- parameters = [x .strip ()+ ", " for x in parameters .split (',' )]
701+ parameters = [x .strip ()+ ", " for x in in_paren .split (',' )]
691702prefix += "("
692703if len (prefix )< length :
693704spaces = " " * len (prefix )
@@ -1589,7 +1600,12 @@ def OverrideStdioWith(stdout):
15891600sys .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."""
15941610group_re = r"\w+" if word else ".+"
15951611pattern = r'{}({}){}'
@@ -1985,7 +2001,7 @@ def file_changed(filename: str, new_contents: str) -> bool:
19852001return 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()
19902006filename_new = f"{ filename } .new"
19912007with open (filename_new ,"w" ,encoding = "utf-8" )as fp :
@@ -2602,7 +2618,10 @@ def __getattribute__(self, name: str):
26022618fail ("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 ]:
26062625if not name :
26072626name = f .__name__
26082627if not name .endswith ('_converter' ):
@@ -2620,7 +2639,10 @@ def add_default_legacy_c_converter(cls):
26202639legacy_converters [cls .format_unit ]= cls
26212640return 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:
38873909return 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 ]:
38913915if f .kind in (CALLABLE ,METHOD_INIT ):
38923916if f .cls :
38933917return "PyObject *" ,"self"
@@ -3898,7 +3922,9 @@ def correct_name_for_self(f) -> tuple[str, str]:
38983922return "PyTypeObject *" ,"type"
38993923raise 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 :
39023928type ,_ = correct_name_for_self (f )
39033929if f .kind in (METHOD_INIT ,METHOD_NEW ,STATIC_METHOD ,CLASS_METHOD ):
39043930return type
@@ -4193,7 +4219,12 @@ class float_return_converter(double_return_converter):
41934219cast = '(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='-'):
42054236if isinstance (node ,ast .Expr ):
42064237node = 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' )
42104241fn = FunctionType (co ,globals )
42114242return fn ()
42124243