Coverage for python/lsst/utils/inheritDoc.py: 23%

11 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-06-02 03:24 -0700

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. 

11 

12__all__ = ("inheritDoc",) 

13 

14from typing import Callable, Type 

15 

16 

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. 

20 

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. 

25 

26 Parameters 

27 ---------- 

28 klass : object 

29 The class to inherit documentation from. 

30 

31 Returns 

32 ------- 

33 decorator : callable 

34 Intermediate decorator used in the documentation process. 

35 """ 

36 

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 

45 

46 return tmpDecorator