Skip to content

Commit bc8a672

Browse files
Oghenemarho OnothojaOghenemarho Onothoja
Oghenemarho Onothoja
authored and
Oghenemarho Onothoja
committed
feature: type hinting for pipeline and transformation
1 parent e5eb3d3 commit bc8a672

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

functional/pipeline.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import sqlite3
1111
import re
1212

13+
from typing import TypeVar
14+
1315
from tabulate import tabulate
1416

1517
from functional.execution import ExecutionEngine
@@ -27,14 +29,17 @@
2729
from functional.execution import ExecutionStrategies
2830

2931

32+
TSequence = TypeVar("TSequence", covariant=True, bound="Sequence")
33+
34+
3035
class Sequence(object):
3136
"""
3237
Sequence is a wrapper around any type of sequence which provides access to common
3338
functional transformations and reductions in a data pipeline style
3439
"""
3540

3641
def __init__(
37-
self, sequence, transform=None, engine=None, max_repr_items=None, no_wrap=None
42+
self, sequence: TSequence, transform=None, engine: ExecutionEngine=None, max_repr_items: int=None, no_wrap: bool=False
3843
):
3944
# pylint: disable=protected-access
4045
"""
@@ -225,7 +230,7 @@ def cache(self, delete_lineage=False):
225230
self._lineage = Lineage(engine=self.engine)
226231
return self
227232

228-
def head(self, no_wrap=None):
233+
def head(self, no_wrap: bool=False):
229234
"""
230235
Returns the first element of the sequence.
231236
@@ -247,7 +252,7 @@ def head(self, no_wrap=None):
247252
else:
248253
return _wrap(self.take(1)[0])
249254

250-
def first(self, no_wrap=None):
255+
def first(self, no_wrap: bool=False):
251256
"""
252257
Returns the first element of the sequence.
253258
@@ -266,7 +271,7 @@ def first(self, no_wrap=None):
266271
"""
267272
return self.head(no_wrap=no_wrap)
268273

269-
def head_option(self, no_wrap=None):
274+
def head_option(self, no_wrap: bool=False):
270275
"""
271276
Returns the first element of the sequence or None, if the sequence is empty.
272277
@@ -283,7 +288,7 @@ def head_option(self, no_wrap=None):
283288
return None
284289
return self.head(no_wrap=no_wrap)
285290

286-
def last(self, no_wrap=None):
291+
def last(self, no_wrap: bool=False):
287292
"""
288293
Returns the last element of the sequence.
289294
@@ -305,7 +310,7 @@ def last(self, no_wrap=None):
305310
else:
306311
return _wrap(self.sequence[-1])
307312

308-
def last_option(self, no_wrap=None):
313+
def last_option(self, no_wrap: bool=False):
309314
"""
310315
Returns the last element of the sequence or None, if the sequence is empty.
311316

functional/transformations.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
import collections
1313
import types
14+
from collections.abc import Callable
1415

1516
from functional.execution import ExecutionStrategies
1617

@@ -24,7 +25,7 @@
2425
CACHE_T = Transformation("cache", None, None)
2526

2627

27-
def name(function):
28+
def name(function: Callable):
2829
"""
2930
Retrieve a pretty name for the function
3031
:param function: function to get name from
@@ -36,7 +37,7 @@ def name(function):
3637
return str(function)
3738

3839

39-
def map_t(func):
40+
def map_t(func: Callable):
4041
"""
4142
Transformation for Sequence.map
4243
:param func: map function
@@ -49,7 +50,7 @@ def map_t(func):
4950
)
5051

5152

52-
def select_t(func):
53+
def select_t(func: Callable):
5354
"""
5455
Transformation for Sequence.select
5556
:param func: select function
@@ -62,7 +63,7 @@ def select_t(func):
6263
)
6364

6465

65-
def starmap_t(func):
66+
def starmap_t(func: Callable):
6667
"""
6768
Transformation for Sequence.starmap and Sequence.smap
6869
:param func: starmap function
@@ -75,7 +76,7 @@ def starmap_t(func):
7576
)
7677

7778

78-
def filter_t(func):
79+
def filter_t(func: Callable):
7980
"""
8081
Transformation for Sequence.filter
8182
:param func: filter function
@@ -88,7 +89,7 @@ def filter_t(func):
8889
)
8990

9091

91-
def where_t(func):
92+
def where_t(func: Callable):
9293
"""
9394
Transformation for Sequence.where
9495
:param func: where function
@@ -101,7 +102,7 @@ def where_t(func):
101102
)
102103

103104

104-
def filter_not_t(func):
105+
def filter_not_t(func: Callable):
105106
"""
106107
Transformation for Sequence.filter_not
107108
:param func: filter_not function
@@ -122,7 +123,7 @@ def reversed_t():
122123
return Transformation("reversed", reversed, [ExecutionStrategies.PRE_COMPUTE])
123124

124125

125-
def slice_t(start, until):
126+
def slice_t(start: int, until: int):
126127
"""
127128
Transformation for Sequence.slice
128129
:param start: start index
@@ -153,7 +154,7 @@ def distinct(sequence):
153154
return Transformation("distinct", distinct, None)
154155

155156

156-
def distinct_by_t(func):
157+
def distinct_by_t(func: Callable):
157158
"""
158159
Transformation for Sequence.distinct_by
159160
:param func: distinct_by function
@@ -171,7 +172,7 @@ def distinct_by(sequence):
171172
return Transformation("distinct_by({0})".format(name(func)), distinct_by, None)
172173

173174

174-
def sorted_t(key=None, reverse=False):
175+
def sorted_t(key=None, reverse: bool=False):
175176
"""
176177
Transformation for Sequence.sorted
177178
:param key: key to sort by
@@ -183,7 +184,7 @@ def sorted_t(key=None, reverse=False):
183184
)
184185

185186

186-
def order_by_t(func):
187+
def order_by_t(func: Callable):
187188
"""
188189
Transformation for Sequence.order_by
189190
:param func: order_by function
@@ -196,7 +197,7 @@ def order_by_t(func):
196197
)
197198

198199

199-
def drop_right_t(n):
200+
def drop_right_t(n: int):
200201
"""
201202
Transformation for Sequence.drop_right
202203
:param n: number to drop from right
@@ -213,7 +214,7 @@ def drop_right_t(n):
213214
)
214215

215216

216-
def drop_t(n):
217+
def drop_t(n: int):
217218
"""
218219
Transformation for Sequence.drop
219220
:param n: number to drop from left
@@ -224,7 +225,7 @@ def drop_t(n):
224225
)
225226

226227

227-
def drop_while_t(func):
228+
def drop_while_t(func: Callable):
228229
"""
229230
Transformation for Sequence.drop_while
230231
:param func: drops while func is true
@@ -235,7 +236,7 @@ def drop_while_t(func):
235236
)
236237

237238

238-
def take_t(n):
239+
def take_t(n: int):
239240
"""
240241
Transformation for Sequence.take
241242
:param n: number to take
@@ -246,7 +247,7 @@ def take_t(n):
246247
)
247248

248249

249-
def take_while_t(func):
250+
def take_while_t(func: Callable):
250251
"""
251252
Transformation for Sequence.take_while
252253
:param func: takes while func is True
@@ -257,7 +258,7 @@ def take_while_t(func):
257258
)
258259

259260

260-
def flat_map_impl(func, sequence):
261+
def flat_map_impl(func: Callable, sequence):
261262
"""
262263
Implementation for flat_map_t
263264
:param func: function to map
@@ -720,7 +721,7 @@ def peek_impl(func, sequence):
720721
yield element
721722

722723

723-
def peek_t(func):
724+
def peek_t(func: Callable):
724725
"""
725726
Transformation for Sequence.peek
726727
:param func: peek function

0 commit comments

Comments
 (0)