From 44618e31c8a9d2fc193ac64af1aa859f0942917a Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 1 Oct 2024 15:58:24 +0200 Subject: stable_partition: use TypeVar (#46686) --- lib/spack/docs/conf.py | 2 ++ lib/spack/llnl/util/lang.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/spack/docs/conf.py b/lib/spack/docs/conf.py index c80a661abd..4873e3e104 100644 --- a/lib/spack/docs/conf.py +++ b/lib/spack/docs/conf.py @@ -220,6 +220,8 @@ nitpick_ignore = [ ("py:class", "spack.filesystem_view.SimpleFilesystemView"), ("py:class", "spack.traverse.EdgeAndDepth"), ("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. diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index 736013fe58..5efe27b8b1 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -12,7 +12,7 @@ import re import sys import traceback 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_modules = r"^\.#|~$" @@ -879,9 +879,12 @@ def enum(**kwargs): return type("Enum", (object,), kwargs) +T = TypeVar("T") + + def stable_partition( - input_iterable: Iterable, predicate_fn: Callable[[Any], bool] -) -> Tuple[List[Any], List[Any]]: + input_iterable: Iterable[T], predicate_fn: Callable[[T], bool] +) -> Tuple[List[T], List[T]]: """Partition the input iterable according to a custom predicate. Args: @@ -893,12 +896,13 @@ def stable_partition( Tuple of the list of elements evaluating to True, and list of elements evaluating to False. """ - true_items, false_items = [], [] + true_items: List[T] = [] + false_items: List[T] = [] for item in input_iterable: if predicate_fn(item): true_items.append(item) - continue - false_items.append(item) + else: + false_items.append(item) return true_items, false_items -- cgit v1.2.3-70-g09d2