Skip to content

Commit 0f28a51

Browse files
committed
update doc decorator from func to class
1 parent 8f9e12c commit 0f28a51

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

pandas/util/_decorators.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]:
245245
return decorate
246246

247247

248-
def doc(*args: Union[str, Callable], **kwargs: str) -> Callable[[F], F]:
248+
class doc:
249249
"""
250250
A decorator take docstring templates, concatenate them and perform string
251251
substitution on it.
@@ -265,37 +265,36 @@ def doc(*args: Union[str, Callable], **kwargs: str) -> Callable[[F], F]:
265265
The string which would be used to format docstring template.
266266
"""
267267

268-
def decorator(func: F) -> F:
269-
@wraps(func)
270-
def wrapper(*args, **kwargs) -> Callable:
271-
return func(*args, **kwargs)
268+
def __init__(self, *args: Union[str, Callable], **kwargs: str) -> None:
269+
self.appenders = args
270+
self.substitution = kwargs
272271

272+
def __call__(self, func: F) -> F:
273273
# collecting docstring and docstring templates
274274
docstring_components: List[Union[str, Callable]] = []
275275
if func.__doc__:
276276
docstring_components.append(dedent(func.__doc__))
277277

278-
for arg in args:
279-
if hasattr(arg, "_docstring_components"):
280-
docstring_components.extend(arg._docstring_components) # type: ignore
281-
elif isinstance(arg, str) or arg.__doc__:
282-
docstring_components.append(arg)
278+
for appender in self.appenders:
279+
if hasattr(appender, "_docstring_components"):
280+
docstring_components.extend(
281+
appender._docstring_components # type: ignore
282+
)
283+
elif isinstance(appender, str) or appender.__doc__:
284+
docstring_components.append(appender)
283285

284286
# formatting templates and concatenating docstring
285-
wrapper.__doc__ = "".join(
287+
func.__doc__ = "".join(
286288
[
287-
arg.format(**kwargs)
288-
if isinstance(arg, str)
289-
else dedent(arg.__doc__ or "")
290-
for arg in docstring_components
289+
component.format(**self.substitution)
290+
if isinstance(component, str)
291+
else dedent(component.__doc__ or "")
292+
for component in docstring_components
291293
]
292294
)
293295

294-
wrapper._docstring_components = docstring_components # type: ignore
295-
296-
return cast(F, wrapper)
297-
298-
return decorator
296+
func._docstring_components = docstring_components # type: ignore
297+
return func
299298

300299

301300
# Substitution and Appender are derived from matplotlib.docstring (1.1.0)

0 commit comments

Comments
 (0)