diff options
Diffstat (limited to 'lib/sqlalchemy/util/langhelpers.py')
-rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 743afccfd..9f259aea3 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -1377,3 +1377,29 @@ class EnsureKWArgType(type): return fn(*arg) return update_wrapper(wrap, fn) + +def wrap_callable(fn): + """Wrap callable and set __name__, __doc__, and __module__. + + :param fn: + object with __call__ method + """ + if hasattr(fn, '__name__'): + _f = update_wrapper(lambda ctx: fn(), fn) + _f.__doc__ = _f.__doc__ or fn.__name__ + return _f + else: + _f = lambda ctx: fn() + _f.__name__ = fn.__class__.__name__ + _f.__module__ = fn.__module__ + + if hasattr(fn.__call__, '__doc__') and fn.__call__.__doc__: + _f.__doc__ = fn.__call__.__doc__ + elif hasattr(fn.__class__, '__doc__') and fn.__class__.__doc__: + _f.__doc__ = fn.__class__.__doc__ + elif fn.__doc__: + _f.__doc__ = fn.__doc__ + else: + _f.__doc__ = fn.__class__.__name__ + + return _f |