Код: Выделить всё
T = TypeVar("T")
S = TypeVar("S", bound=Collection[T])
def limit(i: S, n: int) -> S:
"""
Limits the size of the input iterable to n. Truncation is randomly chosen.
"""
if len(i) len(i), верните i вместо выдачи ошибки
[*]Тип коллекции вывода должен быть идентичен входному. список[интервал | str] in = list[int | стр] выход; set[float] in = set[float] out.
[/list]
Если что-то подобное уже есть в стандартной библиотеке, Я воспользуюсь этим.
Подробнее здесь: [url]https://stackoverflow.com/questions/79036744/type-hinting-to-return-the-same-type-of-sized-iterable-as-the-input[/url]