Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-104050: Annotate Argument Clinic parameter permutation helpers #106431

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
import textwrap
import traceback

from collections.abc import Callable
from collections.abc import (
Callable,
Iterable,
Iterator,
Sequence,
)
from types import FunctionType, NoneType
from typing import (
Any,
Expand Down Expand Up @@ -516,7 +521,13 @@ class PythonLanguage(Language):
checksum_line = "#/*[{dsl_name} end generated code: {arguments}]*/"


def permute_left_option_groups(l):
ParamGroup = Iterable["Parameter"]
ParamTuple = tuple["Parameter", ...]


def permute_left_option_groups(
l: Sequence[ParamGroup]
) -> Iterator[ParamTuple]:
"""
Given [(1,), (2,), (3,)], should yield:
()
Expand All @@ -525,13 +536,15 @@ def permute_left_option_groups(l):
(1, 2, 3)
"""
yield tuple()
accumulator = []
accumulator: list[Parameter] = []
for group in reversed(l):
accumulator = list(group) + accumulator
yield tuple(accumulator)


def permute_right_option_groups(l):
def permute_right_option_groups(
l: Sequence[ParamGroup]
) -> Iterator[ParamTuple]:
"""
Given [(1,), (2,), (3,)], should yield:
()
Expand All @@ -540,13 +553,17 @@ def permute_right_option_groups(l):
(1, 2, 3)
"""
yield tuple()
accumulator = []
accumulator: list[Parameter] = []
for group in l:
accumulator.extend(group)
yield tuple(accumulator)


def permute_optional_groups(left, required, right):
def permute_optional_groups(
left: Sequence[ParamGroup],
required: ParamGroup,
right: Sequence[ParamGroup]
) -> tuple[ParamTuple, ...]:
"""
Generator function that computes the set of acceptable
argument lists for the provided iterables of
Expand All @@ -561,7 +578,7 @@ def permute_optional_groups(left, required, right):
if left:
raise ValueError("required is empty but left is not")

accumulator = []
accumulator: list[ParamTuple] = []
counts = set()
for r in permute_right_option_groups(right):
for l in permute_left_option_groups(left):
Expand Down