Skip to content

Commit

Permalink
Feature type hinting (#190)
Browse files Browse the repository at this point in the history
* feature: type hinting for pipeline and transformation

* use black lint to format

* Update functional/pipeline.py

make max_repr_items optional

Co-authored-by: Mathis Chenuet <[email protected]>

* update hint for sequence class intializer

* Update functional/pipeline.py

make max_repr_items optional

Co-authored-by: Mathis Chenuet <[email protected]>

* update hint for sequence class intializer

* fix: improve pipeline hints for wraps

* fix: improve pipeline hints for wraps

* fix: correct missing update

---------

Co-authored-by: Oghenemarho Onothoja <[email protected]>
Co-authored-by: Mathis Chenuet <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2023
1 parent 95da04b commit 1681050
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
20 changes: 14 additions & 6 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import sqlite3
import re

from collections.abc import Iterable
from typing import Optional

from tabulate import tabulate

from functional.execution import ExecutionEngine
Expand All @@ -34,7 +37,12 @@ class Sequence(object):
"""

def __init__(
self, sequence, transform=None, engine=None, max_repr_items=None, no_wrap=None
self,
sequence: Iterable,
transform=None,
engine: ExecutionEngine = None,
max_repr_items: Optional[int] = None,
no_wrap: Optional[bool] = None,
):
# pylint: disable=protected-access
"""
Expand Down Expand Up @@ -225,7 +233,7 @@ def cache(self, delete_lineage=False):
self._lineage = Lineage(engine=self.engine)
return self

def head(self, no_wrap=None):
def head(self, no_wrap: Optional[bool] = None):
"""
Returns the first element of the sequence.
Expand All @@ -247,7 +255,7 @@ def head(self, no_wrap=None):
else:
return _wrap(self.take(1)[0])

def first(self, no_wrap=None):
def first(self, no_wrap: Optional[bool] = None):
"""
Returns the first element of the sequence.
Expand All @@ -266,7 +274,7 @@ def first(self, no_wrap=None):
"""
return self.head(no_wrap=no_wrap)

def head_option(self, no_wrap=None):
def head_option(self, no_wrap: Optional[bool] = None):
"""
Returns the first element of the sequence or None, if the sequence is empty.
Expand All @@ -283,7 +291,7 @@ def head_option(self, no_wrap=None):
return None
return self.head(no_wrap=no_wrap)

def last(self, no_wrap=None):
def last(self, no_wrap: Optional[bool] = None):
"""
Returns the last element of the sequence.
Expand All @@ -305,7 +313,7 @@ def last(self, no_wrap=None):
else:
return _wrap(self.sequence[-1])

def last_option(self, no_wrap=None):
def last_option(self, no_wrap: Optional[bool] = None):
"""
Returns the last element of the sequence or None, if the sequence is empty.
Expand Down
37 changes: 19 additions & 18 deletions functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
import collections
import types
from collections.abc import Callable

from functional.execution import ExecutionStrategies

Expand All @@ -24,7 +25,7 @@
CACHE_T = Transformation("cache", None, None)


def name(function):
def name(function: Callable):
"""
Retrieve a pretty name for the function
:param function: function to get name from
Expand All @@ -36,7 +37,7 @@ def name(function):
return str(function)


def map_t(func):
def map_t(func: Callable):
"""
Transformation for Sequence.map
:param func: map function
Expand All @@ -49,7 +50,7 @@ def map_t(func):
)


def select_t(func):
def select_t(func: Callable):
"""
Transformation for Sequence.select
:param func: select function
Expand All @@ -62,7 +63,7 @@ def select_t(func):
)


def starmap_t(func):
def starmap_t(func: Callable):
"""
Transformation for Sequence.starmap and Sequence.smap
:param func: starmap function
Expand All @@ -75,7 +76,7 @@ def starmap_t(func):
)


def filter_t(func):
def filter_t(func: Callable):
"""
Transformation for Sequence.filter
:param func: filter function
Expand All @@ -88,7 +89,7 @@ def filter_t(func):
)


def where_t(func):
def where_t(func: Callable):
"""
Transformation for Sequence.where
:param func: where function
Expand All @@ -101,7 +102,7 @@ def where_t(func):
)


def filter_not_t(func):
def filter_not_t(func: Callable):
"""
Transformation for Sequence.filter_not
:param func: filter_not function
Expand All @@ -122,7 +123,7 @@ def reversed_t():
return Transformation("reversed", reversed, [ExecutionStrategies.PRE_COMPUTE])


def slice_t(start, until):
def slice_t(start: int, until: int):
"""
Transformation for Sequence.slice
:param start: start index
Expand Down Expand Up @@ -153,7 +154,7 @@ def distinct(sequence):
return Transformation("distinct", distinct, None)


def distinct_by_t(func):
def distinct_by_t(func: Callable):
"""
Transformation for Sequence.distinct_by
:param func: distinct_by function
Expand All @@ -171,7 +172,7 @@ def distinct_by(sequence):
return Transformation(f"distinct_by({name(func)})", distinct_by, None)


def sorted_t(key=None, reverse=False):
def sorted_t(key=None, reverse: bool = False):
"""
Transformation for Sequence.sorted
:param key: key to sort by
Expand All @@ -183,7 +184,7 @@ def sorted_t(key=None, reverse=False):
)


def order_by_t(func):
def order_by_t(func: Callable):
"""
Transformation for Sequence.order_by
:param func: order_by function
Expand All @@ -196,7 +197,7 @@ def order_by_t(func):
)


def drop_right_t(n):
def drop_right_t(n: int):
"""
Transformation for Sequence.drop_right
:param n: number to drop from right
Expand All @@ -213,7 +214,7 @@ def drop_right_t(n):
)


def drop_t(n):
def drop_t(n: int):
"""
Transformation for Sequence.drop
:param n: number to drop from left
Expand All @@ -224,7 +225,7 @@ def drop_t(n):
)


def drop_while_t(func):
def drop_while_t(func: Callable):
"""
Transformation for Sequence.drop_while
:param func: drops while func is true
Expand All @@ -233,7 +234,7 @@ def drop_while_t(func):
return Transformation(f"drop_while({name(func)})", partial(dropwhile, func), None)


def take_t(n):
def take_t(n: int):
"""
Transformation for Sequence.take
:param n: number to take
Expand All @@ -242,7 +243,7 @@ def take_t(n):
return Transformation(f"take({n})", lambda sequence: islice(sequence, 0, n), None)


def take_while_t(func):
def take_while_t(func: Callable):
"""
Transformation for Sequence.take_while
:param func: takes while func is True
Expand All @@ -251,7 +252,7 @@ def take_while_t(func):
return Transformation(f"take_while({name(func)})", partial(takewhile, func), None)


def flat_map_impl(func, sequence):
def flat_map_impl(func: Callable, sequence):
"""
Implementation for flat_map_t
:param func: function to map
Expand Down Expand Up @@ -710,7 +711,7 @@ def peek_impl(func, sequence):
yield element


def peek_t(func):
def peek_t(func: Callable):
"""
Transformation for Sequence.peek
:param func: peek function
Expand Down
2 changes: 1 addition & 1 deletion functional/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def compose(*functions):
return reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x)


def default_value(*vals):
def default_value(*vals: tuple):
for val in vals:
if val is not None:
return val
Expand Down

0 comments on commit 1681050

Please sign in to comment.