@@ -245,7 +245,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]:
245
245
return decorate
246
246
247
247
248
- def doc ( * args : Union [ str , Callable ], ** kwargs : str ) -> Callable [[ F ], F ] :
248
+ class doc :
249
249
"""
250
250
A decorator take docstring templates, concatenate them and perform string
251
251
substitution on it.
@@ -265,37 +265,36 @@ def doc(*args: Union[str, Callable], **kwargs: str) -> Callable[[F], F]:
265
265
The string which would be used to format docstring template.
266
266
"""
267
267
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
272
271
272
+ def __call__ (self , func : F ) -> F :
273
273
# collecting docstring and docstring templates
274
274
docstring_components : List [Union [str , Callable ]] = []
275
275
if func .__doc__ :
276
276
docstring_components .append (dedent (func .__doc__ ))
277
277
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 )
283
285
284
286
# formatting templates and concatenating docstring
285
- wrapper .__doc__ = "" .join (
287
+ func .__doc__ = "" .join (
286
288
[
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
291
293
]
292
294
)
293
295
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
299
298
300
299
301
300
# Substitution and Appender are derived from matplotlib.docstring (1.1.0)
0 commit comments