Skip to content

Commit

Permalink
Merge pull request #37 from fwfurtado/issue-29
Browse files Browse the repository at this point in the history
Added supporting to type hint in curried decorator
  • Loading branch information
jacobbridges authored Sep 5, 2019
2 parents c01c2b7 + 3211a9f commit 486119e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
20 changes: 16 additions & 4 deletions fn/func.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from functools import partial, update_wrapper, wraps
from inspect import getargspec
from sys import version_info


_has_type_hint_support = version_info[:2] >= (3, 5)


def identity(arg):
Expand Down Expand Up @@ -69,6 +72,17 @@ def curried(func):
>>> sum5(1, 2, 3)(4, 5)
15
"""

def _args_len(func):
if _has_type_hint_support:
from inspect import signature
args = signature(func).parameters
else:
from inspect import getargspec
args = getargspec(func).args

return len(args)

@wraps(func)
def _curried(*args, **kwargs):
f = func
Expand All @@ -78,9 +92,7 @@ def _curried(*args, **kwargs):
count += len(f.args)
f = f.func

spec = getargspec(f)

if count == len(spec.args) - len(args):
if count == _args_len(f) - len(args):
return func(*args, **kwargs)

para_func = partial(func, *args, **kwargs)
Expand Down
6 changes: 3 additions & 3 deletions fn/monad.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def parameter(self, name):
from fn.monad import Option
request = dict(url="face.png", mimetype="PNG")
tp = Option(request.get("type", None)) \ # check "type" key first
.or_call(from_mimetype, request) \ # or.. check "mimetype" key
.or_call(from_extension, request) \ # or... get "url" and check extension
tp = Option(request.get("type", None)) \\ # check "type" key first
.or_call(from_mimetype, request) \\ # or... check "mimetype" key
.or_call(from_extension, request) \\# or... get "url" and check extension
.get_or("application/undefined")
"""
Expand Down
45 changes: 37 additions & 8 deletions tests/test_curry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import unittest

from fn.func import curried
from fn.func import curried, _has_type_hint_support


class Curriedtest(unittest.TestCase):

def _assert_instance(self, expected, acutal):
self.assertEqual(expected.__module__, acutal.__module__)
self.assertEqual(expected.__name__, acutal.__name__)

if _has_type_hint_support:
self.assertEqual(expected.__annotations__, acutal.__annotations__)

def test_curried_wrapper(self):

@curried
Expand All @@ -15,16 +22,38 @@ def _child(a, b, c, d):
def _moma(a, b):
return _child(a, b)

def _assert_instance(expected, acutal):
self.assertEqual(expected.__module__, acutal.__module__)
self.assertEqual(expected.__name__, acutal.__name__)

res1 = _moma(1)
_assert_instance(_moma, res1)
self._assert_instance(_moma, res1)
res2 = res1(2)
self._assert_instance(_child, res2)
res3 = res2(3)
self._assert_instance(_child, res3)
res4 = res3(4)

self.assertEqual(res4, 10)

@unittest.skipIf(not _has_type_hint_support, "Type hint aren't supported")
def test_curried_with_annotations_when_they_are_supported(self):

def _custom_sum(a, b, c, d):
return a + b + c + d

_custom_sum.__annotations__ = {
'a': int,
'b': int,
'c': int,
'd': int,
'return': int
}

custom_sum = curried(_custom_sum)

res1 = custom_sum(1)
self._assert_instance(custom_sum, res1)
res2 = res1(2)
_assert_instance(_child, res2)
self._assert_instance(custom_sum, res2)
res3 = res2(3)
_assert_instance(_child, res3)
self._assert_instance(custom_sum, res3)
res4 = res3(4)

self.assertEqual(res4, 10)

0 comments on commit 486119e

Please sign in to comment.