From 37082d4ac923b6c73de896a93fda93d23971c679 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:31:17 +0200 Subject: [PATCH 01/15] Annotate strip_leading_and_trailing_blank_lines() --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index c02c82876591f8..22b22aa82f11c1 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -575,7 +575,7 @@ def permute_optional_groups(left, required, right): return tuple(accumulator) -def strip_leading_and_trailing_blank_lines(s): +def strip_leading_and_trailing_blank_lines(s: str) -> str: lines = s.rstrip().split('\n') while lines: line = lines[0] From 8543ba42639cf253e97d60c05cc7b750fd71ab66 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:32:20 +0200 Subject: [PATCH 02/15] Annotate normalize_snippet() --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 22b22aa82f11c1..0ba5bdfa70c56f 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -585,7 +585,7 @@ def strip_leading_and_trailing_blank_lines(s: str) -> str: return '\n'.join(lines) @functools.lru_cache() -def normalize_snippet(s, *, indent=0): +def normalize_snippet(s: str, *, indent=0) -> str: """ Reformats s: * removes leading and trailing blank lines From 3dcb343fa3e1d05c8c28ace133c031971cb315e3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:33:04 +0200 Subject: [PATCH 03/15] Annotate declare_parser() --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 0ba5bdfa70c56f..9d472fd245b54f 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -599,7 +599,7 @@ def normalize_snippet(s: str, *, indent=0) -> str: return s -def declare_parser(f, *, hasformat=False): +def declare_parser(f: Function, *, hasformat=False) -> str: """ Generates the code template for a static local PyArg_Parser variable, with an initializer. For core code (incl. builtin modules) the From a985dbbbd4945dc16fbd705e586516144af273b4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:37:21 +0200 Subject: [PATCH 04/15] Annotate wrap_declarations() --- Tools/clinic/clinic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 9d472fd245b54f..cd39b23b03a0ec 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -658,7 +658,7 @@ def declare_parser(f: Function, *, hasformat=False) -> str: return normalize_snippet(declarations) -def wrap_declarations(text, length=78): +def wrap_declarations(text: str, length=78) -> str: """ A simple-minded text wrapper for C function declarations. @@ -680,14 +680,14 @@ def wrap_declarations(text, length=78): if not after_l_paren: lines.append(line) continue - parameters, _, after_r_paren = after_l_paren.partition(')') + in_paren, _, after_r_paren = after_l_paren.partition(')') if not _: lines.append(line) continue - if ',' not in parameters: + if ',' not in in_paren: lines.append(line) continue - parameters = [x.strip() + ", " for x in parameters.split(',')] + parameters = [x.strip() + ", " for x in in_paren.split(',')] prefix += "(" if len(prefix) < length: spaces = " " * len(prefix) From bacaaaac52b141532c04db823f66d69413dac6f7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:40:15 +0200 Subject: [PATCH 05/15] Annotate OverrideStdioWith() --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index cd39b23b03a0ec..c8f49779449a4e 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1579,7 +1579,7 @@ def render_function(self, clinic, f): @contextlib.contextmanager -def OverrideStdioWith(stdout): +def OverrideStdioWith(stdout: io.TextIOWrapper): saved_stdout = sys.stdout sys.stdout = stdout try: From 6dc18619f16f5ad9503b6f87828aac783cb345a9 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:42:33 +0200 Subject: [PATCH 06/15] Annotate create_regex() --- Tools/clinic/clinic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index c8f49779449a4e..2d4bf249ee154d 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1589,7 +1589,12 @@ def OverrideStdioWith(stdout: io.TextIOWrapper): sys.stdout = saved_stdout -def create_regex(before, after, word=True, whole_line=True): +def create_regex( + before: str, + after: str, + word: bool = True, + whole_line: bool = True +) -> re.Pattern[str]: """Create an re object for matching marker lines.""" group_re = r"\w+" if word else ".+" pattern = r'{}({}){}' From 977a1535f961512d565a8be44a4e1bea655304f7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:45:13 +0200 Subject: [PATCH 07/15] Annotate add_c_converter() --- Tools/clinic/clinic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 2d4bf249ee154d..99bf1b0150a140 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2607,7 +2607,10 @@ def __getattribute__(self, name: str): fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__) -def add_c_converter(f, name=None): +def add_c_converter( + f: ConverterType, + name: str | None = None +) -> ConverterType: if not name: name = f.__name__ if not name.endswith('_converter'): From 248ab0808c17ef2a22d6ca85bcb3d7d31fbdb179 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:46:22 +0200 Subject: [PATCH 08/15] Style --- Tools/clinic/clinic.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 99bf1b0150a140..9c2da000c4b17c 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -585,7 +585,11 @@ def strip_leading_and_trailing_blank_lines(s: str) -> str: return '\n'.join(lines) @functools.lru_cache() -def normalize_snippet(s: str, *, indent=0) -> str: +def normalize_snippet( + s: str, + *, + indent: int = 0 +) -> str: """ Reformats s: * removes leading and trailing blank lines @@ -599,7 +603,11 @@ def normalize_snippet(s: str, *, indent=0) -> str: return s -def declare_parser(f: Function, *, hasformat=False) -> str: +def declare_parser( + f: Function, + *, + hasformat: bool = False +) -> str: """ Generates the code template for a static local PyArg_Parser variable, with an initializer. For core code (incl. builtin modules) the @@ -658,7 +666,10 @@ def declare_parser(f: Function, *, hasformat=False) -> str: return normalize_snippet(declarations) -def wrap_declarations(text: str, length=78) -> str: +def wrap_declarations( + text: str, + length: int = 78 +) -> str: """ A simple-minded text wrapper for C function declarations. From 7ed610812b48055398dfb1539a59e3e329a35977 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:47:23 +0200 Subject: [PATCH 09/15] Return type for write_file() --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 9c2da000c4b17c..b127e606a2f242 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2001,7 +2001,7 @@ def file_changed(filename: str, new_contents: str) -> bool: return True -def write_file(filename: str, new_contents: str): +def write_file(filename: str, new_contents: str) -> None: # Atomic write using a temporary file and os.replace() filename_new = f"{filename}.new" with open(filename_new, "w", encoding="utf-8") as fp: From 7b89ffb5a01d92b4826f7e114084995d38bea2b2 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:51:15 +0200 Subject: [PATCH 10/15] Annotate add_legacy_c_converter() --- Tools/clinic/clinic.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index b127e606a2f242..034123293759fd 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2639,7 +2639,10 @@ def add_default_legacy_c_converter(cls): legacy_converters[cls.format_unit] = cls return cls -def add_legacy_c_converter(format_unit, **kwargs): +def add_legacy_c_converter( + format_unit: str, + **kwargs +) -> Callable[[ConverterType], ConverterType]: """ Adds a legacy converter. """ From 0e2d3a0528503e924eba134f17495afc04fd3ed3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:04:47 +0200 Subject: [PATCH 11/15] Annotate correct_name_for_self() --- Tools/clinic/clinic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 034123293759fd..7a366dd123a5dd 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -3909,7 +3909,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) -def correct_name_for_self(f) -> tuple[str, str]: +def correct_name_for_self( + f: Function +) -> tuple[str, str]: if f.kind in (CALLABLE, METHOD_INIT): if f.cls: return "PyObject *", "self" From b5ae56ff4147f90139dbd77e115dbcfee30ca0ba Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:05:25 +0200 Subject: [PATCH 12/15] Annotate required_type_for_self_for_parser() --- Tools/clinic/clinic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 7a366dd123a5dd..c9ff3d129a5d65 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -3922,7 +3922,9 @@ def correct_name_for_self( return "PyTypeObject *", "type" raise RuntimeError("Unhandled type of function f: " + repr(f.kind)) -def required_type_for_self_for_parser(f): +def required_type_for_self_for_parser( + f: Function +) -> str | None: type, _ = correct_name_for_self(f) if f.kind in (METHOD_INIT, METHOD_NEW, STATIC_METHOD, CLASS_METHOD): return type From 77ef6938abf5e5d65019a09c9f3e741fdb85588e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:08:16 +0200 Subject: [PATCH 13/15] Annotate eval_ast_expr() --- Tools/clinic/clinic.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index c9ff3d129a5d65..9ef2d9b27a9539 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -4219,7 +4219,12 @@ class float_return_converter(double_return_converter): cast = '(double)' -def eval_ast_expr(node, globals, *, filename='-'): +def eval_ast_expr( + node: ast.expr, + globals: dict[str, Any], + *, + filename: str = '-' +) -> FunctionType: """ Takes an ast.Expr node. Compiles and evaluates it. Returns the result of the expression. @@ -4231,8 +4236,8 @@ def eval_ast_expr(node, globals, *, filename='-'): if isinstance(node, ast.Expr): node = node.value - node = ast.Expression(node) - co = compile(node, filename, 'eval') + expr = ast.Expression(node) + co = compile(expr, filename, 'eval') fn = FunctionType(co, globals) return fn() From e9edfb8e84b0fc5c0553bacade80ce8aec078890 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:46:31 +0200 Subject: [PATCH 14/15] Revert "Annotate OverrideStdioWith()" This reverts commit bacaaaac52b141532c04db823f66d69413dac6f7. --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 9ef2d9b27a9539..13287ccc11bd0f 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1590,7 +1590,7 @@ def render_function(self, clinic, f): @contextlib.contextmanager -def OverrideStdioWith(stdout: io.TextIOWrapper): +def OverrideStdioWith(stdout): saved_stdout = sys.stdout sys.stdout = stdout try: From f184df746a33b29a1033e0357022cbf5d35e44bf Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:48:36 +0200 Subject: [PATCH 15/15] Address review --- Tools/clinic/clinic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 13287ccc11bd0f..3d18d9560bc28b 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2619,9 +2619,9 @@ def __getattribute__(self, name: str): def add_c_converter( - f: ConverterType, + f: type[CConverter], name: str | None = None -) -> ConverterType: +) -> type[CConverter]: if not name: name = f.__name__ if not name.endswith('_converter'):