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

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

11 statements  

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 """Extends 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 def tmpDecorator(method: Type) -> Callable: 

37 """Decorator to update the documentation from a class with the same 

38 method. 

39 """ 

40 methodName = method.__name__ 

41 if not hasattr(klass, methodName): 

42 raise AttributeError(f"{klass} has no method named {methodName} to inherit from") 

43 appendText = method.__doc__ or "" 

44 method.__doc__ = getattr(klass, methodName).__doc__ + appendText 

45 return method 

46 return tmpDecorator