From cfc40a0bf11f037ffd1387bd2c082cd8584fd3a1 Mon Sep 17 00:00:00 2001 From: Andrei <b1naryth1ef@gmail.com> Date: Fri, 7 Oct 2016 06:22:58 -0500 Subject: [PATCH] Cleaner approach to passing client through models --- disco/types/base.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/disco/types/base.py b/disco/types/base.py index 26e8438..fc41614 100644 --- a/disco/types/base.py +++ b/disco/types/base.py @@ -11,13 +11,7 @@ DATETIME_FORMATS = [ def _make(typ, data, client): - if inspect.isfunction(typ): - args, _, _, _ = inspect.getargspec(typ) - if 'client' in args: - return typ(data, client) - elif issubclass(typ, Model): - if not client: - raise Exception() + if inspect.isclass(typ) and issubclass(typ, Model): return typ(data, client) return typ(data) @@ -37,6 +31,7 @@ def listof(typ): if not data: return [] return [_make(typ, obj, client) for obj in data] + _f._takes_client = None return _f @@ -52,6 +47,7 @@ def dictof(typ, key=None): )} else: return {k: _make(typ, v, client) for k, v in six.iteritems(data)} + _f._takes_client = None return _f @@ -123,14 +119,13 @@ class Model(six.with_metaclass(ModelMeta)): continue try: - if client and inspect.isfunction(typ): - args, _, _, _ = inspect.getargspec(typ) - if 'client' in args: + if client: + if inspect.isfunction(typ) and hasattr(typ, '_takes_client'): + v = typ(obj[name], client) + elif inspect.isclass(typ) and issubclass(typ, Model): v = typ(obj[name], client) else: v = typ(obj[name]) - elif inspect.isclass(typ) and issubclass(typ, Model): - v = typ(obj[name], client) else: v = typ(obj[name]) except Exception: