From 7408259c7e66f63d14d52bf2c744ec18d79e31a3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 20 Jul 2023 23:53:08 +0200 Subject: [PATCH 1/3] gh-104050: Argument Clinic: Increase CConverter typing coverage --- Tools/clinic/clinic.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 6d1796cd6135ed..6851bbbc3a17cd 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2698,10 +2698,10 @@ class CConverter(metaclass=CConverterAutoRegister): """ # The C name to use for this variable. - name: str | None = None + name: str # The Python name to use for this variable. - py_name: str | None = None + py_name: str # The C type to use for this variable. # 'type' should be a Python string specifying the type, e.g. "int". @@ -2713,7 +2713,7 @@ class CConverter(metaclass=CConverterAutoRegister): # Or the magic value "unknown" if this value is a cannot be evaluated # at Argument-Clinic-preprocessing time (but is presumed to be valid # at runtime). - default: object = unspecified + default: object # If not None, default must be isinstance() of this type. # (You can also specify a tuple of types.) @@ -2864,7 +2864,11 @@ def _render_self(self, parameter: Parameter, data: CRenderData) -> None: if self.length: data.impl_parameters.append("Py_ssize_t " + self.length_name()) - def _render_non_self(self, parameter, data): + def _render_non_self( + self, + parameter: Parameter, + data: CRenderData + ) -> None: self.parameter = parameter name = self.name @@ -2917,16 +2921,15 @@ def render(self, parameter: Parameter, data: CRenderData) -> None: self._render_self(parameter, data) self._render_non_self(parameter, data) - def length_name(self): + def length_name(self) -> str: """Computes the name of the associated "length" variable.""" - if not self.length: - return None + assert self.length is not None return self.parser_name + "_length" # Why is this one broken out separately? # For "positional-only" function parsing, # which generates a bunch of PyArg_ParseTuple calls. - def parse_argument(self, list): + def parse_argument(self, list: list[str]) -> None: assert not (self.converter and self.encoding) if self.format_unit == 'O&': assert self.converter @@ -3066,7 +3069,7 @@ def set_template_dict(self, template_dict: TemplateDict) -> None: pass @property - def parser_name(self): + def parser_name(self) -> str: if self.name in CLINIC_PREFIXED_ARGS: # bpo-39741 return CLINIC_PREFIX + self.name else: From 5b94b7dd024b32c824b78982a84504d8360ac3ee Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 21 Jul 2023 00:13:17 +0200 Subject: [PATCH 2/3] Naming: list => args --- Tools/clinic/clinic.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 6851bbbc3a17cd..c111494b38e440 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2929,22 +2929,22 @@ def length_name(self) -> str: # Why is this one broken out separately? # For "positional-only" function parsing, # which generates a bunch of PyArg_ParseTuple calls. - def parse_argument(self, list: list[str]) -> None: + def parse_argument(self, args: list[str]) -> None: assert not (self.converter and self.encoding) if self.format_unit == 'O&': assert self.converter - list.append(self.converter) + args.append(self.converter) if self.encoding: - list.append(c_repr(self.encoding)) + args.append(c_repr(self.encoding)) elif self.subclass_of: - list.append(self.subclass_of) + args.append(self.subclass_of) s = ("&" if self.parse_by_reference else "") + self.name - list.append(s) + args.append(s) if self.length: - list.append("&" + self.length_name()) + args.append("&" + self.length_name()) # # All the functions after here are intended as extension points. From 7cce28f9880f2be59ebf05782a1f8b764e315b2f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 21 Jul 2023 00:16:18 +0200 Subject: [PATCH 3/3] Revert 'default' change --- 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 c111494b38e440..003ecf784d711e 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2713,7 +2713,7 @@ class CConverter(metaclass=CConverterAutoRegister): # Or the magic value "unknown" if this value is a cannot be evaluated # at Argument-Clinic-preprocessing time (but is presumed to be valid # at runtime). - default: object + default: object = unspecified # If not None, default must be isinstance() of this type. # (You can also specify a tuple of types.)