Skip to content

Commit bb4623d

Browse files
committed
allow *args and **kwargs to be passed to Func
1 parent 0f60f67 commit bb4623d

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
lint:
2-
flake8 --ignore=E131,E301,E731,W503,E701,E704 --max-line-length=100 effect/
2+
flake8 --ignore=E131,E301,E302,E731,W503,E701,E704 --max-line-length=100 effect/
33

44
build-dist:
55
rm -rf dist

effect/_intents.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def perform_error(dispatcher, intent):
150150
raise intent.exception
151151

152152

153-
@attr.s
153+
@attr.s(init=False)
154154
class Func(object):
155155
"""
156156
An intent that returns the result of the specified function.
@@ -171,14 +171,23 @@ class Func(object):
171171
way.
172172
173173
:param func: The function to call when this intent is performed.
174+
:param args: Positional arguments to pass to the function.
175+
:param kwargs: Keyword arguments to pass to the function.
174176
"""
175177
func = attr.ib()
178+
args = attr.ib()
179+
kwargs = attr.ib()
180+
181+
def __init__(self, func, *args, **kwargs):
182+
self.func = func
183+
self.args = args
184+
self.kwargs = kwargs
176185

177186

178187
@sync_performer
179188
def perform_func(dispatcher, intent):
180189
"""Performer for :class:`Func`."""
181-
return intent.func()
190+
return intent.func(*intent.args, **intent.kwargs)
182191

183192

184193
base_dispatcher = TypeDispatcher({

effect/test_intents.py

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from testtools import TestCase
88
from testtools.matchers import Equals, MatchesListwise
99

10+
from pytest import raises
11+
1012
from ._base import Effect
1113
from ._dispatcher import ComposedDispatcher, TypeDispatcher
1214
from ._intents import (
@@ -22,51 +24,40 @@
2224
from .test_parallel_performers import EquitableException
2325

2426

25-
class IntentTests(TestCase):
26-
"""Tests for intents."""
27-
28-
def test_perform_constant(self):
29-
"""
30-
perform_constant returns the result of a Constant.
31-
"""
32-
intent = Constant("foo")
33-
result = sync_perform(
34-
TypeDispatcher({Constant: perform_constant}),
35-
Effect(intent))
36-
self.assertEqual(result, "foo")
37-
38-
def test_perform_error(self):
39-
"""
40-
perform_error raises the exception of a Error.
41-
"""
42-
intent = Error(ValueError("foo"))
43-
self.assertRaises(
44-
ValueError,
45-
lambda: sync_perform(
46-
TypeDispatcher({Error: perform_error}),
47-
Effect(intent)))
48-
49-
def test_perform_func(self):
50-
"""
51-
perform_func calls the function given in a Func.
52-
"""
53-
intent = Func(lambda: "foo")
54-
result = sync_perform(
55-
TypeDispatcher({Func: perform_func}),
56-
Effect(intent))
57-
self.assertEqual(result, "foo")
58-
59-
60-
class ParallelTests(TestCase):
61-
"""Tests for :func:`parallel`."""
62-
63-
def test_first_error_str(self):
64-
"""FirstErrors have a pleasing format."""
65-
fe = FirstError(exc_info=(ValueError, ValueError('foo'), None),
66-
index=150)
67-
self.assertEqual(
68-
str(fe),
69-
'(index=150) ValueError: foo')
27+
def test_perform_constant():
28+
"""perform_constant returns the result of a Constant."""
29+
intent = Constant("foo")
30+
result = sync_perform(
31+
TypeDispatcher({Constant: perform_constant}),
32+
Effect(intent))
33+
assert result == "foo"
34+
35+
def test_perform_error():
36+
"""perform_error raises the exception of an Error."""
37+
intent = Error(ValueError("foo"))
38+
with raises(ValueError):
39+
sync_perform(TypeDispatcher({Error: perform_error}), Effect(intent))
40+
41+
def test_perform_func():
42+
"""perform_func calls the function given in a Func."""
43+
intent = Func(lambda: "foo")
44+
result = sync_perform(
45+
TypeDispatcher({Func: perform_func}),
46+
Effect(intent))
47+
assert result == "foo"
48+
49+
def test_perform_func_args_kwargs():
50+
"""arbitrary positional and keyword arguments can be passed to Func."""
51+
f = lambda *a, **kw: (a, kw)
52+
intent = Func(f, 1, 2, key=3)
53+
result = sync_perform(TypeDispatcher({Func: perform_func}), Effect(intent))
54+
assert result == ((1, 2), {'key': 3})
55+
56+
def test_first_error_str():
57+
"""FirstErrors have a pleasing format."""
58+
fe = FirstError(exc_info=(ValueError, ValueError('foo'), None),
59+
index=150)
60+
assert str(fe) == '(index=150) ValueError: foo'
7061

7162

7263
class ParallelAllErrorsTests(TestCase):

0 commit comments

Comments
 (0)