Skip to content

Commit eea104e

Browse files
committed
use dostring_wrapper class
1 parent d093278 commit eea104e

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
from pandas import compat
7979
from pandas.compat.numpy import function as nv
8080
from pandas.util.decorators import (deprecate_kwarg, Appender,
81-
Substitution, generate_dynamic_docstring)
81+
Substitution, docstring_wrapper)
8282
from pandas.util.validators import validate_bool_kwarg
8383

8484
from pandas.tseries.period import PeriodIndex
@@ -945,7 +945,7 @@ def to_gbq(self, destination_table, project_id, chunksize=10000,
945945
def _f():
946946
from pandas.io import gbq
947947
return gbq.to_gbq.__doc__
948-
to_gbq.__doc__ = generate_dynamic_docstring(_f)
948+
to_gbq = docstring_wrapper(to_gbq, _f)
949949

950950
@classmethod
951951
def from_records(cls, data, index=None, exclude=None, columns=None,

pandas/io/gbq.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" Google BigQuery support """
22

3-
from pandas.util.decorators import generate_dynamic_docstring
3+
from pandas.util.decorators import docstring_wrapper
44

55

66
def _try_import():
@@ -31,8 +31,7 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
3131
**kwargs)
3232

3333

34-
read_gbq.__doc__ = generate_dynamic_docstring(
35-
lambda: _try_import().read_gbq.__doc__)
34+
read_gbq = docstring_wrapper(read_gbq, lambda: _try_import().read_gbq.__doc__)
3635

3736

3837
def to_gbq(dataframe, destination_table, project_id, chunksize=10000,
@@ -44,5 +43,4 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=10000,
4443
if_exists=if_exists, private_key=private_key)
4544

4645

47-
to_gbq.__doc__ = generate_dynamic_docstring(
48-
lambda: _try_import().to_gbq.__doc__)
46+
to_gbq = docstring_wrapper(to_gbq, lambda: _try_import().to_gbq.__doc__)

pandas/util/decorators.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import warnings
55
from textwrap import dedent
6-
from functools import wraps
6+
from functools import wraps, update_wrapper
77

88

99
def deprecate(name, alternative, alt_name=None):
@@ -235,22 +235,20 @@ def make_signature(func):
235235
return args, spec.args
236236

237237

238-
def generate_dynamic_docstring(creator):
239-
"""
240-
this function returns a dynamically generated doc-string
241-
that will call the creator callable to actually
242-
create the docstring.
243-
244-
The point of this is that we want to get a docstring
245-
from another module, but don't want to actually
246-
import that module until we look up the docstring
247-
rather than at function definition time
248-
"""
238+
class docstring_wrapper(object):
239+
""" wrap a function, provide a dynamically evaluated doc-string """
249240

250-
class Docstring(str):
241+
def __init__(self, func, creator):
242+
self.func = func
243+
self.creator = creator
244+
update_wrapper(
245+
self, func, ['__module__', '__name__',
246+
'__qualname__', '__annotations__'])
251247

252-
@staticmethod
253-
def expandtabs(*args):
254-
return creator().expandtabs(*args)
248+
def __call__(*args, **kwargs):
249+
self = args[0]
250+
return self.func(*args[1:], **kwargs)
255251

256-
return Docstring()
252+
@property
253+
def __doc__(self):
254+
return self.creator()

0 commit comments

Comments
 (0)