From 63066c8d3bf600ab59942e9d67768c1329d2bded Mon Sep 17 00:00:00 2001 From: zyd Date: Tue, 24 May 2022 22:18:11 +0900 Subject: [PATCH] Fix arity issue --- ramda/private/_arity.py | 2 +- ramda/private/_curry1.py | 2 +- ramda/private/_curry2.py | 16 ++++------- ramda/private/_curry3.py | 27 +++-------------- test/private/test__curry1.py | 27 +++++++++++++++++ test/private/test__curry2.py | 12 ++++++-- test/private/test__curry3.py | 8 ++++-- test/test_curryN.py | 56 ++++++++++++++++++++++++++++++++++++ 8 files changed, 110 insertions(+), 40 deletions(-) create mode 100644 test/private/test__curry1.py diff --git a/ramda/private/_arity.py b/ramda/private/_arity.py index 7f35a0f..baa083c 100644 --- a/ramda/private/_arity.py +++ b/ramda/private/_arity.py @@ -4,7 +4,7 @@ # pylint: disable=dangerous-default-value # pylint: disable=keyword-arg-before-vararg def _arity(n, fn): - def f0(): return fn() + def f0(*_): return fn(*_) def f1(a0=__, *_): return fn(a0, *_) def f2(a0=__, a1=__, *_): return fn(a0, a1, *_) def f3(a0=__, a1=__, a2=__, *_): return fn(a0, a1, a2, *_) diff --git a/ramda/private/_curry1.py b/ramda/private/_curry1.py index 4a3f7eb..ac93aba 100644 --- a/ramda/private/_curry1.py +++ b/ramda/private/_curry1.py @@ -8,5 +8,5 @@ def _curry1(fn): def f1(a=__, *_): if _isPlaceholder(a): return f1 - return fn(a) + return fn(a, *_) return f1 diff --git a/ramda/private/_curry2.py b/ramda/private/_curry2.py index 53b6220..3668286 100644 --- a/ramda/private/_curry2.py +++ b/ramda/private/_curry2.py @@ -1,19 +1,13 @@ +from ..__ import __ from ._curry1 import _curry1 from ._isPlaceholder import _isPlaceholder def _curry2(fn): - def f2(*args): - def f_b(_b): return fn(args[0], _b) - if len(args) == 0: - return f2 - if len(args) == 1: - a = args[0] - if _isPlaceholder(a): - return f2 - - return _curry1(f_b) - a, b = args[0], args[1] + # pylint: disable=dangerous-default-value + # pylint: disable=keyword-arg-before-vararg + def f2(a=__, b=__, *_): + def f_b(_b): return fn(a, _b) if _isPlaceholder(a) and _isPlaceholder(b): return f2 if _isPlaceholder(a): diff --git a/ramda/private/_curry3.py b/ramda/private/_curry3.py index cb7026b..6a93bb9 100644 --- a/ramda/private/_curry3.py +++ b/ramda/private/_curry3.py @@ -1,38 +1,19 @@ +from ..__ import __ from ._curry1 import _curry1 from ._curry2 import _curry2 from ._isPlaceholder import _isPlaceholder def _curry3(fn): - # pylint: disable=too-many-return-statements - def f3(*args): - if len(args) >= 1: - a = args[0] - if len(args) >= 2: - b = args[1] - if len(args) >= 3: - c = args[2] - + # pylint: disable=dangerous-default-value + # pylint: disable=keyword-arg-before-vararg + def f3(a=__, b=__, c=__, *_): def f_ab(_a, _b): return fn(_a, _b, c) def f_bc(_b, _c): return fn(a, _b, _c) def f_ac(_a, _c): return fn(_a, b, _c) def f_a(_a): return fn(_a, b, c) def f_b(_b): return fn(a, _b, c) def f_c(_c): return fn(a, b, _c) - if len(args) == 0: - return f3 - if len(args) == 1: - if _isPlaceholder(a): - return f3 - return _curry2(f_bc) - if len(args) == 2: - if _isPlaceholder(a) and _isPlaceholder(b): - return f3 - if _isPlaceholder(a): - return _curry2(f_ac) - if _isPlaceholder(b): - return _curry2(f_bc) - return _curry1(f_c) if _isPlaceholder(a) and _isPlaceholder(b) and _isPlaceholder(c): return f3 if _isPlaceholder(a) and _isPlaceholder(b): diff --git a/test/private/test__curry1.py b/test/private/test__curry1.py new file mode 100644 index 0000000..8afe761 --- /dev/null +++ b/test/private/test__curry1.py @@ -0,0 +1,27 @@ +import unittest + +import ramda as R +from ramda.private._curry1 import _curry1 +from ramda.private._helper import funcArgsLength + + +def f(a): return [a] + + +g = _curry1(f) + + +class Test_Curry1(unittest.TestCase): + def test_supports_placeholder(self): + _ = R.__ + + self.assertEqual([1], g()(1)) + self.assertEqual([1], g(_)(1)) + self.assertEqual([1], g(1)) + + def test_has_1_arity(self): + self.assertEqual(1, funcArgsLength(g)) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/private/test__curry2.py b/test/private/test__curry2.py index 2e910c9..7793134 100644 --- a/test/private/test__curry2.py +++ b/test/private/test__curry2.py @@ -2,16 +2,21 @@ import ramda as R from ramda.private._curry2 import _curry2 +from ramda.private._helper import funcArgsLength """ https://github.com/ramda/ramda/blob/master/test/internal/_curry2.js """ +def f(a, b): return [a, b] + + +g = _curry2(f) + + class Test_Curry2(unittest.TestCase): def test_supports_placeholder(self): - def f(a, b): return [a, b] - g = _curry2(f) _ = R.__ self.assertEqual([1, 2], g(1)(2)) @@ -25,6 +30,9 @@ def f(a, b): return [a, b] self.assertEqual([1, 2], g(_, _)(_)(1, 2)) self.assertEqual([1, 2], g(_, _)(_, 2)(1)) + def test_has_2_arity(self): + self.assertEqual(2, funcArgsLength(g)) + if __name__ == '__main__': unittest.main() diff --git a/test/private/test__curry3.py b/test/private/test__curry3.py index 3c0e7e7..eb6f454 100644 --- a/test/private/test__curry3.py +++ b/test/private/test__curry3.py @@ -3,16 +3,17 @@ import ramda as R from ramda.private._curry3 import _curry3 +from ramda.private._helper import funcArgsLength """ https://github.com/ramda/ramda/blob/master/test/internal/_curry3.js """ +def f(a, b, c): return [a, b, c] +g = _curry3(f) class Test_Curry3(unittest.TestCase): def test_supports_placeholder(self): - def f(a, b, c): return [a, b, c] - g = _curry3(f) _ = R.__ self.assertEqual([1, 2, 3], g(1)(2)(3)) @@ -39,6 +40,9 @@ def f(a, b, c): return [a, b, c] self.assertEqual([1, 2, 3], g(_, _, _)(_, _)(1, 2, 3)) self.assertEqual([1, 2, 3], g(_, _, _)(1, _, _)(_, _)(2, _)(_)(3)) + def test_has_3_arity(self): + self.assertEqual(3, funcArgsLength(g)) + if __name__ == '__main__': unittest.main() diff --git a/test/test_curryN.py b/test/test_curryN.py index 32345ff..4235266 100644 --- a/test/test_curryN.py +++ b/test/test_curryN.py @@ -3,6 +3,7 @@ from random import randint import ramda as R +from ramda.private._helper import funcArgsLength def source(a, b, c): @@ -101,6 +102,61 @@ def f(*arguments): self.assertEqual([1, 2, 3, 4], g(1)(2, 3, 4)) self.assertEqual([1, 2, 3, 4], g(1)(2)(3, 4)) + def test_return_proper_arity_with_given_arity_number(self): + def f0(): + return 0 + self.assertEqual(0, funcArgsLength(R.curryN(0, f0))) + def f1(x1): return [x1] + self.assertEqual(1, funcArgsLength(R.curryN(1, f1))) + def f2(x1, x2): return [x1, x2] + self.assertEqual(2, funcArgsLength(R.curryN(2, f2))) + def f3(x1, x2, x3): return [x1, x2, x3] + self.assertEqual(3, funcArgsLength(R.curryN(3, f3))) + def f4(x1, x2, x3, x4): return [x1, x2, x3, x4] + self.assertEqual(4, funcArgsLength(R.curryN(4, f4))) + def f5(x1, x2, x3, x4, x5): return [x1, x2, x3, x4, x5] + self.assertEqual(5, funcArgsLength(R.curryN(5, f5))) + def f6(x1, x2, x3, x4, x5, x6): return [x1, x2, x3, x4, x5, x6] + self.assertEqual(6, funcArgsLength(R.curryN(6, f6))) + def f7(x1, x2, x3, x4, x5, x6, x7): return [x1, x2, x3, x4, x5, x6, x7] + self.assertEqual(7, funcArgsLength(R.curryN(7, f7))) + def f8(x1, x2, x3, x4, x5, x6, x7, x8): return [x1, x2, x3, x4, x5, x6, x7, x8] + self.assertEqual(8, funcArgsLength(R.curryN(8, f8))) + def f9(x1, x2, x3, x4, x5, x6, x7, x8, x9): return [x1, x2, x3, x4, x5, x6, x7, x8, x9] + self.assertEqual(9, funcArgsLength(R.curryN(9, f9))) + def f10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10): return [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10] + self.assertEqual(10, funcArgsLength(R.curryN(10, f10))) + with self.assertRaises(Exception): + def f11(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11): return [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11] + R.curryN(11, f11) + + def test_returned_function_can_be_called_with_proper_arity(self): + expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] + def f11(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11): return [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11] + + self.assertEqual(0, funcArgsLength(R.curryN(0, f11))) + self.assertEqual(expected, R.curryN(0, f11)(*expected)) + self.assertEqual(1, funcArgsLength(R.curryN(1, f11))) + self.assertEqual(expected, R.curryN(1, f11)(*expected)) + self.assertEqual(2, funcArgsLength(R.curryN(2, f11))) + self.assertEqual(expected, R.curryN(2, f11)(1)(*expected[1:])) + self.assertEqual(3, funcArgsLength(R.curryN(3, f11))) + self.assertEqual(expected, R.curryN(3, f11)(1)(2)(*expected[2:])) + self.assertEqual(4, funcArgsLength(R.curryN(4, f11))) + self.assertEqual(expected, R.curryN(4, f11)(1)(2)(3)(*expected[3:])) + self.assertEqual(5, funcArgsLength(R.curryN(5, f11))) + self.assertEqual(expected, R.curryN(5, f11)(1)(2)(3)(4)(*expected[4:])) + self.assertEqual(6, funcArgsLength(R.curryN(6, f11))) + self.assertEqual(expected, R.curryN(6, f11)(1)(2)(3)(4)(5)(*expected[5:])) + self.assertEqual(7, funcArgsLength(R.curryN(7, f11))) + self.assertEqual(expected, R.curryN(7, f11)(1)(2)(3)(4)(5)(6)(*expected[6:])) + self.assertEqual(8, funcArgsLength(R.curryN(8, f11))) + self.assertEqual(expected, R.curryN(8, f11)(1)(2)(3)(4)(5)(6)(7)(*expected[7:])) + self.assertEqual(9, funcArgsLength(R.curryN(9, f11))) + self.assertEqual(expected, R.curryN(9, f11)(1)(2)(3)(4)(5)(6)(7)(8)(*expected[8:])) + self.assertEqual(10, funcArgsLength(R.curryN(10, f11))) + self.assertEqual(expected, R.curryN(10, f11)(1)(2)(3)(4)(5)(6)(7)(8)(9)(*expected[9:])) + if __name__ == '__main__': unittest.main()