Add copy_docstring helper (#23)
diff --git a/google/auth/_helpers.py b/google/auth/_helpers.py
index 9c49e06..9fe2caa 100644
--- a/google/auth/_helpers.py
+++ b/google/auth/_helpers.py
@@ -21,6 +21,20 @@
from six.moves import urllib
+def copy_docstring(source_class):
+ """Decorator that copies a method's docstring from another class."""
+ def decorator(method):
+ """Decorator implementation."""
+ if method.__doc__:
+ raise ValueError('Method already has a docstring.')
+
+ source_method = getattr(source_class, method.__name__)
+ method.__doc__ = source_method.__doc__
+
+ return method
+ return decorator
+
+
def utcnow():
"""Returns the current UTC datetime.
diff --git a/tests/test__helpers.py b/tests/test__helpers.py
index 86cacef..35c16d4 100644
--- a/tests/test__helpers.py
+++ b/tests/test__helpers.py
@@ -20,6 +20,38 @@
from google.auth import _helpers
+class SourceClass(object):
+ def func(self): # pragma: NO COVER
+ """example docstring"""
+ pass
+
+
+def test_copy_docstring_success():
+ def func(): # pragma: NO COVER
+ pass
+
+ _helpers.copy_docstring(SourceClass)(func)
+
+ assert func.__doc__ == SourceClass.func.__doc__
+
+
+def test_copy_docstring_conflict():
+ def func(): # pragma: NO COVER
+ """existing docstring"""
+ pass
+
+ with pytest.raises(ValueError):
+ _helpers.copy_docstring(SourceClass)(func)
+
+
+def test_copy_docstring_non_existing():
+ def func2(): # pragma: NO COVER
+ pass
+
+ with pytest.raises(AttributeError):
+ _helpers.copy_docstring(SourceClass)(func2)
+
+
def test_utcnow():
assert isinstance(_helpers.utcnow(), datetime.datetime)