Skip to content

Commit 75349f6

Browse files
extended @typed with explicit argtypes and returntye args
1 parent 127caf7 commit 75349f6

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

modeled/typed.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
__all__ = ['base']
2525

26-
from inspect import isclass
26+
from inspect import getargspec, isclass
2727

2828
from decorator import decorator
2929
from moretools import cached, qualname
@@ -84,9 +84,22 @@ def new(self, value, func):
8484
qualname(type(value))))
8585

8686

87-
def typed(func):
88-
from inspect import getfullargspec
89-
spec = getfullargspec(func)
87+
def typed(func=None, argtypes=None, returntype=None):
88+
if func is None:
89+
def typed(func):
90+
global typed
91+
return typed(func, argtypes, returntype)
92+
93+
return typed
94+
95+
if argtypes is not None or returntype is not None:
96+
spec = getargspec(func)
97+
mtypes = {} if argtypes is None else dict(argtypes)
98+
mtypes['return'] = returntype
99+
else:
100+
from inspect import getfullargspec
101+
spec = getfullargspec(func)
102+
mtypes = spec.annotations
90103

91104
def typed(func, *args, **kwargs):
92105
iargs = iter(args)
@@ -103,5 +116,5 @@ def typed(func, *args, **kwargs):
103116
return result
104117

105118
wrapper = decorator(typed, func)
106-
wrapper.mtypes = spec.annotations
119+
wrapper.mtypes = mtypes
107120
return wrapper

0 commit comments

Comments
 (0)