Skip to content

Commit 7bf70fe

Browse files
committed
modernize type hints
1 parent b31e53d commit 7bf70fe

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

clj/seqs.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import collections.abc as collections_abc
44
import itertools
55
import random
6-
from typing import Iterable, TypeVar, Any, Callable, Iterator, Union, Tuple, Dict, Optional, List, Set, cast, Deque
6+
from typing import Iterable, TypeVar, Any, Callable, Iterator, Union, cast, Deque
77

88
import clj
99

@@ -224,7 +224,7 @@ def butlast(coll: Iterable[T]) -> Iterable[T]:
224224
Return a generator of all but the last item in ``coll``, in linear time.
225225
"""
226226
first_ = True
227-
last_e: Optional[T] = None
227+
last_e: T | None = None
228228
for e in coll:
229229
if first_:
230230
last_e = e
@@ -303,7 +303,7 @@ def _iter(coll, n=0):
303303
return coll[n:]
304304

305305

306-
def split_at(n: int, coll: Iterable[T]) -> Tuple[Iterable[T], Iterable[T]]:
306+
def split_at(n: int, coll: Iterable[T]) -> tuple[Iterable[T], Iterable[T]]:
307307
"""
308308
Returns a tuple of ``(take(n, coll), drop(n coll))``.
309309
"""
@@ -325,7 +325,7 @@ def split_at(n: int, coll: Iterable[T]) -> Tuple[Iterable[T], Iterable[T]]:
325325
return taken, _iter(coll, n)
326326

327327

328-
def split_with(pred: Callable[[T], Any], coll: Iterable[T]) -> Tuple[Iterable[T], Iterable[T]]:
328+
def split_with(pred: Callable[[T], Any], coll: Iterable[T]) -> tuple[Iterable[T], Iterable[T]]:
329329
"""
330330
Returns a tuple of ``(take_while(pred, coll), drop_while(pred coll))``.
331331
"""
@@ -348,7 +348,7 @@ def dropped_while():
348348
return taken, dropped_while()
349349

350350

351-
def replace(smap: Dict[T, T2], coll: Iterable[T]) -> Iterable[Union[T, T2]]:
351+
def replace(smap: dict[T, T2], coll: Iterable[T]) -> Iterable[Union[T, T2]]:
352352
"""
353353
Given a map of replacement pairs and a list/collection, yield a sequence
354354
where any element = a key in ``smap`` replaced with the corresponding val
@@ -396,7 +396,7 @@ def map_indexed(f: Callable[[int, T], T2], coll: Iterable[T]) -> Iterable[T2]:
396396
return map(lambda pair: f(pair[0], pair[1]), enumerate(coll))
397397

398398

399-
def _first(coll: Iterable[T]) -> Tuple[Optional[T], bool]:
399+
def _first(coll: Iterable[T]) -> tuple[T | None, bool]:
400400
"""
401401
Like first(coll), but return a tuple of ``(first, is_empty)`` where `first` is either the first
402402
element of the collection or ``None`` and ``is_empty`` is a boolean that is ``True`` if the collection
@@ -409,18 +409,18 @@ def _first(coll: Iterable[T]) -> Tuple[Optional[T], bool]:
409409
first_value: Union[T, object] = next(_make_gen(take(1, coll)), _flag)
410410
if first_value is _flag:
411411
return None, True
412-
return cast(Optional[T], first_value), False
412+
return cast(T | None, first_value), False
413413

414414

415-
def first(coll: Iterable[T]) -> Optional[T]:
415+
def first(coll: Iterable[T]) -> T | None:
416416
"""
417417
Returns the first item in the collection. If ``coll`` is empty, returns ``None``.
418418
"""
419-
first_value: Optional[T] = _first(coll)[0]
419+
first_value: T | None = _first(coll)[0]
420420
return first_value
421421

422422

423-
def ffirst(x: Iterable[Iterable[T]]) -> Optional[T]:
423+
def ffirst(x: Iterable[Iterable[T]]) -> T | None:
424424
"""
425425
Same as ``first(first(x))``
426426
"""
@@ -440,7 +440,7 @@ def nfirst(x: Iterable[Iterable[T]]) -> Iterable[T]:
440440
return rest(f)
441441

442442

443-
def second(coll: Iterable[T]) -> Optional[T]:
443+
def second(coll: Iterable[T]) -> T | None:
444444
"""
445445
Same as ``first(rest(coll))``.
446446
"""
@@ -473,7 +473,7 @@ def nth(coll: Iterable[T], n: int, not_found: Any = _nil) -> Any:
473473
return not_found
474474

475475

476-
def last(coll: Iterable[T]) -> Optional[T]:
476+
def last(coll: Iterable[T]) -> T | None:
477477
"""
478478
Return the last item in ``coll``, in linear time. Return ``None`` if ``coll`` is empty.
479479
"""
@@ -483,14 +483,14 @@ def last(coll: Iterable[T]) -> Optional[T]:
483483
return e
484484

485485

486-
def zipmap(keys: Iterable[T], vals: Iterable[T2]) -> Dict[T, T2]:
486+
def zipmap(keys: Iterable[T], vals: Iterable[T2]) -> dict[T, T2]:
487487
"""
488488
Return a ``dict`` with the keys mapped to the corresponding ``vals``.
489489
"""
490490
return dict(zip(keys, vals))
491491

492492

493-
def group_by(f: Callable[[T], T2], coll: Iterable[T]) -> Dict[T2, List[T]]:
493+
def group_by(f: Callable[[T], T2], coll: Iterable[T]) -> dict[T2, list[T]]:
494494
"""
495495
Returns a ``dict`` of the elements of ``coll`` keyed by the result of ``f``
496496
on each element. The value at each key will be a list of the corresponding
@@ -503,14 +503,14 @@ def group_by(f: Callable[[T], T2], coll: Iterable[T]) -> Dict[T2, List[T]]:
503503
return dict(groups)
504504

505505

506-
def _make_pred(pred: Union[Callable[[T], T2], Set[T]]) -> Callable[[T], Union[T2, bool]]:
506+
def _make_pred(pred: Union[Callable[[T], T2], set[T]]) -> Callable[[T], Union[T2, bool]]:
507507
if isinstance(pred, set):
508-
return lambda x: x in cast(Set[T], pred)
508+
return lambda x: x in cast(set[T], pred)
509509

510510
return pred
511511

512512

513-
def some(pred: Union[Callable[[T], Any], Set[T]], coll: Iterable[T]) -> Optional[T]:
513+
def some(pred: Union[Callable[[T], Any], set[T]], coll: Iterable[T]) -> T | None:
514514
"""
515515
Returns the first logical true value of ``pred(x)`` for any ``x`` in coll,
516516
else ``None``.
@@ -536,7 +536,7 @@ def is_seq(x):
536536
return isinstance(x, collections_abc.Sequence)
537537

538538

539-
def every(pred: Union[Callable[[T], Any], Set[T]], coll: Iterable[T]) -> bool:
539+
def every(pred: Union[Callable[[T], Any], set[T]], coll: Iterable[T]) -> bool:
540540
"""
541541
Returns ``True`` if ``pred(x)`` is logical true for every ``x`` in
542542
``coll``, else i``False``.
@@ -550,15 +550,15 @@ def every(pred: Union[Callable[[T], Any], Set[T]], coll: Iterable[T]) -> bool:
550550
return True
551551

552552

553-
def not_every(pred: Union[Callable[[T], Any], Set[T]], coll: Iterable[T]) -> bool:
553+
def not_every(pred: Union[Callable[[T], Any], set[T]], coll: Iterable[T]) -> bool:
554554
"""
555555
Returns ``False`` if ``pred(x)`` is logical true for every ``x`` in
556556
``coll``, else ``True``.
557557
"""
558558
return not every(pred, coll)
559559

560560

561-
def not_any(pred: Union[Callable[[T], Any], Set[T]], coll: Iterable[T]) -> bool:
561+
def not_any(pred: Union[Callable[[T], Any], set[T]], coll: Iterable[T]) -> bool:
562562
"""
563563
Return ``False`` if ``pred(x)`` is logical true for any ``x`` in ``coll``,
564564
else ``True``.
@@ -581,7 +581,7 @@ def dorun(coll: Iterable) -> None:
581581
return None
582582

583583

584-
def repeatedly(f: Union[Callable[[], T2], int], n: Optional[Union[int, Callable[[], T2]]] = None) \
584+
def repeatedly(f: Union[Callable[[], T2], int], n: Union[int, Callable[[], T2]] | None = None) \
585585
-> Iterable[T2]:
586586
"""
587587
Takes a function of no args, presumably with side effects, and returns an
@@ -611,7 +611,7 @@ def iterate(f: Callable[[Any], Any], x) -> Iterable:
611611
x = f(x)
612612

613613

614-
def repeat(x: T, n: Optional[int] = None) -> Iterable[T]:
614+
def repeat(x: T, n: int | None = None) -> Iterable[T]:
615615
"""
616616
Returns a generator that indefinitely yields ``x`` (or ``n`` times if ``n`` is supplied).
617617
@@ -688,7 +688,7 @@ def dedupe(coll: Iterable[T]) -> Iterable[T]:
688688
prev = e
689689

690690

691-
def empty(coll: T) -> Optional[T]:
691+
def empty(coll: T) -> T | None:
692692
"""
693693
Returns an empty collection of the same type as ``coll``, or ``None``.
694694
"""
@@ -713,8 +713,8 @@ def count(coll: Iterable) -> int:
713713
return n
714714

715715

716-
def partition(coll: Iterable[T], n: int, step: Optional[int] = None, pad: Optional[Iterable[T2]] = None) \
717-
-> Iterator[List[Union[T, T2]]]:
716+
def partition(coll: Iterable[T], n: int, step: int | None = None, pad: Iterable[T2] | None = None) \
717+
-> Iterator[list[Union[T, T2]]]:
718718
"""
719719
Returns a generator of lists of ``n`` items each, at offsets ``step`` apart. If ``step`` is not supplied, defaults
720720
to ``n``, i.e. the partitions do not overlap. If a ``pad`` collection is supplied, use its elements as necessary to
@@ -733,7 +733,7 @@ def partition(coll: Iterable[T], n: int, step: Optional[int] = None, pad: Option
733733
# TODO
734734
raise NotImplementedError("Step != n is not supported for now.")
735735

736-
current_partition: List[Union[T, T2]] = []
736+
current_partition: list[Union[T, T2]] = []
737737
partition_index = 0
738738
partition_end = n
739739

@@ -755,8 +755,8 @@ def partition(coll: Iterable[T], n: int, step: Optional[int] = None, pad: Option
755755
yield current_partition
756756

757757

758-
def partition_by(f: Callable[[T], Any], coll: Iterable[T]) -> Iterable[List[T]]:
759-
current: List[T] = []
758+
def partition_by(f: Callable[[T], Any], coll: Iterable[T]) -> Iterable[list[T]]:
759+
current: list[T] = []
760760
current_value = None
761761
for element in coll:
762762
if not current:
@@ -777,7 +777,7 @@ def partition_by(f: Callable[[T], Any], coll: Iterable[T]) -> Iterable[List[T]]:
777777
yield current
778778

779779

780-
def seq_gen(coll: Iterable[T]) -> Optional[Iterable[T]]:
780+
def seq_gen(coll: Iterable[T]) -> Iterable[T] | None:
781781
"""
782782
Like Clojure’s ``seq``, but return a lazy iterable that’s equivalent to ``coll`` if not empty.
783783
@@ -789,5 +789,5 @@ def seq_gen(coll: Iterable[T]) -> Optional[Iterable[T]]:
789789
"""
790790
first_element, _is_empty = _first(coll)
791791
if _is_empty:
792-
return
792+
return None
793793
return clj.concat([first_element], _iter(coll, 1))

tests/test_seqs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_flatten_infinite_generators():
246246

247247

248248
def test_flatten_deep_list():
249-
deep_list = ["foo"]
249+
deep_list: list[Any] = ["foo"]
250250
for _ in range(200):
251251
deep_list = [[[[[deep_list]]]]]
252252

0 commit comments

Comments
 (0)