From 81adeb50e618ca9d2e2d73c2e069607f653d7bb4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 21:15:26 +0200 Subject: [PATCH 1/6] gh-104050: Annotate Argument Clinic parameter permutation helpers --- Tools/clinic/clinic.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index c02c82876591f8..6ff00bd75893b6 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -28,11 +28,12 @@ import textwrap import traceback -from collections.abc import Callable +from collections.abc import Callable, Iterable, Reversible from types import FunctionType, NoneType from typing import ( Any, Final, + Generator, Literal, NamedTuple, NoReturn, @@ -516,7 +517,13 @@ class PythonLanguage(Language): checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/" -def permute_left_option_groups(l): +ParamGroup = Iterable["Parameter"] +ParamGenerator = Generator[tuple[ParamGroup, ...], None, None] + + +def permute_left_option_groups( + l: Reversible[ParamGroup] +) -> ParamGenerator: """ Given [(1,), (2,), (3,)], should yield: () @@ -525,13 +532,15 @@ def permute_left_option_groups(l): (1, 2, 3) """ yield tuple() - accumulator = [] + accumulator: list[ParamGroup] = [] for group in reversed(l): accumulator = list(group) + accumulator yield tuple(accumulator) -def permute_right_option_groups(l): +def permute_right_option_groups( + l: Iterable[ParamGroup] +) -> ParamGenerator: """ Given [(1,), (2,), (3,)], should yield: () @@ -540,13 +549,17 @@ def permute_right_option_groups(l): (1, 2, 3) """ yield tuple() - accumulator = [] + accumulator: list[ParamGroup] = [] for group in l: accumulator.extend(group) yield tuple(accumulator) -def permute_optional_groups(left, required, right): +def permute_optional_groups( + left: Reversible[ParamGroup], + required: Iterable[ParamGroup], + right: Iterable[ParamGroup] +) -> tuple[ParamGroup, ...]: """ Generator function that computes the set of acceptable argument lists for the provided iterables of @@ -561,7 +574,7 @@ def permute_optional_groups(left, required, right): if left: raise ValueError("required is empty but left is not") - accumulator = [] + accumulator: list[ParamGroup] = [] counts = set() for r in permute_right_option_groups(right): for l in permute_left_option_groups(left): From 3667a1706ed576c03f2d2f0f50d685bcdad9b2d8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 21:21:35 +0200 Subject: [PATCH 2/6] Address review: import Generator from collections.abc --- Tools/clinic/clinic.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 6ff00bd75893b6..c2e6f09769b3bc 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -28,12 +28,11 @@ import textwrap import traceback -from collections.abc import Callable, Iterable, Reversible +from collections.abc import Callable, Generator, Iterable, Reversible from types import FunctionType, NoneType from typing import ( Any, Final, - Generator, Literal, NamedTuple, NoReturn, From c6241b016f62bcf7a077640ec770540700d483d4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 22:24:02 +0200 Subject: [PATCH 3/6] Address review: - Use a single alias: ParamTuple - Ditch generators, use Iterator - Use Sequence iso. layers of Iterable/Reversible/etc. --- Tools/clinic/clinic.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index c2e6f09769b3bc..9b95f618f5c5ce 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -28,7 +28,12 @@ import textwrap import traceback -from collections.abc import Callable, Generator, Iterable, Reversible +from collections.abc import ( + Callable, + Iterable, + Iterator, + Sequence, +) from types import FunctionType, NoneType from typing import ( Any, @@ -516,13 +521,12 @@ class PythonLanguage(Language): checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/" -ParamGroup = Iterable["Parameter"] -ParamGenerator = Generator[tuple[ParamGroup, ...], None, None] +ParamTuple = tuple["Parameter", ...] def permute_left_option_groups( - l: Reversible[ParamGroup] -) -> ParamGenerator: + l: Sequence[ParamTuple] +) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: () @@ -531,15 +535,15 @@ def permute_left_option_groups( (1, 2, 3) """ yield tuple() - accumulator: list[ParamGroup] = [] + accumulator: list[Parameter] = [] for group in reversed(l): accumulator = list(group) + accumulator yield tuple(accumulator) def permute_right_option_groups( - l: Iterable[ParamGroup] -) -> ParamGenerator: + l: Sequence[ParamTuple] +) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: () @@ -548,17 +552,17 @@ def permute_right_option_groups( (1, 2, 3) """ yield tuple() - accumulator: list[ParamGroup] = [] + accumulator: list[Parameter] = [] for group in l: accumulator.extend(group) yield tuple(accumulator) def permute_optional_groups( - left: Reversible[ParamGroup], - required: Iterable[ParamGroup], - right: Iterable[ParamGroup] -) -> tuple[ParamGroup, ...]: + left: Sequence[ParamTuple], + required: ParamTuple, + right: Sequence[ParamTuple] +) -> tuple[ParamTuple, ...]: """ Generator function that computes the set of acceptable argument lists for the provided iterables of @@ -573,7 +577,7 @@ def permute_optional_groups( if left: raise ValueError("required is empty but left is not") - accumulator: list[ParamGroup] = [] + accumulator: list[ParamTuple] = [] counts = set() for r in permute_right_option_groups(right): for l in permute_left_option_groups(left): From d854fbd916fea42a713417ca57ce8b7c7667c26d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:41:11 +0200 Subject: [PATCH 4/6] Address review: make it less specific --- Tools/clinic/clinic.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 9b95f618f5c5ce..4e91d5340f0f59 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -525,7 +525,7 @@ class PythonLanguage(Language): def permute_left_option_groups( - l: Sequence[ParamTuple] + l: Sequence[Iterable[Parameter]] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -542,7 +542,7 @@ def permute_left_option_groups( def permute_right_option_groups( - l: Sequence[ParamTuple] + l: Sequence[Iterable[Parameter]] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -559,9 +559,9 @@ def permute_right_option_groups( def permute_optional_groups( - left: Sequence[ParamTuple], - required: ParamTuple, - right: Sequence[ParamTuple] + left: Sequence[Iterable[Parameter]], + required: Iterable[Parameter], + right: Sequence[Iterable[Parameter]] ) -> tuple[ParamTuple, ...]: """ Generator function that computes the set of acceptable From 8d1821c26aa29e4bc9a1a83bb853d83ba17e9920 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:41:25 +0200 Subject: [PATCH 5/6] Add ParamGroup alias --- Tools/clinic/clinic.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 4e91d5340f0f59..4fbe3ad5fd6d4f 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -521,11 +521,12 @@ class PythonLanguage(Language): checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/" +ParamGroup = Iterable["Parameter"] ParamTuple = tuple["Parameter", ...] def permute_left_option_groups( - l: Sequence[Iterable[Parameter]] + l: Sequence[ParamGroup] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -542,7 +543,7 @@ def permute_left_option_groups( def permute_right_option_groups( - l: Sequence[Iterable[Parameter]] + l: Sequence[ParamGroup] ) -> Iterator[ParamTuple]: """ Given [(1,), (2,), (3,)], should yield: @@ -559,9 +560,9 @@ def permute_right_option_groups( def permute_optional_groups( - left: Sequence[Iterable[Parameter]], + left: Sequence[ParamGroup], required: Iterable[Parameter], - right: Sequence[Iterable[Parameter]] + right: Sequence[ParamGroup] ) -> tuple[ParamTuple, ...]: """ Generator function that computes the set of acceptable From 40cc1dc8cae75c5be6d2a1b1eaa1d09838d3eb61 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Jul 2023 23:49:33 +0200 Subject: [PATCH 6/6] Update Tools/clinic/clinic.py 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 4fbe3ad5fd6d4f..6133e9592b94e3 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -561,7 +561,7 @@ def permute_right_option_groups( def permute_optional_groups( left: Sequence[ParamGroup], - required: Iterable[Parameter], + required: ParamGroup, right: Sequence[ParamGroup] ) -> tuple[ParamTuple, ...]: """