PageRenderTime 73ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/astropy/utils/decorators.py

https://github.com/eteq/astropy
Python | 1101 lines | 983 code | 15 blank | 103 comment | 11 complexity | 14a44125832118862d824d74b9aad55b MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Licensed under a 3-clause BSD style license - see LICENSE.rst
  3. """Sundry function and class decorators."""
  4. import functools
  5. import inspect
  6. import textwrap
  7. import types
  8. import warnings
  9. from inspect import signature
  10. from .codegen import make_function_with_signature
  11. from .exceptions import (AstropyDeprecationWarning, AstropyUserWarning,
  12. AstropyPendingDeprecationWarning)
  13. __all__ = ['classproperty', 'deprecated', 'deprecated_attribute',
  14. 'deprecated_renamed_argument', 'format_doc',
  15. 'lazyproperty', 'sharedmethod', 'wraps']
  16. _NotFound = object()
  17. def deprecated(since, message='', name='', alternative='', pending=False,
  18. obj_type=None):
  19. """
  20. Used to mark a function or class as deprecated.
  21. To mark an attribute as deprecated, use `deprecated_attribute`.
  22. Parameters
  23. ------------
  24. since : str
  25. The release at which this API became deprecated. This is
  26. required.
  27. message : str, optional
  28. Override the default deprecation message. The format
  29. specifier ``func`` may be used for the name of the function,
  30. and ``alternative`` may be used in the deprecation message
  31. to insert the name of an alternative to the deprecated
  32. function. ``obj_type`` may be used to insert a friendly name
  33. for the type of object being deprecated.
  34. name : str, optional
  35. The name of the deprecated function or class; if not provided
  36. the name is automatically determined from the passed in
  37. function or class, though this is useful in the case of
  38. renamed functions, where the new function is just assigned to
  39. the name of the deprecated function. For example::
  40. def new_function():
  41. ...
  42. oldFunction = new_function
  43. alternative : str, optional
  44. An alternative function or class name that the user may use in
  45. place of the deprecated object. The deprecation warning will
  46. tell the user about this alternative if provided.
  47. pending : bool, optional
  48. If True, uses a AstropyPendingDeprecationWarning instead of a
  49. AstropyDeprecationWarning.
  50. obj_type : str, optional
  51. The type of this object, if the automatically determined one
  52. needs to be overridden.
  53. """
  54. method_types = (classmethod, staticmethod, types.MethodType)
  55. def deprecate_doc(old_doc, message):
  56. """
  57. Returns a given docstring with a deprecation message prepended
  58. to it.
  59. """
  60. if not old_doc:
  61. old_doc = ''
  62. old_doc = textwrap.dedent(old_doc).strip('\n')
  63. new_doc = (('\n.. deprecated:: {since}'
  64. '\n {message}\n\n'.format(
  65. **{'since': since, 'message': message.strip()})) + old_doc)
  66. if not old_doc:
  67. # This is to prevent a spurious 'unexpected unindent' warning from
  68. # docutils when the original docstring was blank.
  69. new_doc += r'\ '
  70. return new_doc
  71. def get_function(func):
  72. """
  73. Given a function or classmethod (or other function wrapper type), get
  74. the function object.
  75. """
  76. if isinstance(func, method_types):
  77. func = func.__func__
  78. return func
  79. def deprecate_function(func, message):
  80. """
  81. Returns a wrapped function that displays an
  82. ``AstropyDeprecationWarning`` when it is called.
  83. """
  84. if isinstance(func, method_types):
  85. func_wrapper = type(func)
  86. else:
  87. func_wrapper = lambda f: f
  88. func = get_function(func)
  89. def deprecated_func(*args, **kwargs):
  90. if pending:
  91. category = AstropyPendingDeprecationWarning
  92. else:
  93. category = AstropyDeprecationWarning
  94. warnings.warn(message, category, stacklevel=2)
  95. return func(*args, **kwargs)
  96. # If this is an extension function, we can't call
  97. # functools.wraps on it, but we normally don't care.
  98. # This crazy way to get the type of a wrapper descriptor is
  99. # straight out of the Python 3.3 inspect module docs.
  100. if type(func) is not type(str.__dict__['__add__']): # nopep8
  101. deprecated_func = functools.wraps(func)(deprecated_func)
  102. deprecated_func.__doc__ = deprecate_doc(
  103. deprecated_func.__doc__, message)
  104. return func_wrapper(deprecated_func)
  105. def deprecate_class(cls, message):
  106. """
  107. Update the docstring and wrap the ``__init__`` in-place (or ``__new__``
  108. if the class or any of the bases overrides ``__new__``) so it will give
  109. a deprecation warning when an instance is created.
  110. This won't work for extension classes because these can't be modified
  111. in-place and the alternatives don't work in the general case:
  112. - Using a new class that looks and behaves like the original doesn't
  113. work because the __new__ method of extension types usually makes sure
  114. that it's the same class or a subclass.
  115. - Subclassing the class and return the subclass can lead to problems
  116. with pickle and will look weird in the Sphinx docs.
  117. """
  118. cls.__doc__ = deprecate_doc(cls.__doc__, message)
  119. if cls.__new__ is object.__new__:
  120. cls.__init__ = deprecate_function(get_function(cls.__init__), message)
  121. else:
  122. cls.__new__ = deprecate_function(get_function(cls.__new__), message)
  123. return cls
  124. def deprecate(obj, message=message, name=name, alternative=alternative,
  125. pending=pending):
  126. if obj_type is None:
  127. if isinstance(obj, type):
  128. obj_type_name = 'class'
  129. elif inspect.isfunction(obj):
  130. obj_type_name = 'function'
  131. elif inspect.ismethod(obj) or isinstance(obj, method_types):
  132. obj_type_name = 'method'
  133. else:
  134. obj_type_name = 'object'
  135. else:
  136. obj_type_name = obj_type
  137. if not name:
  138. name = get_function(obj).__name__
  139. altmessage = ''
  140. if not message or type(message) is type(deprecate):
  141. if pending:
  142. message = ('The {func} {obj_type} will be deprecated in a '
  143. 'future version.')
  144. else:
  145. message = ('The {func} {obj_type} is deprecated and may '
  146. 'be removed in a future version.')
  147. if alternative:
  148. altmessage = '\n Use {} instead.'.format(alternative)
  149. message = ((message.format(**{
  150. 'func': name,
  151. 'name': name,
  152. 'alternative': alternative,
  153. 'obj_type': obj_type_name})) +
  154. altmessage)
  155. if isinstance(obj, type):
  156. return deprecate_class(obj, message)
  157. else:
  158. return deprecate_function(obj, message)
  159. if type(message) is type(deprecate):
  160. return deprecate(message)
  161. return deprecate
  162. def deprecated_attribute(name, since, message=None, alternative=None,
  163. pending=False):
  164. """
  165. Used to mark a public attribute as deprecated. This creates a
  166. property that will warn when the given attribute name is accessed.
  167. To prevent the warning (i.e. for internal code), use the private
  168. name for the attribute by prepending an underscore
  169. (i.e. ``self._name``).
  170. Parameters
  171. ----------
  172. name : str
  173. The name of the deprecated attribute.
  174. since : str
  175. The release at which this API became deprecated. This is
  176. required.
  177. message : str, optional
  178. Override the default deprecation message. The format
  179. specifier ``name`` may be used for the name of the attribute,
  180. and ``alternative`` may be used in the deprecation message
  181. to insert the name of an alternative to the deprecated
  182. function.
  183. alternative : str, optional
  184. An alternative attribute that the user may use in place of the
  185. deprecated attribute. The deprecation warning will tell the
  186. user about this alternative if provided.
  187. pending : bool, optional
  188. If True, uses a AstropyPendingDeprecationWarning instead of a
  189. AstropyDeprecationWarning.
  190. Examples
  191. --------
  192. ::
  193. class MyClass:
  194. # Mark the old_name as deprecated
  195. old_name = misc.deprecated_attribute('old_name', '0.1')
  196. def method(self):
  197. self._old_name = 42
  198. """
  199. private_name = '_' + name
  200. @deprecated(since, name=name, obj_type='attribute')
  201. def get(self):
  202. return getattr(self, private_name)
  203. @deprecated(since, name=name, obj_type='attribute')
  204. def set(self, val):
  205. setattr(self, private_name, val)
  206. @deprecated(since, name=name, obj_type='attribute')
  207. def delete(self):
  208. delattr(self, private_name)
  209. return property(get, set, delete)
  210. def deprecated_renamed_argument(old_name, new_name, since,
  211. arg_in_kwargs=False, relax=False,
  212. pending=False):
  213. """Deprecate a _renamed_ function argument.
  214. The decorator assumes that the argument with the ``old_name`` was removed
  215. from the function signature and the ``new_name`` replaced it at the
  216. **same position** in the signature. If the ``old_name`` argument is
  217. given when calling the decorated function the decorator will catch it and
  218. issue a deprecation warning and pass it on as ``new_name`` argument.
  219. Parameters
  220. ----------
  221. old_name : str or list/tuple thereof
  222. The old name of the argument.
  223. new_name : str or list/tuple thereof
  224. The new name of the argument.
  225. since : str or number or list/tuple thereof
  226. The release at which the old argument became deprecated.
  227. arg_in_kwargs : bool or list/tuple thereof, optional
  228. If the argument is not a named argument (for example it
  229. was meant to be consumed by ``**kwargs``) set this to
  230. ``True``. Otherwise the decorator will throw an Exception
  231. if the ``new_name`` cannot be found in the signature of
  232. the decorated function.
  233. Default is ``False``.
  234. relax : bool or list/tuple thereof, optional
  235. If ``False`` a ``TypeError`` is raised if both ``new_name`` and
  236. ``old_name`` are given. If ``True`` the value for ``new_name`` is used
  237. and a Warning is issued.
  238. Default is ``False``.
  239. pending : bool or list/tuple thereof, optional
  240. If ``True`` this will hide the deprecation warning and ignore the
  241. corresponding ``relax`` parameter value.
  242. Default is ``False``.
  243. Raises
  244. ------
  245. TypeError
  246. If the new argument name cannot be found in the function
  247. signature and arg_in_kwargs was False or if it is used to
  248. deprecate the name of the ``*args``-, ``**kwargs``-like arguments.
  249. At runtime such an Error is raised if both the new_name
  250. and old_name were specified when calling the function and
  251. "relax=False".
  252. Notes
  253. -----
  254. The decorator should be applied to a function where the **name**
  255. of an argument was changed but it applies the same logic.
  256. .. warning::
  257. If ``old_name`` is a list or tuple the ``new_name`` and ``since`` must
  258. also be a list or tuple with the same number of entries. ``relax`` and
  259. ``arg_in_kwarg`` can be a single bool (applied to all) or also a
  260. list/tuple with the same number of entries like ``new_name``, etc.
  261. Examples
  262. --------
  263. The deprecation warnings are not shown in the following examples.
  264. To deprecate a positional or keyword argument::
  265. >>> from astropy.utils.decorators import deprecated_renamed_argument
  266. >>> @deprecated_renamed_argument('sig', 'sigma', '1.0')
  267. ... def test(sigma):
  268. ... return sigma
  269. >>> test(2)
  270. 2
  271. >>> test(sigma=2)
  272. 2
  273. >>> test(sig=2)
  274. 2
  275. To deprecate an argument caught inside the ``**kwargs`` the
  276. ``arg_in_kwargs`` has to be set::
  277. >>> @deprecated_renamed_argument('sig', 'sigma', '1.0',
  278. ... arg_in_kwargs=True)
  279. ... def test(**kwargs):
  280. ... return kwargs['sigma']
  281. >>> test(sigma=2)
  282. 2
  283. >>> test(sig=2)
  284. 2
  285. By default providing the new and old keyword will lead to an Exception. If
  286. a Warning is desired set the ``relax`` argument::
  287. >>> @deprecated_renamed_argument('sig', 'sigma', '1.0', relax=True)
  288. ... def test(sigma):
  289. ... return sigma
  290. >>> test(sig=2)
  291. 2
  292. It is also possible to replace multiple arguments. The ``old_name``,
  293. ``new_name`` and ``since`` have to be `tuple` or `list` and contain the
  294. same number of entries::
  295. >>> @deprecated_renamed_argument(['a', 'b'], ['alpha', 'beta'],
  296. ... ['1.0', 1.2])
  297. ... def test(alpha, beta):
  298. ... return alpha, beta
  299. >>> test(a=2, b=3)
  300. (2, 3)
  301. In this case ``arg_in_kwargs`` and ``relax`` can be a single value (which
  302. is applied to all renamed arguments) or must also be a `tuple` or `list`
  303. with values for each of the arguments.
  304. """
  305. cls_iter = (list, tuple)
  306. if isinstance(old_name, cls_iter):
  307. n = len(old_name)
  308. # Assume that new_name and since are correct (tuple/list with the
  309. # appropriate length) in the spirit of the "consenting adults". But the
  310. # optional parameters may not be set, so if these are not iterables
  311. # wrap them.
  312. if not isinstance(arg_in_kwargs, cls_iter):
  313. arg_in_kwargs = [arg_in_kwargs] * n
  314. if not isinstance(relax, cls_iter):
  315. relax = [relax] * n
  316. if not isinstance(pending, cls_iter):
  317. pending = [pending] * n
  318. else:
  319. # To allow a uniform approach later on, wrap all arguments in lists.
  320. n = 1
  321. old_name = [old_name]
  322. new_name = [new_name]
  323. since = [since]
  324. arg_in_kwargs = [arg_in_kwargs]
  325. relax = [relax]
  326. pending = [pending]
  327. def decorator(function):
  328. # The named arguments of the function.
  329. arguments = signature(function).parameters
  330. keys = list(arguments.keys())
  331. position = [None] * n
  332. for i in range(n):
  333. # Determine the position of the argument.
  334. if new_name[i] in arguments:
  335. param = arguments[new_name[i]]
  336. # There are several possibilities now:
  337. # 1.) Positional or keyword argument:
  338. if param.kind == param.POSITIONAL_OR_KEYWORD:
  339. position[i] = keys.index(new_name[i])
  340. # 2.) Keyword only argument:
  341. elif param.kind == param.KEYWORD_ONLY:
  342. # These cannot be specified by position.
  343. position[i] = None
  344. # 3.) positional-only argument, varargs, varkwargs or some
  345. # unknown type:
  346. else:
  347. raise TypeError('cannot replace argument "{0}" of kind '
  348. '{1!r}.'.format(new_name[i], param.kind))
  349. # In case the argument is not found in the list of arguments
  350. # the only remaining possibility is that it should be caught
  351. # by some kind of **kwargs argument.
  352. # This case has to be explicitly specified, otherwise throw
  353. # an exception!
  354. elif arg_in_kwargs[i]:
  355. position[i] = None
  356. else:
  357. raise TypeError('"{}" was not specified in the function '
  358. 'signature. If it was meant to be part of '
  359. '"**kwargs" then set "arg_in_kwargs" to "True"'
  360. '.'.format(new_name[i]))
  361. @functools.wraps(function)
  362. def wrapper(*args, **kwargs):
  363. for i in range(n):
  364. # The only way to have oldkeyword inside the function is
  365. # that it is passed as kwarg because the oldkeyword
  366. # parameter was renamed to newkeyword.
  367. if old_name[i] in kwargs:
  368. value = kwargs.pop(old_name[i])
  369. # Display the deprecation warning only when it's only
  370. # pending.
  371. if not pending[i]:
  372. warnings.warn(
  373. '"{0}" was deprecated in version {1} '
  374. 'and will be removed in a future version. '
  375. 'Use argument "{2}" instead.'
  376. ''.format(old_name[i], since[i], new_name[i]),
  377. AstropyDeprecationWarning, stacklevel=2)
  378. # Check if the newkeyword was given as well.
  379. newarg_in_args = (position[i] is not None and
  380. len(args) > position[i])
  381. newarg_in_kwargs = new_name[i] in kwargs
  382. if newarg_in_args or newarg_in_kwargs:
  383. if not pending[i]:
  384. # If both are given print a Warning if relax is
  385. # True or raise an Exception is relax is False.
  386. if relax[i]:
  387. warnings.warn(
  388. '"{0}" and "{1}" keywords were set. '
  389. 'Using the value of "{1}".'
  390. ''.format(old_name[i], new_name[i]),
  391. AstropyUserWarning)
  392. else:
  393. raise TypeError(
  394. 'cannot specify both "{}" and "{}"'
  395. '.'.format(old_name[i], new_name[i]))
  396. else:
  397. # If the new argument isn't specified just pass the old
  398. # one with the name of the new argument to the function
  399. kwargs[new_name[i]] = value
  400. return function(*args, **kwargs)
  401. return wrapper
  402. return decorator
  403. # TODO: This can still be made to work for setters by implementing an
  404. # accompanying metaclass that supports it; we just don't need that right this
  405. # second
  406. class classproperty(property):
  407. """
  408. Similar to `property`, but allows class-level properties. That is,
  409. a property whose getter is like a `classmethod`.
  410. The wrapped method may explicitly use the `classmethod` decorator (which
  411. must become before this decorator), or the `classmethod` may be omitted
  412. (it is implicit through use of this decorator).
  413. .. note::
  414. classproperty only works for *read-only* properties. It does not
  415. currently allow writeable/deletable properties, due to subtleties of how
  416. Python descriptors work. In order to implement such properties on a class
  417. a metaclass for that class must be implemented.
  418. Parameters
  419. ----------
  420. fget : callable
  421. The function that computes the value of this property (in particular,
  422. the function when this is used as a decorator) a la `property`.
  423. doc : str, optional
  424. The docstring for the property--by default inherited from the getter
  425. function.
  426. lazy : bool, optional
  427. If True, caches the value returned by the first call to the getter
  428. function, so that it is only called once (used for lazy evaluation
  429. of an attribute). This is analogous to `lazyproperty`. The ``lazy``
  430. argument can also be used when `classproperty` is used as a decorator
  431. (see the third example below). When used in the decorator syntax this
  432. *must* be passed in as a keyword argument.
  433. Examples
  434. --------
  435. ::
  436. >>> class Foo:
  437. ... _bar_internal = 1
  438. ... @classproperty
  439. ... def bar(cls):
  440. ... return cls._bar_internal + 1
  441. ...
  442. >>> Foo.bar
  443. 2
  444. >>> foo_instance = Foo()
  445. >>> foo_instance.bar
  446. 2
  447. >>> foo_instance._bar_internal = 2
  448. >>> foo_instance.bar # Ignores instance attributes
  449. 2
  450. As previously noted, a `classproperty` is limited to implementing
  451. read-only attributes::
  452. >>> class Foo:
  453. ... _bar_internal = 1
  454. ... @classproperty
  455. ... def bar(cls):
  456. ... return cls._bar_internal
  457. ... @bar.setter
  458. ... def bar(cls, value):
  459. ... cls._bar_internal = value
  460. ...
  461. Traceback (most recent call last):
  462. ...
  463. NotImplementedError: classproperty can only be read-only; use a
  464. metaclass to implement modifiable class-level properties
  465. When the ``lazy`` option is used, the getter is only called once::
  466. >>> class Foo:
  467. ... @classproperty(lazy=True)
  468. ... def bar(cls):
  469. ... print("Performing complicated calculation")
  470. ... return 1
  471. ...
  472. >>> Foo.bar
  473. Performing complicated calculation
  474. 1
  475. >>> Foo.bar
  476. 1
  477. If a subclass inherits a lazy `classproperty` the property is still
  478. re-evaluated for the subclass::
  479. >>> class FooSub(Foo):
  480. ... pass
  481. ...
  482. >>> FooSub.bar
  483. Performing complicated calculation
  484. 1
  485. >>> FooSub.bar
  486. 1
  487. """
  488. def __new__(cls, fget=None, doc=None, lazy=False):
  489. if fget is None:
  490. # Being used as a decorator--return a wrapper that implements
  491. # decorator syntax
  492. def wrapper(func):
  493. return cls(func, lazy=lazy)
  494. return wrapper
  495. return super().__new__(cls)
  496. def __init__(self, fget, doc=None, lazy=False):
  497. self._lazy = lazy
  498. if lazy:
  499. self._cache = {}
  500. fget = self._wrap_fget(fget)
  501. super().__init__(fget=fget, doc=doc)
  502. # There is a buglet in Python where self.__doc__ doesn't
  503. # get set properly on instances of property subclasses if
  504. # the doc argument was used rather than taking the docstring
  505. # from fget
  506. # Related Python issue: https://bugs.python.org/issue24766
  507. if doc is not None:
  508. self.__doc__ = doc
  509. def __get__(self, obj, objtype):
  510. if self._lazy and objtype in self._cache:
  511. return self._cache[objtype]
  512. # The base property.__get__ will just return self here;
  513. # instead we pass objtype through to the original wrapped
  514. # function (which takes the class as its sole argument)
  515. val = self.fget.__wrapped__(objtype)
  516. if self._lazy:
  517. self._cache[objtype] = val
  518. return val
  519. def getter(self, fget):
  520. return super().getter(self._wrap_fget(fget))
  521. def setter(self, fset):
  522. raise NotImplementedError(
  523. "classproperty can only be read-only; use a metaclass to "
  524. "implement modifiable class-level properties")
  525. def deleter(self, fdel):
  526. raise NotImplementedError(
  527. "classproperty can only be read-only; use a metaclass to "
  528. "implement modifiable class-level properties")
  529. @staticmethod
  530. def _wrap_fget(orig_fget):
  531. if isinstance(orig_fget, classmethod):
  532. orig_fget = orig_fget.__func__
  533. # Using stock functools.wraps instead of the fancier version
  534. # found later in this module, which is overkill for this purpose
  535. @functools.wraps(orig_fget)
  536. def fget(obj):
  537. return orig_fget(obj.__class__)
  538. return fget
  539. class lazyproperty(property):
  540. """
  541. Works similarly to property(), but computes the value only once.
  542. This essentially memorizes the value of the property by storing the result
  543. of its computation in the ``__dict__`` of the object instance. This is
  544. useful for computing the value of some property that should otherwise be
  545. invariant. For example::
  546. >>> class LazyTest:
  547. ... @lazyproperty
  548. ... def complicated_property(self):
  549. ... print('Computing the value for complicated_property...')
  550. ... return 42
  551. ...
  552. >>> lt = LazyTest()
  553. >>> lt.complicated_property
  554. Computing the value for complicated_property...
  555. 42
  556. >>> lt.complicated_property
  557. 42
  558. As the example shows, the second time ``complicated_property`` is accessed,
  559. the ``print`` statement is not executed. Only the return value from the
  560. first access off ``complicated_property`` is returned.
  561. By default, a setter and deleter are used which simply overwrite and
  562. delete, respectively, the value stored in ``__dict__``. Any user-specified
  563. setter or deleter is executed before executing these default actions.
  564. The one exception is that the default setter is not run if the user setter
  565. already sets the new value in ``__dict__`` and returns that value and the
  566. returned value is not ``None``.
  567. Adapted from the recipe at
  568. http://code.activestate.com/recipes/363602-lazy-property-evaluation
  569. """
  570. def __init__(self, fget, fset=None, fdel=None, doc=None):
  571. super().__init__(fget, fset, fdel, doc)
  572. self._key = self.fget.__name__
  573. def __get__(self, obj, owner=None):
  574. try:
  575. val = obj.__dict__.get(self._key, _NotFound)
  576. if val is not _NotFound:
  577. return val
  578. else:
  579. val = self.fget(obj)
  580. obj.__dict__[self._key] = val
  581. return val
  582. except AttributeError:
  583. if obj is None:
  584. return self
  585. raise
  586. def __set__(self, obj, val):
  587. obj_dict = obj.__dict__
  588. if self.fset:
  589. ret = self.fset(obj, val)
  590. if ret is not None and obj_dict.get(self._key) is ret:
  591. # By returning the value set the setter signals that it took
  592. # over setting the value in obj.__dict__; this mechanism allows
  593. # it to override the input value
  594. return
  595. obj_dict[self._key] = val
  596. def __delete__(self, obj):
  597. if self.fdel:
  598. self.fdel(obj)
  599. if self._key in obj.__dict__:
  600. del obj.__dict__[self._key]
  601. class sharedmethod(classmethod):
  602. """
  603. This is a method decorator that allows both an instancemethod and a
  604. `classmethod` to share the same name.
  605. When using `sharedmethod` on a method defined in a class's body, it
  606. may be called on an instance, or on a class. In the former case it
  607. behaves like a normal instance method (a reference to the instance is
  608. automatically passed as the first ``self`` argument of the method)::
  609. >>> class Example:
  610. ... @sharedmethod
  611. ... def identify(self, *args):
  612. ... print('self was', self)
  613. ... print('additional args were', args)
  614. ...
  615. >>> ex = Example()
  616. >>> ex.identify(1, 2)
  617. self was <astropy.utils.decorators.Example object at 0x...>
  618. additional args were (1, 2)
  619. In the latter case, when the `sharedmethod` is called directly from a
  620. class, it behaves like a `classmethod`::
  621. >>> Example.identify(3, 4)
  622. self was <class 'astropy.utils.decorators.Example'>
  623. additional args were (3, 4)
  624. This also supports a more advanced usage, where the `classmethod`
  625. implementation can be written separately. If the class's *metaclass*
  626. has a method of the same name as the `sharedmethod`, the version on
  627. the metaclass is delegated to::
  628. >>> class ExampleMeta(type):
  629. ... def identify(self):
  630. ... print('this implements the {0}.identify '
  631. ... 'classmethod'.format(self.__name__))
  632. ...
  633. >>> class Example(metaclass=ExampleMeta):
  634. ... @sharedmethod
  635. ... def identify(self):
  636. ... print('this implements the instancemethod')
  637. ...
  638. >>> Example().identify()
  639. this implements the instancemethod
  640. >>> Example.identify()
  641. this implements the Example.identify classmethod
  642. """
  643. def __get__(self, obj, objtype=None):
  644. if obj is None:
  645. mcls = type(objtype)
  646. clsmeth = getattr(mcls, self.__func__.__name__, None)
  647. if callable(clsmeth):
  648. func = clsmeth
  649. else:
  650. func = self.__func__
  651. return self._make_method(func, objtype)
  652. else:
  653. return self._make_method(self.__func__, obj)
  654. @staticmethod
  655. def _make_method(func, instance):
  656. return types.MethodType(func, instance)
  657. def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
  658. updated=functools.WRAPPER_UPDATES, exclude_args=()):
  659. """
  660. An alternative to `functools.wraps` which also preserves the original
  661. function's call signature by way of
  662. `~astropy.utils.codegen.make_function_with_signature`.
  663. This also adds an optional ``exclude_args`` argument. If given it should
  664. be a sequence of argument names that should not be copied from the wrapped
  665. function (either positional or keyword arguments).
  666. The documentation for the original `functools.wraps` follows:
  667. """
  668. wrapped_args = _get_function_args(wrapped, exclude_args=exclude_args)
  669. def wrapper(func):
  670. if '__name__' in assigned:
  671. name = wrapped.__name__
  672. else:
  673. name = func.__name__
  674. func = make_function_with_signature(func, name=name, **wrapped_args)
  675. func = functools.update_wrapper(func, wrapped, assigned=assigned,
  676. updated=updated)
  677. return func
  678. return wrapper
  679. if (isinstance(wraps.__doc__, str) and
  680. wraps.__doc__ is not None and functools.wraps.__doc__ is not None):
  681. wraps.__doc__ += functools.wraps.__doc__
  682. def _get_function_args_internal(func):
  683. """
  684. Utility function for `wraps`.
  685. Reads the argspec for the given function and converts it to arguments
  686. for `make_function_with_signature`.
  687. """
  688. argspec = inspect.getfullargspec(func)
  689. if argspec.defaults:
  690. args = argspec.args[:-len(argspec.defaults)]
  691. kwargs = zip(argspec.args[len(args):], argspec.defaults)
  692. else:
  693. args = argspec.args
  694. kwargs = []
  695. if argspec.kwonlyargs:
  696. kwargs.extend((argname, argspec.kwonlydefaults[argname])
  697. for argname in argspec.kwonlyargs)
  698. return {'args': args, 'kwargs': kwargs, 'varargs': argspec.varargs,
  699. 'varkwargs': argspec.varkw}
  700. def _get_function_args(func, exclude_args=()):
  701. all_args = _get_function_args_internal(func)
  702. if exclude_args:
  703. exclude_args = set(exclude_args)
  704. for arg_type in ('args', 'kwargs'):
  705. all_args[arg_type] = [arg for arg in all_args[arg_type]
  706. if arg not in exclude_args]
  707. for arg_type in ('varargs', 'varkwargs'):
  708. if all_args[arg_type] in exclude_args:
  709. all_args[arg_type] = None
  710. return all_args
  711. def format_doc(docstring, *args, **kwargs):
  712. """
  713. Replaces the docstring of the decorated object and then formats it.
  714. The formatting works like :meth:`str.format` and if the decorated object
  715. already has a docstring this docstring can be included in the new
  716. documentation if you use the ``{__doc__}`` placeholder.
  717. Its primary use is for reusing a *long* docstring in multiple functions
  718. when it is the same or only slightly different between them.
  719. Parameters
  720. ----------
  721. docstring : str or object or None
  722. The docstring that will replace the docstring of the decorated
  723. object. If it is an object like a function or class it will
  724. take the docstring of this object. If it is a string it will use the
  725. string itself. One special case is if the string is ``None`` then
  726. it will use the decorated functions docstring and formats it.
  727. args :
  728. passed to :meth:`str.format`.
  729. kwargs :
  730. passed to :meth:`str.format`. If the function has a (not empty)
  731. docstring the original docstring is added to the kwargs with the
  732. keyword ``'__doc__'``.
  733. Raises
  734. ------
  735. ValueError
  736. If the ``docstring`` (or interpreted docstring if it was ``None``
  737. or not a string) is empty.
  738. IndexError, KeyError
  739. If a placeholder in the (interpreted) ``docstring`` was not filled. see
  740. :meth:`str.format` for more information.
  741. Notes
  742. -----
  743. Using this decorator allows, for example Sphinx, to parse the
  744. correct docstring.
  745. Examples
  746. --------
  747. Replacing the current docstring is very easy::
  748. >>> from astropy.utils.decorators import format_doc
  749. >>> @format_doc('''Perform num1 + num2''')
  750. ... def add(num1, num2):
  751. ... return num1+num2
  752. ...
  753. >>> help(add) # doctest: +SKIP
  754. Help on function add in module __main__:
  755. <BLANKLINE>
  756. add(num1, num2)
  757. Perform num1 + num2
  758. sometimes instead of replacing you only want to add to it::
  759. >>> doc = '''
  760. ... {__doc__}
  761. ... Parameters
  762. ... ----------
  763. ... num1, num2 : Numbers
  764. ... Returns
  765. ... -------
  766. ... result: Number
  767. ... '''
  768. >>> @format_doc(doc)
  769. ... def add(num1, num2):
  770. ... '''Perform addition.'''
  771. ... return num1+num2
  772. ...
  773. >>> help(add) # doctest: +SKIP
  774. Help on function add in module __main__:
  775. <BLANKLINE>
  776. add(num1, num2)
  777. Perform addition.
  778. Parameters
  779. ----------
  780. num1, num2 : Numbers
  781. Returns
  782. -------
  783. result : Number
  784. in case one might want to format it further::
  785. >>> doc = '''
  786. ... Perform {0}.
  787. ... Parameters
  788. ... ----------
  789. ... num1, num2 : Numbers
  790. ... Returns
  791. ... -------
  792. ... result: Number
  793. ... result of num1 {op} num2
  794. ... {__doc__}
  795. ... '''
  796. >>> @format_doc(doc, 'addition', op='+')
  797. ... def add(num1, num2):
  798. ... return num1+num2
  799. ...
  800. >>> @format_doc(doc, 'subtraction', op='-')
  801. ... def subtract(num1, num2):
  802. ... '''Notes: This one has additional notes.'''
  803. ... return num1-num2
  804. ...
  805. >>> help(add) # doctest: +SKIP
  806. Help on function add in module __main__:
  807. <BLANKLINE>
  808. add(num1, num2)
  809. Perform addition.
  810. Parameters
  811. ----------
  812. num1, num2 : Numbers
  813. Returns
  814. -------
  815. result : Number
  816. result of num1 + num2
  817. >>> help(subtract) # doctest: +SKIP
  818. Help on function subtract in module __main__:
  819. <BLANKLINE>
  820. subtract(num1, num2)
  821. Perform subtraction.
  822. Parameters
  823. ----------
  824. num1, num2 : Numbers
  825. Returns
  826. -------
  827. result : Number
  828. result of num1 - num2
  829. Notes : This one has additional notes.
  830. These methods can be combined an even taking the docstring from another
  831. object is possible as docstring attribute. You just have to specify the
  832. object::
  833. >>> @format_doc(add)
  834. ... def another_add(num1, num2):
  835. ... return num1 + num2
  836. ...
  837. >>> help(another_add) # doctest: +SKIP
  838. Help on function another_add in module __main__:
  839. <BLANKLINE>
  840. another_add(num1, num2)
  841. Perform addition.
  842. Parameters
  843. ----------
  844. num1, num2 : Numbers
  845. Returns
  846. -------
  847. result : Number
  848. result of num1 + num2
  849. But be aware that this decorator *only* formats the given docstring not
  850. the strings passed as ``args`` or ``kwargs`` (not even the original
  851. docstring)::
  852. >>> @format_doc(doc, 'addition', op='+')
  853. ... def yet_another_add(num1, num2):
  854. ... '''This one is good for {0}.'''
  855. ... return num1 + num2
  856. ...
  857. >>> help(yet_another_add) # doctest: +SKIP
  858. Help on function yet_another_add in module __main__:
  859. <BLANKLINE>
  860. yet_another_add(num1, num2)
  861. Perform addition.
  862. Parameters
  863. ----------
  864. num1, num2 : Numbers
  865. Returns
  866. -------
  867. result : Number
  868. result of num1 + num2
  869. This one is good for {0}.
  870. To work around it you could specify the docstring to be ``None``::
  871. >>> @format_doc(None, 'addition')
  872. ... def last_add_i_swear(num1, num2):
  873. ... '''This one is good for {0}.'''
  874. ... return num1 + num2
  875. ...
  876. >>> help(last_add_i_swear) # doctest: +SKIP
  877. Help on function last_add_i_swear in module __main__:
  878. <BLANKLINE>
  879. last_add_i_swear(num1, num2)
  880. This one is good for addition.
  881. Using it with ``None`` as docstring allows to use the decorator twice
  882. on an object to first parse the new docstring and then to parse the
  883. original docstring or the ``args`` and ``kwargs``.
  884. """
  885. def set_docstring(obj):
  886. if docstring is None:
  887. # None means: use the objects __doc__
  888. doc = obj.__doc__
  889. # Delete documentation in this case so we don't end up with
  890. # awkwardly self-inserted docs.
  891. obj.__doc__ = None
  892. elif isinstance(docstring, str):
  893. # String: use the string that was given
  894. doc = docstring
  895. else:
  896. # Something else: Use the __doc__ of this
  897. doc = docstring.__doc__
  898. if not doc:
  899. # In case the docstring is empty it's probably not what was wanted.
  900. raise ValueError('docstring must be a string or containing a '
  901. 'docstring that is not empty.')
  902. # If the original has a not-empty docstring append it to the format
  903. # kwargs.
  904. kwargs['__doc__'] = obj.__doc__ or ''
  905. obj.__doc__ = doc.format(*args, **kwargs)
  906. return obj
  907. return set_docstring