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 1 commit
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
Prev Previous commit
Next Next commit
Address review:
- Use a single alias: ParamTuple
- Ditch generators, use Iterator
- Use Sequence iso. layers of Iterable/Reversible/etc.
  • Loading branch information
erlend-aasland committed Jul 4, 2023
commit c6241b016f62bcf7a077640ec770540700d483d4
32 changes: 18 additions & 14 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, Generator, Iterable, Reversible
from collections.abc import (
Callable,
Iterable,
Iterator,
Sequence,
)
from types import FunctionType, NoneType
from typing import (
Any,
Expand Down Expand Up @@ -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:
()
Expand All @@ -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:
()
Expand All @@ -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]
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
) -> tuple[ParamTuple, ...]:
"""
Generator function that computes the set of acceptable
argument lists for the provided iterables of
Expand All @@ -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):
Expand Down
Loading