Coverage for tests/test_inheritDoc.py: 67%

48 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-27 11:49 +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# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22import inspect 

23import unittest 

24 

25from lsst.utils import inheritDoc 

26 

27 

28class Base: 

29 """Base class containing some docstrings.""" 

30 

31 def method1() -> None: 

32 """Return Method 1. 

33 

34 A note. 

35 """ 

36 pass 

37 

38 def method2() -> None: 

39 """Return Method 2.""" 

40 pass 

41 

42 def method3() -> None: 

43 pass 

44 

45 def method4() -> None: 

46 """Return Method 4.""" 

47 pass 

48 

49 def method5() -> None: 

50 pass 

51 

52 def method6() -> None: 

53 """Return Method 6. 

54 

55 Content.""" # noqa: D209 

56 pass 

57 

58 

59class NoInheritDoc(Base): 

60 """Class that inherits from base class and inherits docstring.""" 

61 

62 def method2() -> None: 

63 # Docstring inherited. 

64 pass 

65 

66 

67class InheritDoc: 

68 """Class that uses inheritDoc and no explicit inheritance.""" 

69 

70 @inheritDoc(Base) 

71 def method1() -> None: 

72 """Note on method 1. 

73 

74 New line. 

75 """ 

76 pass 

77 

78 @inheritDoc(Base) 

79 def method2() -> None: 

80 """Note on method 2.""" 

81 

82 @inheritDoc(Base) 

83 def method3() -> None: 

84 """Note on method 3.""" 

85 

86 @inheritDoc(Base) 

87 def method4() -> None: 

88 # Will inherit even though not parent class. 

89 pass 

90 

91 @inheritDoc(Base) 

92 def method5() -> None: 

93 # No doc string here or in Base. 

94 pass 

95 

96 @inheritDoc(Base) 

97 def method6() -> None: 

98 """ 

99 Notes 

100 ----- 

101 A note. 

102 """ # noqa: D401 

103 pass 

104 

105 

106class InheritDocTestCase(unittest.TestCase): 

107 """Test inheritDoc functionality.""" 

108 

109 def test_no_inheritdoc(self): 

110 self.assertIsNone(NoInheritDoc.method2.__doc__) 

111 self.assertEqual(inspect.getdoc(NoInheritDoc.method2), "Return Method 2.") 

112 self.assertIsNone(inspect.getdoc(NoInheritDoc.method5)) 

113 

114 def test_inheritDoc(self): 

115 self.assertEqual( 

116 inspect.getdoc(InheritDoc.method1), 

117 """Return Method 1. 

118 

119A note. 

120 

121Note on method 1. 

122 

123New line.""", 

124 ) 

125 self.assertEqual( 

126 inspect.getdoc(InheritDoc.method2), 

127 """Return Method 2. 

128 

129Note on method 2.""", 

130 ) 

131 self.assertEqual(inspect.getdoc(InheritDoc.method3), "Note on method 3.") 

132 self.assertEqual(inspect.getdoc(InheritDoc.method4), "Return Method 4.") 

133 self.assertIsNone(inspect.getdoc(InheritDoc.method5)) 

134 

135 self.assertEqual( 

136 inspect.getdoc(InheritDoc.method6), 

137 """Return Method 6. 

138 

139Content. 

140 

141Notes 

142----- 

143A note.""", 

144 ) 

145 

146 

147if __name__ == "__main__": 

148 unittest.main()