Skip to content

Commit 5b043db

Browse files
committedMay 15, 2024·
Fix typing issues
1 parent 4031501 commit 5b043db

File tree

9 files changed

+84
-83
lines changed

9 files changed

+84
-83
lines changed
 

‎.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ repos:
1414
language: node
1515
pass_filenames: false
1616
types: [python]
17-
additional_dependencies: ["pyright@1.1.353"]
17+
additional_dependencies: ["pyright@1.1.363"]
1818
repo: local

‎expression/collections/block.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def of(*args: _TSource) -> Block[_TSource]:
280280
return Block((*args,))
281281

282282
@staticmethod
283-
def of_seq(xs: Iterable[_TSource]) -> Block[_TSource]:
283+
def of_seq(xs: Iterable[_TResult]) -> Block[_TResult]:
284284
"""Create list from iterable sequence."""
285285
return Block((*xs,))
286286

‎expression/collections/map.py

+71-70
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,52 @@
2626
from .maptree import MapTree
2727

2828

29-
Key = TypeVar("Key", bound=SupportsLessThan)
30-
Value = TypeVar("Value")
31-
Result = TypeVar("Result")
29+
_Key = TypeVar("_Key", bound=SupportsLessThan)
30+
_Key_ = TypeVar("_Key_", bound=SupportsLessThan)
31+
_Value = TypeVar("_Value")
32+
_Result = TypeVar("_Result")
3233

33-
T1 = TypeVar("T1")
34-
T2 = TypeVar("T2")
35-
T3 = TypeVar("T3")
36-
T4 = TypeVar("T4")
37-
T5 = TypeVar("T5")
38-
T6 = TypeVar("T6")
34+
_T1 = TypeVar("_T1")
35+
_T2 = TypeVar("_T2")
36+
_T3 = TypeVar("_T3")
37+
_T4 = TypeVar("_T4")
38+
_T5 = TypeVar("_T5")
39+
_T6 = TypeVar("_T6")
3940

4041

41-
class Map(Mapping[Key, Value], PipeMixin):
42+
class Map(Mapping[_Key, _Value], PipeMixin):
4243
"""The immutable map class."""
4344

44-
def __init__(self, __tree: MapTree[Key, Value] | None = None) -> None:
45-
self._tree: MapTree[Key, Value] = __tree if __tree else maptree.empty
45+
def __init__(self, __tree: MapTree[_Key, _Value] | None = None) -> None:
46+
self._tree: MapTree[_Key, _Value] = __tree if __tree else maptree.empty
4647

47-
def add(self, key: Key, value: Value) -> Map[Key, Value]:
48+
def add(self, key: _Key, value: _Value) -> Map[_Key, _Value]:
4849
return Map(maptree.add(key, value, self._tree))
4950

5051
@staticmethod
51-
def create(ie: Iterable[tuple[Key, Value]]) -> Map[Key, Value]:
52+
def create(ie: Iterable[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
5253
return create(ie)
5354

54-
def contains_key(self, key: Key) -> bool:
55+
def contains_key(self, key: _Key) -> bool:
5556
return maptree.mem(key, self._tree)
5657

57-
def change(self, key: Key, f: Callable[[Option[Value]], Option[Value]]) -> Map[Key, Value]:
58+
def change(self, key: _Key, f: Callable[[Option[_Value]], Option[_Value]]) -> Map[_Key, _Value]:
5859
return Map(maptree.change(key, f, self._tree))
5960

6061
@staticmethod
61-
def empty() -> Map[Key, Value]:
62+
def empty() -> Map[_Key, _Value]:
6263
return Map(maptree.empty)
6364

6465
def is_empty(self) -> bool:
6566
return maptree.is_empty(self._tree)
6667

67-
def exists(self, predicate: Callable[[Key, Value], bool]) -> bool:
68+
def exists(self, predicate: Callable[[_Key, _Value], bool]) -> bool:
6869
return maptree.exists(predicate, self._tree)
6970

70-
def filter(self, predicate: Callable[[Key, Value], bool]) -> Map[Key, Value]:
71+
def filter(self, predicate: Callable[[_Key, _Value], bool]) -> Map[_Key, _Value]:
7172
return Map(maptree.filter(predicate, self._tree))
7273

73-
def for_all(self, predicate: Callable[[Key, Value], bool]) -> bool:
74+
def for_all(self, predicate: Callable[[_Key, _Value], bool]) -> bool:
7475
"""Test all elements in map.
7576
7677
Returns true if the given predicate returns true for all of
@@ -85,19 +86,19 @@ def for_all(self, predicate: Callable[[Key, Value], bool]) -> bool:
8586
"""
8687
return maptree.forall(predicate, self._tree)
8788

88-
def iterate(self, f: Callable[[Key, Value], None]) -> None:
89+
def iterate(self, f: Callable[[_Key, _Value], None]) -> None:
8990
return maptree.iter(f, self._tree)
9091

9192
# def MapRange (f:'Value->'Result) =
9293
# return Map<'Key, 'Result>(comparer, maptree.map f tree)
9394

94-
def fold(self, folder: Callable[[Result, tuple[Key, Value]], Result], state: Result) -> Result:
95+
def fold(self, folder: Callable[[_Result, tuple[_Key, _Value]], _Result], state: _Result) -> _Result:
9596
return maptree.fold(folder, state, self._tree)
9697

97-
def fold_back(self, folder: Callable[[tuple[Key, Value], Result], Result], state: Result) -> Result:
98+
def fold_back(self, folder: Callable[[tuple[_Key, _Value], _Result], _Result], state: _Result) -> _Result:
9899
return maptree.fold_back(folder, self._tree, state)
99100

100-
def map(self, mapping: Callable[[Key, Value], Result]) -> Map[Key, Result]:
101+
def map(self, mapping: Callable[[_Key, _Value], _Result]) -> Map[_Key, _Result]:
101102
"""Map the mapping.
102103
103104
Builds a new collection whose elements are the results of
@@ -113,7 +114,7 @@ def map(self, mapping: Callable[[Key, Value], Result]) -> Map[Key, Result]:
113114
"""
114115
return Map(maptree.map(mapping, self._tree))
115116

116-
def partition(self, predicate: Callable[[Key, Value], bool]) -> tuple[Map[Key, Value], Map[Key, Value]]:
117+
def partition(self, predicate: Callable[[_Key, _Value], bool]) -> tuple[Map[_Key, _Value], Map[_Key, _Value]]:
117118
r1, r2 = maptree.partition(predicate, self._tree)
118119
return Map(r1), Map(r2)
119120

@@ -131,43 +132,43 @@ def partition(self, predicate: Callable[[Key, Value], bool]) -> tuple[Map[Key, V
131132

132133
# return default
133134

134-
def items(self) -> ItemsView[Key, Value]:
135+
def items(self) -> ItemsView[_Key, _Value]:
135136
items = maptree.to_seq(self._tree)
136137
return ItemsView(dict(items))
137138

138-
def remove(self, key: Key) -> Map[Key, Value]:
139+
def remove(self, key: _Key) -> Map[_Key, _Value]:
139140
return Map(maptree.remove(key, self._tree))
140141

141-
def to_list(self) -> Block[tuple[Key, Value]]:
142+
def to_list(self) -> Block[tuple[_Key, _Value]]:
142143
return maptree.to_list(self._tree)
143144

144-
def to_seq(self) -> Iterable[tuple[Key, Value]]:
145+
def to_seq(self) -> Iterable[tuple[_Key, _Value]]:
145146
"""Convert to sequence.
146147
147148
Returns:
148149
Sequence of key, value tuples.
149150
"""
150151
return maptree.to_seq(self._tree)
151152

152-
def try_get_value(self, key: Key, value: list[Value]):
153+
def try_get_value(self, key: _Key, value: list[_Value]):
153154
for v in maptree.try_find(key, self._tree).to_list():
154155
value.append(v)
155156
return True
156157
else:
157158
return False
158159

159-
def try_find(self, key: Key) -> Option[Value]:
160+
def try_find(self, key: _Key) -> Option[_Value]:
160161
return maptree.try_find(key, self._tree)
161162

162-
def try_pick(self, chooser: Callable[[Key, Value], Option[Result]]) -> Option[Result]:
163+
def try_pick(self, chooser: Callable[[_Key, _Value], Option[_Result]]) -> Option[_Result]:
163164
return maptree.try_pick(chooser, self._tree)
164165

165166
@staticmethod
166-
def of(**args: Value) -> Map[str, Value]:
167+
def of(**args: _Result) -> Map[str, _Result]:
167168
return Map(maptree.of_seq(args.items()))
168169

169170
@staticmethod
170-
def of_block(lst: Block[tuple[Key, Value]]) -> Map[Key, Value]:
171+
def of_block(lst: Block[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
171172
"""Generate map from list.
172173
173174
Returns:
@@ -176,7 +177,7 @@ def of_block(lst: Block[tuple[Key, Value]]) -> Map[Key, Value]:
176177
return of_block(lst)
177178

178179
@staticmethod
179-
def of_list(lst: list[tuple[Key, Value]]) -> Map[Key, Value]:
180+
def of_list(lst: list[tuple[_Key_, _Result]]) -> Map[_Key_, _Result]:
180181
"""Generate map from list.
181182
182183
Returns:
@@ -185,7 +186,7 @@ def of_list(lst: list[tuple[Key, Value]]) -> Map[Key, Value]:
185186
return of_list(lst)
186187

187188
@staticmethod
188-
def of_seq(sequence: Iterable[tuple[Key, Value]]) -> Map[Key, Value]:
189+
def of_seq(sequence: Iterable[tuple[_Key_, _Result]]) -> Map[_Key_, _Result]:
189190
"""Generate map from sequence.
190191
191192
Generates a new map from an iterable of key/value tuples. This
@@ -206,10 +207,10 @@ def combine_hash(x: int, y: int) -> int:
206207
res = combine_hash(res, hash(y))
207208
return res
208209

209-
def __getitem__(self, k: Key) -> Value:
210+
def __getitem__(self, k: _Key) -> _Value:
210211
return maptree.find(k, self._tree)
211212

212-
def __iter__(self) -> Iterator[Key]:
213+
def __iter__(self) -> Iterator[_Key]:
213214
xs = maptree.mk_iterator(self._tree)
214215
return (k for (k, _) in xs)
215216

@@ -241,7 +242,7 @@ def __bool__(self) -> bool:
241242
return not maptree.is_empty(self._tree)
242243

243244
def __str__(self) -> str:
244-
def to_str(item: tuple[Key, Value]) -> str:
245+
def to_str(item: tuple[_Key, _Value]) -> str:
245246
key, value = item
246247
if isinstance(key, str):
247248
return f'("{key}", {value})'
@@ -255,7 +256,7 @@ def __repr__(self) -> str:
255256

256257

257258
@curry_flip(1)
258-
def add(table: Map[Key, Value], key: Key, value: Value) -> Map[Key, Value]:
259+
def add(table: Map[_Key, _Value], key: _Key, value: _Value) -> Map[_Key, _Value]:
259260
"""Add key with value to map.
260261
261262
Returns a new map with the binding added to the given map. If a
@@ -276,7 +277,7 @@ def add(table: Map[Key, Value], key: Key, value: Value) -> Map[Key, Value]:
276277

277278

278279
@curry_flip(1)
279-
def change(table: Map[Key, Value], key: Key, fn: Callable[[Option[Value]], Option[Value]]) -> Map[Key, Value]:
280+
def change(table: Map[_Key, _Value], key: _Key, fn: Callable[[Option[_Value]], Option[_Value]]) -> Map[_Key, _Value]:
280281
"""Change element in map.
281282
282283
Returns a new map with the value stored under key changed
@@ -294,7 +295,7 @@ def change(table: Map[Key, Value], key: Key, fn: Callable[[Option[Value]], Optio
294295

295296

296297
@curry_flip(1)
297-
def contains_key(table: Map[Key, Any], key: Key) -> bool:
298+
def contains_key(table: Map[_Key, Any], key: _Key) -> bool:
298299
return table.contains_key(key)
299300

300301

@@ -303,12 +304,12 @@ def count(table: Map[Any, Any]) -> int:
303304
return len(table)
304305

305306

306-
def create(ie: Iterable[tuple[Key, Value]]) -> Map[Key, Value]:
307+
def create(ie: Iterable[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
307308
return Map(maptree.of_seq(ie))
308309

309310

310311
@curry_flip(1)
311-
def find(table: Map[Key, Value], key: Key) -> Value:
312+
def find(table: Map[_Key, _Value], key: _Key) -> _Value:
312313
"""Find element with key in map.
313314
314315
Lookup an element in the map, raising KeyNotFoundException if no
@@ -334,15 +335,15 @@ def is_empty(table: Map[Any, Any]) -> bool:
334335
return table.is_empty()
335336

336337

337-
def iterate(action: Callable[[Key, Value], None]) -> Callable[[Map[Key, Value]], None]:
338-
def _iterate(table: Map[Key, Value]) -> None:
338+
def iterate(action: Callable[[_Key, _Value], None]) -> Callable[[Map[_Key, _Value]], None]:
339+
def _iterate(table: Map[_Key, _Value]) -> None:
339340
return table.iterate(action)
340341

341342
return _iterate
342343

343344

344345
@curry_flip(1)
345-
def try_pick(table: Map[Key, Value], chooser: Callable[[Key, Value], Option[Result]]) -> Option[Result]:
346+
def try_pick(table: Map[_Key, _Value], chooser: Callable[[_Key, _Value], Option[_Result]]) -> Option[_Result]:
346347
"""Pick element in map.
347348
348349
Searches the map looking for the first element where the given
@@ -361,15 +362,15 @@ def try_pick(table: Map[Key, Value], chooser: Callable[[Key, Value], Option[Resu
361362

362363

363364
@curry_flip(1)
364-
def pick(table: Map[Key, Value], chooser: Callable[[Key, Value], Option[Result]]) -> Result:
365+
def pick(table: Map[_Key, _Value], chooser: Callable[[_Key, _Value], Option[_Result]]) -> _Result:
365366
for res in table.try_pick(chooser):
366367
return res
367368
else:
368369
raise KeyError()
369370

370371

371372
@curry_flip(1)
372-
def exists(table: Map[Key, Value], predicate: Callable[[Key, Value], bool]) -> bool:
373+
def exists(table: Map[_Key, _Value], predicate: Callable[[_Key, _Value], bool]) -> bool:
373374
"""Test if element exists in map.
374375
375376
Returns true if the given predicate returns true for one of the
@@ -388,47 +389,47 @@ def exists(table: Map[Key, Value], predicate: Callable[[Key, Value], bool]) -> b
388389

389390

390391
@curry_flip(1)
391-
def filter(table: Map[Key, Value], predicate: Callable[[Key, Value], bool]) -> Map[Key, Value]:
392+
def filter(table: Map[_Key, _Value], predicate: Callable[[_Key, _Value], bool]) -> Map[_Key, _Value]:
392393
return table.filter(predicate)
393394

394395

395396
@curry_flip(1)
396-
def for_all(table: Map[Key, Value], predicate: Callable[[Key, Value], bool]) -> bool:
397+
def for_all(table: Map[_Key, _Value], predicate: Callable[[_Key, _Value], bool]) -> bool:
397398
return table.for_all(predicate)
398399

399400

400401
@curry_flip(1)
401-
def map(table: Map[Key, Value], mapping: Callable[[Key, Value], Result]) -> Map[Key, Result]:
402+
def map(table: Map[_Key, _Value], mapping: Callable[[_Key, _Value], _Result]) -> Map[_Key, _Result]:
402403
return table.map(mapping)
403404

404405

405406
@curry_flip(1)
406407
def fold(
407-
table: Map[Key, Value],
408-
folder: Callable[[Result, tuple[Key, Value]], Result],
409-
state: Result,
410-
) -> Result:
408+
table: Map[_Key, _Value],
409+
folder: Callable[[_Result, tuple[_Key, _Value]], _Result],
410+
state: _Result,
411+
) -> _Result:
411412
return table.fold(folder, state)
412413

413414

414415
@curry_flip(1)
415416
def fold_back(
416-
state: Result,
417-
folder: Callable[[tuple[Key, Value], Result], Result],
418-
table: Map[Key, Value],
419-
) -> Result:
417+
state: _Result,
418+
folder: Callable[[tuple[_Key, _Value], _Result], _Result],
419+
table: Map[_Key, _Value],
420+
) -> _Result:
420421
return table.fold_back(folder, state)
421422

422423

423424
@curry_flip(1)
424425
def partition(
425-
table: Map[Key, Value], predicate: Callable[[Key, Value], bool]
426-
) -> tuple[Map[Key, Value], Map[Key, Value]]:
426+
table: Map[_Key, _Value], predicate: Callable[[_Key, _Value], bool]
427+
) -> tuple[Map[_Key, _Value], Map[_Key, _Value]]:
427428
return table.partition(predicate)
428429

429430

430431
@curry_flip(1)
431-
def remove(table: Map[Key, Value], key: Key) -> Map[Key, Value]:
432+
def remove(table: Map[_Key, _Value], key: _Key) -> Map[_Key, _Value]:
432433
"""Remove element from map.
433434
434435
Removes an element from the domain of the map. No exception is
@@ -444,33 +445,33 @@ def remove(table: Map[Key, Value], key: Key) -> Map[Key, Value]:
444445
return table.remove(key)
445446

446447

447-
def of(**args: Value) -> Map[str, Value]:
448+
def of(**args: _Value) -> Map[str, _Value]:
448449
"""Create map from arguments."""
449450
return Map(maptree.of_seq(args.items()))
450451

451452

452-
def of_block(elements: Block[tuple[Key, Value]]) -> Map[Key, Value]:
453+
def of_block(elements: Block[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
453454
return Map(maptree.of_list(elements))
454455

455456

456-
def of_list(elements: list[tuple[Key, Value]]) -> Map[Key, Value]:
457+
def of_list(elements: list[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
457458
return Map(maptree.of_list(Block(elements)))
458459

459460

460-
def of_seq(elements: Iterable[tuple[Key, Value]]) -> Map[Key, Value]:
461+
def of_seq(elements: Iterable[tuple[_Key, _Value]]) -> Map[_Key, _Value]:
461462
return Map(maptree.of_seq(elements))
462463

463464

464-
def to_list(table: Map[Key, Value]) -> Block[tuple[Key, Value]]:
465+
def to_list(table: Map[_Key, _Value]) -> Block[tuple[_Key, _Value]]:
465466
return table.to_list()
466467

467468

468-
def to_seq(table: Map[Key, Value]) -> Iterable[tuple[Key, Value]]:
469+
def to_seq(table: Map[_Key, _Value]) -> Iterable[tuple[_Key, _Value]]:
469470
return table.to_seq()
470471

471472

472473
@curry_flip(1)
473-
def try_find(table: Map[Key, Value], key: Key) -> Option[Value]:
474+
def try_find(table: Map[_Key, _Value], key: _Key) -> Option[_Value]:
474475
"""Try to find element with key in map.
475476
476477
Lookup an element in the map, returning a `Some` value if the

‎expression/core/option.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Option(
4848
some: _TSource = case()
4949

5050
@staticmethod
51-
def Some(value: _TSource) -> Option[_TSource]:
51+
def Some(value: _TResult) -> Option[_TResult]:
5252
"""Create a Some option."""
5353
return Option(some=value)
5454

@@ -185,7 +185,7 @@ def to_seq(self) -> Seq[_TSource]:
185185

186186
match self:
187187
case Option(tag="some", some=some):
188-
return Seq.of(some)
188+
return Seq[_TSource].of(some)
189189
case _:
190190
return Seq()
191191

@@ -365,7 +365,7 @@ def validate_none(v: Any, handler: ValidatorFunctionWrapHandler) -> Option[Any]:
365365

366366
def Some(value: _TSource) -> Option[_TSource]:
367367
"""Create a Some option."""
368-
return Option.Some(value)
368+
return Option[_TSource].Some(value)
369369

370370

371371
@curry_flip(1)

‎expression/core/result.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Result(
5656
error: _TError = case()
5757

5858
@staticmethod
59-
def Ok(value: _TSource) -> Result[_TSource, _TError]:
59+
def Ok(value: _TResult) -> Result[_TResult, _TError]:
6060
"""Create a new Ok result."""
6161
return Result(tag="ok", ok=value)
6262

@@ -97,7 +97,7 @@ def map(self, mapper: Callable[[_TSource], _TResult]) -> Result[_TResult, _TErro
9797
"""
9898
match self:
9999
case Result(tag="ok", ok=value):
100-
return Result.Ok(mapper(value))
100+
return Result[_TResult, _TError].Ok(mapper(value))
101101
case Result(error=error):
102102
return Result[_TResult, _TError].Error(error)
103103

@@ -127,7 +127,7 @@ def map_error(self, mapper: Callable[[_TError], _TResult]) -> Result[_TSource, _
127127
case Result(tag="ok", ok=value):
128128
return Result[_TSource, _TResult].Ok(value)
129129
case Result(error=error):
130-
return Result.Error(mapper(error))
130+
return Result[_TSource, _TResult].Error(mapper(error))
131131

132132
def bind(self, mapper: Callable[[_TSource], Result[_TResult, _TError]]) -> Result[_TResult, _TError]:
133133
"""Bind result.

‎expression/extra/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import string
44
from collections.abc import Callable
5-
from typing import Any, Generic, TypeVar, cast, overload
5+
from typing import Any, Generic, TypeVar, overload
66

77
from expression.collections import Block, block
88
from expression.core import Error, Nothing, Ok, Option, Result, Some, curry, fst, pipe
@@ -344,7 +344,7 @@ def sep_by(p: Parser[_A], sep: Parser[Any]) -> Parser[Block[_A]]:
344344

345345

346346
def opt(p: Parser[_A]) -> Parser[Option[_A]]:
347-
nothing = cast(Option[_A], Nothing)
347+
nothing: Option[_A] = Nothing
348348

349349
def mapper(a: _A) -> Option[_A]:
350350
return Some(a)

‎tests/test_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def unfolder(state: int) -> Option[tuple[int, int]]:
319319
return Some((state, state + 1))
320320
return Nothing
321321

322-
result = TypedArray.unfold(unfolder, 0)
322+
result: TypedArray[int] = TypedArray.unfold(unfolder, 0)
323323

324324
assert list(result) == list(range(x))
325325

‎tests/test_block.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def unfolder(state: int) -> Option[tuple[int, int]]:
286286
return Some((state, state + 1))
287287
return Nothing
288288

289-
result = Block.unfold(unfolder, 0)
289+
result: Block[int] = Block.unfold(unfolder, 0)
290290

291291
assert list(result) == list(range(x))
292292

‎tests/test_option.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_option_some():
3232

3333

3434
def test_option_some_match():
35-
xs = Some(42)
35+
xs = Option.Some(42)
3636

3737
match xs:
3838
case Option(tag="some", some=x):

0 commit comments

Comments
 (0)
Please sign in to comment.