summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/util')
-rw-r--r--lib/sqlalchemy/util/__init__.py2
-rw-r--r--lib/sqlalchemy/util/langhelpers.py26
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py
index ed968f168..36a81dbce 100644
--- a/lib/sqlalchemy/util/__init__.py
+++ b/lib/sqlalchemy/util/__init__.py
@@ -36,7 +36,7 @@ from .langhelpers import iterate_attributes, class_hierarchy, \
generic_repr, counter, PluginLoader, hybridproperty, hybridmethod, \
safe_reraise,\
get_callable_argspec, only_once, attrsetter, ellipses_string, \
- warn_limited, map_bits, MemoizedSlots, EnsureKWArgType
+ warn_limited, map_bits, MemoizedSlots, EnsureKWArgType, wrap_callable
from .deprecations import warn_deprecated, warn_pending_deprecation, \
deprecated, pending_deprecation, inject_docstring_text
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