Skip to content

Commit b507fe2

Browse files
committed
first stab at typesafe pipes
1 parent 6ce938c commit b507fe2

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/pyff/builtins.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def fork(req, *opts):
268268
return req.t
269269

270270

271+
@deprecated(reason="any pipeline has been replace by other behaviour")
271272
@pipe(name='any')
272273
def _any(lst, d):
273274
for x in lst:

src/pyff/pipes.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import os
88
import traceback
9-
from typing import Any, Dict, Optional
9+
import functools
10+
from typing import Any, Dict, Optional, Callable, Type, Tuple
1011

1112
import yaml
1213
from apscheduler.schedulers.background import BackgroundScheduler
@@ -22,7 +23,34 @@
2223
registry = dict()
2324

2425

25-
def pipe(*args, **kwargs):
26+
def pipe(*args, **kwargs) -> Callable:
27+
def pipe_decorator(f: Callable) -> Callable:
28+
if 'name' in kwargs: # called with the name argument @pipe(name=...) or as @pipe()
29+
f_name = kwargs.get('name', f.__name__)
30+
registry[f_name] = f
31+
32+
@functools.wraps(f)
33+
def wrapper_pipe(*iargs, **ikwargs) -> Any:
34+
opts_type: Optional[Type] = None
35+
if 'opts' in f.__annotations__:
36+
opts_type = f.__annotations__['opts']
37+
38+
if opts_type is not None:
39+
opts_in = ikwargs.pop('opts')
40+
ikwargs['opts'] = opts_type(**dict(list(zip(opts_in[::2], opts_in[1::2]))))
41+
42+
return f(*iargs, **ikwargs)
43+
44+
return wrapper_pipe
45+
46+
if len(args) == 1 and callable(args[0]): # called without arguments @pipe
47+
registry[args[0].__name__] = args[0]
48+
return pipe_decorator(args[0])
49+
else:
50+
return pipe_decorator
51+
52+
53+
def pipe_old(*args, **kwargs):
2654
"""
2755
Register the decorated function in the pyff pipe registry
2856
:param name: optional name - if None, use function name

0 commit comments

Comments
 (0)