Skip to content

Commit 2132ab4

Browse files
authored
Pyright upgrade (#36)
- Typing cleanup
1 parent 849af4c commit 2132ab4

14 files changed

+79
-113
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ repos:
2626
language: node
2727
pass_filenames: false
2828
types: [python]
29-
additional_dependencies: ["[email protected].127"]
29+
additional_dependencies: ["[email protected].158"]
3030
repo: local

expression/collections/asyncseq.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ async def repeat(value: TSource, times: Optional[int] = None) -> AsyncIterable[T
6363

6464

6565
@overload
66-
async def range(stop: int) -> AsyncIterable[int]:
66+
def range(stop: int) -> AsyncIterable[int]:
6767
...
6868

6969

7070
@overload
71-
async def range(start: int, stop: int) -> AsyncIterable[int]:
71+
def range(start: int, stop: int) -> AsyncIterable[int]:
7272
...
7373

7474

7575
@overload
76-
async def range(start: int, stop: int, step: int) -> AsyncIterable[int]:
76+
def range(start: int, stop: int, step: int) -> AsyncIterable[int]:
7777
...
7878

7979

80-
async def range(*args: Any, **kw: Any) -> AsyncIterable[Any]:
80+
async def range(*args: int, **kw: int) -> AsyncIterable[int]:
8181
for value in builtins.range(*args, **kw):
8282
yield value
8383

expression/collections/frozenlist.py

+9-24
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
Iterator,
3131
List,
3232
Optional,
33-
Protocol,
3433
Tuple,
3534
TypeVar,
3635
cast,
@@ -438,8 +437,8 @@ def __getitem__(self, key: Any) -> Any:
438437
def __iter__(self) -> Iterator[TSource]:
439438
return iter(self.value)
440439

441-
def __eq__(self, other: Any) -> bool:
442-
return self.value == other
440+
def __eq__(self, o: Any) -> bool:
441+
return self.value == o
443442

444443
def __len__(self) -> int:
445444
return len(self.value)
@@ -464,20 +463,6 @@ def __repr__(self) -> str:
464463
return str(self)
465464

466465

467-
class Cata(Protocol[TSourceIn, TResultOut]):
468-
"""A partially applied exit function."""
469-
470-
def __call__(self, source: FrozenList[TSourceIn]) -> TResultOut:
471-
...
472-
473-
474-
class Projection(Protocol[TSourceIn, TResultOut]):
475-
"""A partially applied filter function."""
476-
477-
def __call__(self, __source: FrozenList[TSourceIn]) -> FrozenList[TResultOut]:
478-
...
479-
480-
481466
def append(source: FrozenList[TSource]) -> Callable[[FrozenList[TSource]], FrozenList[TSource]]:
482467
def _append(other: FrozenList[TSource]) -> FrozenList[TSource]:
483468
return source.append(other)
@@ -634,7 +619,7 @@ def indexed(source: FrozenList[TSource]) -> FrozenList[Tuple[int, TSource]]:
634619
return source.indexed()
635620

636621

637-
def item(index: int) -> Cata[TSource, TSource]:
622+
def item(index: int) -> Callable[[FrozenList[TSource]], TSource]:
638623
"""Indexes into the list. The first element has index 0.
639624
640625
Args:
@@ -655,7 +640,7 @@ def is_empty(source: FrozenList[Any]) -> bool:
655640
return source.is_empty()
656641

657642

658-
def map(mapper: Callable[[TSource], TResult]) -> Projection[TSource, TResult]:
643+
def map(mapper: Callable[[TSource], TResult]) -> Callable[[FrozenList[TSource]], FrozenList[TResult]]:
659644
"""Map list.
660645
661646
Builds a new collection whose elements are the results of applying
@@ -674,7 +659,7 @@ def _map(source: FrozenList[TSource]) -> FrozenList[TResult]:
674659
return _map
675660

676661

677-
def mapi(mapper: Callable[[int, TSource], TResult]) -> Projection[TSource, TResult]:
662+
def mapi(mapper: Callable[[int, TSource], TResult]) -> Callable[[FrozenList[TSource]], FrozenList[TResult]]:
678663
"""Map list with index.
679664
680665
Builds a new collection whose elements are the results of
@@ -735,7 +720,7 @@ def singleton(value: TSource) -> FrozenList[TSource]:
735720
return FrozenList((value,))
736721

737722

738-
def skip(count: int) -> Projection[TSource, TSource]:
723+
def skip(count: int) -> Callable[[FrozenList[TSource]], FrozenList[TSource]]:
739724
"""Returns the list after removing the first N elements.
740725
741726
Args:
@@ -751,7 +736,7 @@ def _skip(source: FrozenList[TSource]) -> FrozenList[TSource]:
751736
return _skip
752737

753738

754-
def skip_last(count: int) -> Projection[TSource, TSource]:
739+
def skip_last(count: int) -> Callable[[FrozenList[TSource]], FrozenList[TSource]]:
755740
"""Returns the list after removing the last N elements.
756741
757742
Args:
@@ -771,7 +756,7 @@ def tail(source: FrozenList[TSource]) -> FrozenList[TSource]:
771756
return source.tail()
772757

773758

774-
def take(count: int) -> Projection[TSource, TSource]:
759+
def take(count: int) -> Callable[[FrozenList[TSource]], FrozenList[TSource]]:
775760
"""Returns the first N elements of the list.
776761
777762
Args:
@@ -787,7 +772,7 @@ def _take(source: FrozenList[TSource]) -> FrozenList[TSource]:
787772
return _take
788773

789774

790-
def take_last(count: int) -> Projection[TSource, TSource]:
775+
def take_last(count: int) -> Callable[[FrozenList[TSource]], FrozenList[TSource]]:
791776
"""Returns a specified number of contiguous elements from the end of
792777
the list.
793778

expression/collections/map.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ def combine_hash(x: int, y: int) -> int:
248248
res = combine_hash(res, hash(y))
249249
return res
250250

251-
def __getitem__(self, key: Key) -> Value:
252-
return maptree.find(key, self._tree)
251+
def __getitem__(self, k: Key) -> Value:
252+
return maptree.find(k, self._tree)
253253

254254
def __iter__(self) -> Iterator[Key]:
255255
xs = maptree.mk_iterator(self._tree)
@@ -259,14 +259,14 @@ def __len__(self) -> int:
259259
"""Return the number of bindings in the map."""
260260
return maptree.size(self._tree)
261261

262-
def __contains__(self, key: Any) -> bool:
263-
return self.contains_key(key)
262+
def __contains__(self, o: Any) -> bool:
263+
return self.contains_key(o)
264264

265-
def __eq__(self, other: Any) -> bool:
266-
if not isinstance(other, Map):
265+
def __eq__(self, o: Any) -> bool:
266+
if not isinstance(o, Map):
267267
return False
268268

269-
other = cast(Map[Any, Any], other)
269+
other = cast(Map[Any, Any], o)
270270
iterator: Iterator[Tuple[Any, Any]] = iter(other.to_seq())
271271

272272
for kv in self.to_seq():

expression/collections/seq.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import builtins
2727
import functools
2828
import itertools
29-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, Optional, Protocol, Tuple, TypeVar, cast, overload
29+
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, Optional, Tuple, TypeVar, cast, overload
3030

3131
from expression.core import Case, Option, SupportsLessThan, identity, pipe
3232

@@ -350,18 +350,7 @@ def __iter__(self) -> Iterator[TSource]:
350350
return builtins.iter(xs)
351351

352352

353-
class Projection(Protocol[TSourceIn, TResultOut]):
354-
"""Sequence transformation protocol.
355-
356-
A sequence transformation protocol that encapsulates a function of
357-
type `Iterable[TSource]) -> Iterable[TResult]`
358-
"""
359-
360-
def __call__(self, __source: Iterable[TSourceIn]) -> Iterable[TResultOut]:
361-
raise NotImplementedError
362-
363-
364-
def append(*others: Iterable[TSource]) -> Projection[TSource, TSource]:
353+
def append(*others: Iterable[TSource]) -> Callable[[Iterable[TSource]], Iterable[TSource]]:
365354
"""Wraps the given enumerations as a single concatenated
366355
enumeration."""
367356

@@ -371,7 +360,7 @@ def _(source: Iterable[TSource]) -> Iterable[TSource]:
371360
return _
372361

373362

374-
def choose(chooser: Callable[[TSource], Option[TResult]]) -> Projection[TSource, TResult]:
363+
def choose(chooser: Callable[[TSource], Option[TResult]]) -> Callable[[Iterable[TSource]], Iterable[TResult]]:
375364
"""Choose items from the sequence.
376365
377366
Applies the given function to each element of the list. Returns
@@ -395,7 +384,7 @@ def mapper(x: TSource) -> Iterable[TResult]:
395384
return _choose
396385

397386

398-
def collect(mapping: Callable[[TSource], Iterable[TResult]]) -> Projection[TSource, TResult]:
387+
def collect(mapping: Callable[[TSource], Iterable[TResult]]) -> Callable[[Iterable[TSource]], Iterable[TResult]]:
399388
def _collect(source: Iterable[TSource]) -> Iterable[TResult]:
400389
def gen():
401390
for xs in source:
@@ -444,7 +433,7 @@ def delay(generator: Callable[[], Iterable[TSource]]) -> Iterable[TSource]:
444433
"""The empty sequence."""
445434

446435

447-
def filter(predicate: Callable[[TSource], bool]) -> Projection[TSource, TSource]:
436+
def filter(predicate: Callable[[TSource], bool]) -> Callable[[Iterable[TSource]], Iterable[TSource]]:
448437
"""Filter sequence.
449438
450439
Filters the sequence to a new sequence containing only the
@@ -609,7 +598,7 @@ def length(source: Seq[Any]) -> int:
609598
return builtins.sum(1 for _ in source)
610599

611600

612-
def map(mapper: Callable[[TSource], TResult]) -> Projection[TSource, TResult]:
601+
def map(mapper: Callable[[TSource], TResult]) -> Callable[[Iterable[TSource]], Iterable[TResult]]:
613602
"""Map source sequence.
614603
615604
Builds a new collection whose elements are the results of
@@ -641,7 +630,7 @@ def gen():
641630
return _map
642631

643632

644-
def mapi(mapping: Callable[[int, TSource], TResult]) -> Projection[TSource, TResult]:
633+
def mapi(mapping: Callable[[int, TSource], TResult]) -> Callable[[Iterable[TSource]], Iterable[TResult]]:
645634
"""Map list with index.
646635
647636
Builds a new collection whose elements are the results of
@@ -766,7 +755,7 @@ def singleton(item: TSource) -> Seq[TSource]:
766755
return Seq([item])
767756

768757

769-
def skip(count: int) -> Projection[Any, Any]:
758+
def skip(count: int) -> Callable[[Iterable[TSource]], Iterable[TSource]]:
770759
"""Returns a sequence that skips N elements of the underlying
771760
sequence and then yields the remaining elements of the sequence.
772761
@@ -809,7 +798,7 @@ def tail(source: Iterable[TSource]) -> Iterable[TSource]:
809798
return proj(source)
810799

811800

812-
def take(count: int) -> Projection[Any, Any]:
801+
def take(count: int) -> Callable[[Iterable[TSource]], Iterable[TSource]]:
813802
"""Returns the first N elements of the sequence.
814803
815804
Args:
@@ -932,7 +921,6 @@ def _zip(source2: Iterable[TResult]) -> Iterable[Tuple[TSource, TResult]]:
932921
"sum_by",
933922
"tail",
934923
"take",
935-
"Projection",
936924
"unfold",
937925
"zip",
938926
]

expression/core/aiotools.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def cancel(_: OperationCanceledError) -> None:
4444
future.cancel()
4545

4646
callback(done, error, cancel)
47-
return asyncio.ensure_future(future)
47+
return future
4848

4949

5050
def start(computation: Awaitable[Any], token: Optional[CancellationToken] = None) -> None:
@@ -66,6 +66,8 @@ def cb():
6666

6767

6868
def start_immediate(computation: Awaitable[Any], token: Optional[CancellationToken] = None) -> None:
69+
"""Runs an asynchronous computation, starting immediately on the
70+
current operating system thread."""
6971
task = asyncio.create_task(computation)
7072

7173
def cb() -> None:
@@ -99,6 +101,9 @@ async def empty() -> None:
99101

100102

101103
def from_result(result: TSource) -> Awaitable[TSource]:
104+
"""Creates a async operation that's completed successfully with the
105+
specified result."""
106+
102107
async def from_result(result: TSource) -> TSource:
103108
"""Async return value"""
104109
return result

expression/core/choice.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def match(cls, case: Case[A_]) -> Iterable[A_]:
6969
def match(cls, case: Any) -> Iterable[Any]:
7070
return case(cls)
7171

72-
def __eq__(self, other: Any) -> bool:
73-
if isinstance(other, Choice1of2):
74-
return self.value == other.value
72+
def __eq__(self, o: Any) -> bool:
73+
if isinstance(o, Choice1of2):
74+
return self.value == o.value
7575
return False
7676

7777
def __str__(self) -> str:
@@ -103,9 +103,9 @@ def match(cls, case: Case[B_]) -> Iterable[B_]:
103103
def match(cls, case: Any) -> Iterable[Any]:
104104
return case(cls)
105105

106-
def __eq__(self, other: Any) -> bool:
107-
if isinstance(other, Choice2of2):
108-
return self.value == other.value
106+
def __eq__(self, o: Any) -> bool:
107+
if isinstance(o, Choice2of2):
108+
return self.value == o.value
109109
return False
110110

111111
def __str__(self) -> str:

0 commit comments

Comments
 (0)