Skip to content

Commit 38f8e7e

Browse files
authored
Update type hints to pyright 1.1.96 (#13)
* Update type hints to pyright 1.1.96 * Rename TransformFn protocol to Projection
1 parent 00979ab commit 38f8e7e

File tree

18 files changed

+82
-89
lines changed

18 files changed

+82
-89
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ repos:
2626
language: node
2727
pass_filenames: false
2828
types: [python]
29-
additional_dependencies: ['[email protected].95']
29+
additional_dependencies: ['[email protected].96']
3030
repo: local
3131

expression/collections/frozenlist.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,14 @@ def __repr__(self) -> str:
432432
return str(self)
433433

434434

435-
class ExitFn(Protocol):
435+
class Cata(Protocol[TSource, TResult]):
436436
"""A partially applied exit function."""
437437

438-
def __call__(self, source: FrozenList[TSource]) -> TSource:
438+
def __call__(self, source: FrozenList[TSource]) -> TResult:
439439
...
440440

441441

442-
class FilterFn(Protocol):
443-
"""A partially applied filter function."""
444-
445-
def __call__(self, __source: FrozenList[TSource]) -> FrozenList[TSource]:
446-
...
447-
448-
449-
class TransformFn(Protocol[TResult]):
442+
class Projection(Protocol[TSource, TResult]):
450443
"""A partially applied filter function."""
451444

452445
def __call__(self, __source: FrozenList[TSource]) -> FrozenList[TResult]:
@@ -592,7 +585,7 @@ def indexed(source: FrozenList[TSource]) -> FrozenList[Tuple[int, TSource]]:
592585
return source.indexed()
593586

594587

595-
def item(index: int) -> ExitFn:
588+
def item(index: int) -> Cata[TSource, TSource]:
596589
"""Indexes into the list. The first element has index 0.
597590
598591
Args:
@@ -608,12 +601,12 @@ def _item(source: FrozenList[TSource]) -> TSource:
608601
return _item
609602

610603

611-
def is_empty(source: FrozenList[TSource]) -> bool:
604+
def is_empty(source: FrozenList[Any]) -> bool:
612605
"""Returns `True` if the list is empty, `False` otherwise."""
613606
return source.is_empty()
614607

615608

616-
def map(mapper: Callable[[TSource], TResult]) -> TransformFn[TResult]:
609+
def map(mapper: Callable[[TSource], TResult]) -> Projection[TSource, TResult]:
617610
"""Map list.
618611
619612
Builds a new collection whose elements are the results of applying
@@ -671,7 +664,7 @@ def singleton(value: TSource) -> FrozenList[TSource]:
671664
return FrozenList((value,))
672665

673666

674-
def skip(count: int) -> FilterFn:
667+
def skip(count: int) -> Projection[TSource, TSource]:
675668
"""Returns the list after removing the first N elements.
676669
677670
Args:
@@ -687,7 +680,7 @@ def _skip(source: FrozenList[TSource]) -> FrozenList[TSource]:
687680
return _skip
688681

689682

690-
def skip_last(count: int) -> FilterFn:
683+
def skip_last(count: int) -> Projection[TSource, TSource]:
691684
"""Returns the list after removing the last N elements.
692685
693686
Args:
@@ -707,7 +700,7 @@ def tail(source: FrozenList[TSource]) -> FrozenList[TSource]:
707700
return source.tail()
708701

709702

710-
def take(count: int) -> FilterFn:
703+
def take(count: int) -> Projection[TSource, TSource]:
711704
"""Returns the first N elements of the list.
712705
713706
Args:
@@ -723,7 +716,7 @@ def _take(source: FrozenList[TSource]) -> FrozenList[TSource]:
723716
return _take
724717

725718

726-
def take_last(count: int) -> FilterFn:
719+
def take_last(count: int) -> Projection[TSource, TSource]:
727720
"""Returns a specified number of contiguous elements from the end of
728721
the list.
729722

expression/collections/map.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ def _change(table: Map[Key, Value]) -> Map[Key, Value]:
351351
return _change
352352

353353

354-
def contains_key(key: Key) -> Callable[[Map[Key, Value]], bool]:
355-
def _contains_key(table: Map[Key, Value]) -> bool:
354+
def contains_key(key: Key) -> Callable[[Map[Key, Any]], bool]:
355+
def _contains_key(table: Map[Key, Any]) -> bool:
356356
return table.contains_key(key)
357357

358358
return _contains_key
359359

360360

361-
def count(table: Map[Key, Value]) -> int:
361+
def count(table: Map[Any, Any]) -> int:
362362
"""Return the number of bindings in the map."""
363363
return len(table)
364364

@@ -383,7 +383,7 @@ def _find(table: Map[Key, Value]) -> Value:
383383
return _find
384384

385385

386-
def is_empty(table: Map[Key, Value]) -> bool:
386+
def is_empty(table: Map[Any, Any]) -> bool:
387387
"""Is the map empty?
388388
389389
Args:
@@ -558,7 +558,7 @@ def try_find(key: Key) -> Callable[[Map[Key, Value]], Option[Value]]:
558558
instance and returns the result.
559559
"""
560560

561-
def _try_find(table: Map[Key, Value]):
561+
def _try_find(table: Map[Key, Value]) -> Option[Value]:
562562
"""Lookup an element in the map, returning a `Some` value if the
563563
element is in the domain of the map and `Nothing` if not.
564564

expression/collections/maptree.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class MapTreeNode(MapTreeLeaf[Key, Value]):
5656
empty: MapTree[Any, Any] = Nothing
5757

5858

59-
def is_empty(m: MapTree[Key, Value]):
59+
def is_empty(m: MapTree[Any, Any]):
6060
return m.is_none()
6161

6262

63-
def size_aux(acc: int, m: MapTree[Key, Value]) -> int:
63+
def size_aux(acc: int, m: MapTree[Any, Any]) -> int:
6464
for m2 in m.to_list():
6565
if isinstance(m2, MapTreeNode):
6666
mn = cast(MapTreeNode[Key, Value], m2)
@@ -71,11 +71,11 @@ def size_aux(acc: int, m: MapTree[Key, Value]) -> int:
7171
return acc
7272

7373

74-
def size(x: MapTree[Key, Value]):
74+
def size(x: MapTree[Any, Any]):
7575
return size_aux(0, x)
7676

7777

78-
def height(m: MapTree[Key, Value]) -> int:
78+
def height(m: MapTree[Any, Any]) -> int:
7979
for m2 in m.to_list():
8080
if isinstance(m2, MapTreeNode):
8181
mn = cast(MapTreeNode[Key, Value], m2)
@@ -324,7 +324,7 @@ def change(k: Key, u: Callable[[Option[Value]], Option[Value]], m: MapTree[Key,
324324
return m
325325

326326

327-
def mem(k: Key, m: MapTree[Key, Value]) -> bool:
327+
def mem(k: Key, m: MapTree[Key, Any]) -> bool:
328328
for m2 in m.to_list():
329329
if isinstance(m2, MapTreeNode):
330330
mn = cast(MapTreeNode[Key, Value], m2)

expression/collections/seq.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,27 +231,18 @@ def __iter__(self) -> Iterator[TSource]:
231231
return builtins.iter(self._value)
232232

233233

234-
class FilterFn(Protocol):
235-
"""Sequence filtering protocol function.
234+
class Projection(Protocol[TSource, TResult]):
235+
"""Sequence transformation protocol.
236236
237-
`Iterable[TSource]) -> Iterable[TSource]`
238-
"""
239-
240-
def __call__(self, __source: Iterable[TSource]) -> Iterable[TSource]:
241-
raise NotImplementedError
242-
243-
244-
class TransformFn(Protocol[TResult]):
245-
"""Sequence transforming protocol function.
246-
247-
`Iterable[TSource]) -> Iterable[TResult]`
237+
A sequence transformation protocol that encapsulates a function of
238+
type `Iterable[TSource]) -> Iterable[TResult]`
248239
"""
249240

250241
def __call__(self, __source: Iterable[TSource]) -> Iterable[TResult]:
251242
raise NotImplementedError
252243

253244

254-
def choose(chooser: Callable[[TSource], Option[TResult]]) -> TransformFn[TResult]:
245+
def choose(chooser: Callable[[TSource], Option[TResult]]) -> Projection[TSource, TResult]:
255246
"""Choose items from the sequence.
256247
257248
Applies the given function to each element of the list. Returns
@@ -275,7 +266,7 @@ def mapper(x: TSource) -> Iterable[TResult]:
275266
return _choose
276267

277268

278-
def collect(mapping: Callable[[TSource], Iterable[TResult]]) -> TransformFn[TResult]:
269+
def collect(mapping: Callable[[TSource], Iterable[TResult]]) -> Projection[TSource, TResult]:
279270
def _collect(source: Iterable[TSource]) -> Iterable[TResult]:
280271
return (x for xs in source for x in mapping(xs))
281272

@@ -303,7 +294,7 @@ def concat(*iterables: Iterable[TSource]) -> Iterable[TSource]:
303294
"""The empty sequence."""
304295

305296

306-
def filter(predicate: Callable[[TSource], bool]) -> FilterFn:
297+
def filter(predicate: Callable[[TSource], bool]) -> Projection[TSource, TSource]:
307298
"""Filter sequence.
308299
309300
Filters the sequence to a new sequence containing only the
@@ -463,7 +454,7 @@ def _iter(source: Iterable[TSource]) -> None:
463454
return _iter
464455

465456

466-
def map(mapper: Callable[[TSource], TResult]) -> TransformFn[TResult]:
457+
def map(mapper: Callable[[TSource], TResult]) -> Projection[TSource, TResult]:
467458
"""Map source sequence.
468459
469460
Builds a new collection whose elements are the results of
@@ -593,7 +584,7 @@ def singleton(item: TSource) -> Seq[TSource]:
593584
return Seq([item])
594585

595586

596-
def take(count: int) -> FilterFn:
587+
def take(count: int) -> Projection[Any, Any]:
597588
"""Returns the first N elements of the sequence.
598589
599590
Args:
@@ -709,6 +700,7 @@ def _zip(source2: Iterable[TResult]) -> Iterable[Tuple[TSource, TResult]]:
709700
"scan",
710701
"singleton",
711702
"take",
703+
"Projection",
712704
"unfold",
713705
"zip",
714706
]

expression/core/choice.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def __init__(self, value: A) -> None:
5050

5151
@overload
5252
@classmethod
53-
def match(cls, case: Case[Choice2[A_, B_]]) -> Iterable[A_]:
53+
def match(cls, case: Case[Choice2[A_, Any]]) -> Iterable[A_]:
5454
"""Helper to cast the match result to correct type."""
5555
...
5656

5757
@overload
5858
@classmethod
59-
def match(cls, case: "Case[Choice1of2[A_, B_]]") -> Iterable[A_]:
59+
def match(cls, case: "Case[Choice1of2[A_, Any]]") -> Iterable[A_]:
6060
"""Helper to cast the match result to correct type."""
6161
...
6262

@@ -84,13 +84,13 @@ def __init__(self, value: B) -> None:
8484

8585
@overload
8686
@classmethod
87-
def match(cls, case: Case[Choice2[A_, B_]]) -> Iterable[B_]:
87+
def match(cls, case: Case[Choice2[Any, B_]]) -> Iterable[B_]:
8888
"""Helper to cast the match result to correct type."""
8989
...
9090

9191
@overload
9292
@classmethod
93-
def match(cls, case: "Case[Choice1of2[A_, B_]]") -> Iterable[B_]:
93+
def match(cls, case: "Case[Choice1of2[Any, B_]]") -> Iterable[B_]:
9494
"""Helper to cast the match result to correct type."""
9595
...
9696

expression/core/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def _flip(b: B, a: A) -> Any:
3333
return _flip
3434

3535

36-
def snd(value: Tuple[A, B]) -> B:
36+
def snd(value: Tuple[Any, B]) -> B:
3737
"""Return second argument of the tuple."""
3838

3939
_, b = value
4040
return b
4141

4242

43-
def fst(value: Tuple[A, B]) -> A:
43+
def fst(value: Tuple[A, Any]) -> A:
4444
"""Return first argument of the tuple."""
4545

4646
a, _ = value

expression/core/option.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def is_none(self) -> bool:
174174
"""Returns `False`."""
175175
return False
176176

177-
def map(self, mapper: Callable[[TSource], TResult]):
177+
def map(self, mapper: Callable[[TSource], TResult]) -> Option[TResult]:
178178
return Some(mapper(self._value))
179179

180180
def map2(self, mapper: Callable[[TSource, T2], TResult], other: Option[T2]) -> Option[TResult]:
@@ -343,7 +343,7 @@ def __str__(self):
343343
return "Nothing"
344344

345345

346-
class TransformFn(Protocol[TResult]):
346+
class Projection(Protocol[TSource, TResult]):
347347
"""Option transforming protocol function.
348348
349349
`Option[TSource]) -> Option[TResult]`
@@ -365,7 +365,7 @@ def __call__(self, __source: Option[TSource]) -> Option[TResult]:
365365
"""
366366

367367

368-
def bind(mapper: Callable[[TSource], Option[TResult]]) -> TransformFn[TResult]:
368+
def bind(mapper: Callable[[TSource], Option[TResult]]) -> Projection[TSource, TResult]:
369369
"""Bind option.
370370
371371
Applies and returns the result of the mapper if the value is
@@ -398,15 +398,15 @@ def _default_value(option: Option[TSource]) -> TSource:
398398
return _default_value
399399

400400

401-
def is_none(option: Option[TSource]) -> bool:
401+
def is_none(option: Option[Any]) -> bool:
402402
return option.is_none()
403403

404404

405-
def is_some(option: Option[TSource]) -> bool:
405+
def is_some(option: Option[Any]) -> bool:
406406
return option.is_some()
407407

408408

409-
def map(mapper: Callable[[TSource], TResult]) -> TransformFn[TResult]:
409+
def map(mapper: Callable[[TSource], TResult]) -> Projection[TSource, TResult]:
410410
def _map(option: Option[TSource]) -> Option[TResult]:
411411
return option.map(mapper)
412412

@@ -485,4 +485,5 @@ def of_obj(value: Any) -> Option[Any]:
485485
"to_seq",
486486
"of_optional",
487487
"of_obj",
488+
"Projection",
488489
]

expression/core/pipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def pipe(
9696
__fn6: Callable[[F], G],
9797
__fn7: Callable[[G], H],
9898
__fn8: Callable[[H], T],
99-
) -> H:
99+
) -> T:
100100
...
101101

102102

@@ -146,7 +146,7 @@ def pipe2(values: Tuple[A, B], __fn1: Callable[[A, B], C], __fn2: Callable[[C],
146146

147147

148148
@overload
149-
def pipe2(values: Tuple[A, B], __fn1: Callable[[A, B], C], __fn2: Callable[[C], D], __fn3: Callable[[D], E]) -> D:
149+
def pipe2(values: Tuple[A, B], __fn1: Callable[[A, B], C], __fn2: Callable[[C], D], __fn3: Callable[[D], E]) -> E:
150150
...
151151

152152

0 commit comments

Comments
 (0)