Coverage for python/lsst/utils/inheritDoc.py: 23%
11 statements
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-29 09:54 +0000
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-29 09:54 +0000
1# This file is part of utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12__all__ = ("inheritDoc",)
14from typing import Callable, Type
17def inheritDoc(klass: Type) -> Callable:
18 """Extend existing documentation for a method that exists in another
19 class and extend it with any additional documentation defined.
21 This decorator takes a class from which to draw documentation from as an
22 argument. This is so that any class may be used as a source of
23 documentation and not just the immediate parent of a class. This is useful
24 when there may be a long inheritance chain, or in the case of mixins.
26 Parameters
27 ----------
28 klass : object
29 The class to inherit documentation from.
31 Returns
32 -------
33 decorator : callable
34 Intermediate decorator used in the documentation process.
35 """
37 def tmpDecorator(method: Type) -> Callable:
38 """Update the documentation from a class with the same method."""
39 methodName = method.__name__
40 if not hasattr(klass, methodName):
41 raise AttributeError(f"{klass} has no method named {methodName} to inherit from")
42 appendText = method.__doc__ or ""
43 method.__doc__ = getattr(klass, methodName).__doc__ + appendText
44 return method
46 return tmpDecorator