From c0d3d9d747c5e62abff2d794bf95bd42d852ae0d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 20 Jul 2023 22:38:53 +0200 Subject: [PATCH 1/4] gh-104050: Argument Clinic: Annotate CLanguage.render_option_group_parsing() --- Tools/clinic/clinic.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index bff8935df13bc6..d211ada3d55692 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -530,12 +530,12 @@ class PythonLanguage(Language): checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/" -ParamGroup = Iterable["Parameter"] +ParamIter = Iterable["Parameter"] ParamTuple = tuple["Parameter", ...] def permute_left_option_groups( - l: Sequence[ParamGroup] + l: Sequence[ParamIter] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -552,7 +552,7 @@ def permute_left_option_groups( def permute_right_option_groups( - l: Sequence[ParamGroup] + l: Sequence[ParamIter] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -569,9 +569,9 @@ def permute_right_option_groups( def permute_optional_groups( - left: Sequence[ParamGroup], - required: ParamGroup, - right: Sequence[ParamGroup] + left: Sequence[ParamIter], + required: ParamIter, + right: Sequence[ParamIter] ) -> tuple[ParamTuple, ...]: """ Generator function that computes the set of acceptable @@ -1374,7 +1374,11 @@ def group_to_variable_name(group: int) -> str: adjective = "left_" if group < 0 else "right_" return "group_" + adjective + str(abs(group)) - def render_option_group_parsing(self, f, template_dict): + def render_option_group_parsing( + self, + f: Function, + template_dict: TemplateDict + ) -> None: # positional only, grouped, optional arguments! # can be optional on the left or right. # here's an example: @@ -1398,11 +1402,11 @@ def render_option_group_parsing(self, f, template_dict): if isinstance(parameters[0].converter, self_converter): del parameters[0] - group = None + group: list["Parameter"] | None = None left = [] right = [] - required = [] - last = unspecified + required: list["Parameter"] = [] + last: int | Literal[Sentinels.unspecified] = unspecified for p in parameters: group_id = p.group @@ -1415,6 +1419,7 @@ def render_option_group_parsing(self, f, template_dict): group = required else: right.append(group) + assert group is not None group.append(p) count_min = sys.maxsize @@ -1433,19 +1438,21 @@ def render_option_group_parsing(self, f, template_dict): continue group_ids = {p.group for p in subset} # eliminate duplicates - d = {} + d: dict[str, str | int] = {} d['count'] = count d['name'] = f.name d['format_units'] = "".join(p.converter.format_unit for p in subset) - parse_arguments = [] + parse_arguments: list[Any] = [] for p in subset: p.converter.parse_argument(parse_arguments) d['parse_arguments'] = ", ".join(parse_arguments) group_ids.discard(0) - lines = [self.group_to_variable_name(g) + " = 1;" for g in group_ids] - lines = "\n".join(lines) + lines = "\n".join([ + self.group_to_variable_name(g) + " = 1;" + for g in group_ids + ]) s = """\ case {count}: From a13d26bb22e1d73822bbbca7a0e0e2b66dd907bf Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 20 Jul 2023 22:52:19 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Extra quotes around Parameter are not needed anymore Co-authored-by: Alex Waygood --- 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 d211ada3d55692..cb258f626a8fb5 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1402,10 +1402,10 @@ def render_option_group_parsing( if isinstance(parameters[0].converter, self_converter): del parameters[0] - group: list["Parameter"] | None = None + group: list[Parameter] | None = None left = [] right = [] - required: list["Parameter"] = [] + required: list[Parameter] = [] last: int | Literal[Sentinels.unspecified] = unspecified for p in parameters: From 4fa647ef6c3d95e378d1edbfa52d02e895b0fbac Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 20 Jul 2023 22:59:00 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Rip out ParamIter --- Tools/clinic/clinic.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index cb258f626a8fb5..3ed9879c2a4ccb 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -530,12 +530,11 @@ class PythonLanguage(Language): checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/" -ParamIter = Iterable["Parameter"] ParamTuple = tuple["Parameter", ...] def permute_left_option_groups( - l: Sequence[ParamIter] + l: Sequence[Iterable[Parameter]] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -552,7 +551,7 @@ def permute_left_option_groups( def permute_right_option_groups( - l: Sequence[ParamIter] + l: Sequence[Iterable[Parameter]] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -569,9 +568,9 @@ def permute_right_option_groups( def permute_optional_groups( - left: Sequence[ParamIter], - required: ParamIter, - right: Sequence[ParamIter] + left: Sequence[Iterable[Parameter]], + required: Iterable[Parameter], + right: Sequence[Iterable[Parameter]] ) -> tuple[ParamTuple, ...]: """ Generator function that computes the set of acceptable From 5fe6c3ff7390ebc5e9d0f70bd9d5ffe05303406a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 20 Jul 2023 23:01:47 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=A4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alex Waygood --- 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 3ed9879c2a4ccb..6d1796cd6135ed 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1442,7 +1442,7 @@ def render_option_group_parsing( d['name'] = f.name d['format_units'] = "".join(p.converter.format_unit for p in subset) - parse_arguments: list[Any] = [] + parse_arguments: list[str] = [] for p in subset: p.converter.parse_argument(parse_arguments) d['parse_arguments'] = ", ".join(parse_arguments)