stable_partition: use TypeVar (#46686)

This commit is contained in:
Harmen Stoppels 2024-10-01 15:58:24 +02:00 committed by GitHub
parent 5bc105c01c
commit 44618e31c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

View File

@ -220,6 +220,8 @@ def setup(sphinx):
("py:class", "spack.filesystem_view.SimpleFilesystemView"), ("py:class", "spack.filesystem_view.SimpleFilesystemView"),
("py:class", "spack.traverse.EdgeAndDepth"), ("py:class", "spack.traverse.EdgeAndDepth"),
("py:class", "archspec.cpu.microarchitecture.Microarchitecture"), ("py:class", "archspec.cpu.microarchitecture.Microarchitecture"),
# TypeVar that is not handled correctly
("py:class", "llnl.util.lang.T"),
] ]
# The reST default role (used for this markup: `text`) to use for all documents. # The reST default role (used for this markup: `text`) to use for all documents.

View File

@ -12,7 +12,7 @@
import sys import sys
import traceback import traceback
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any, Callable, Iterable, List, Tuple from typing import Callable, Iterable, List, Tuple, TypeVar
# Ignore emacs backups when listing modules # Ignore emacs backups when listing modules
ignore_modules = r"^\.#|~$" ignore_modules = r"^\.#|~$"
@ -879,9 +879,12 @@ def enum(**kwargs):
return type("Enum", (object,), kwargs) return type("Enum", (object,), kwargs)
T = TypeVar("T")
def stable_partition( def stable_partition(
input_iterable: Iterable, predicate_fn: Callable[[Any], bool] input_iterable: Iterable[T], predicate_fn: Callable[[T], bool]
) -> Tuple[List[Any], List[Any]]: ) -> Tuple[List[T], List[T]]:
"""Partition the input iterable according to a custom predicate. """Partition the input iterable according to a custom predicate.
Args: Args:
@ -893,12 +896,13 @@ def stable_partition(
Tuple of the list of elements evaluating to True, and Tuple of the list of elements evaluating to True, and
list of elements evaluating to False. list of elements evaluating to False.
""" """
true_items, false_items = [], [] true_items: List[T] = []
false_items: List[T] = []
for item in input_iterable: for item in input_iterable:
if predicate_fn(item): if predicate_fn(item):
true_items.append(item) true_items.append(item)
continue else:
false_items.append(item) false_items.append(item)
return true_items, false_items return true_items, false_items