Coverage for C:\Repos\ekr-pylint\pylint\config\arguments_provider.py: 68%

37 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE 

3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5"""Arguments provider class used to expose options.""" 

6 

7from __future__ import annotations 

8 

9import argparse 

10import optparse # pylint: disable=deprecated-module 

11import warnings 

12from collections.abc import Iterator 

13from typing import Any 

14 

15from pylint.config.arguments_manager import _ArgumentsManager 

16from pylint.typing import OptionDict, Options 

17 

18 

19class UnsupportedAction(Exception): 

20 """Raised by set_option when it doesn't know what to do for an action.""" 

21 

22 def __init__(self, *args: object) -> None: 

23 # TODO: 3.0: Remove deprecated exception 

24 warnings.warn( 

25 "UnsupportedAction has been deprecated and will be removed in pylint 3.0", 

26 DeprecationWarning, 

27 ) 

28 super().__init__(*args) 

29 

30 

31class _ArgumentsProvider: 

32 """Base class for classes that provide arguments.""" 

33 

34 name: str 

35 """Name of the provider.""" 

36 

37 options: Options = () 

38 """Options provided by this provider.""" 

39 

40 option_groups_descs: dict[str, str] = {} 

41 """Option groups of this provider and their descriptions.""" 

42 

43 def __init__(self, arguments_manager: _ArgumentsManager) -> None: 

44 self._arguments_manager = arguments_manager 

45 """The manager that will parse and register any options provided.""" 

46 

47 self._arguments_manager._register_options_provider(self) 

48 

49 self._level = 0 

50 

51 @property 

52 def level(self) -> int: 

53 # TODO: 3.0: Remove deprecated attribute 

54 warnings.warn( 

55 "The level attribute has been deprecated. It was used to display the checker in the help or not," 

56 " and everything is displayed in the help now. It will be removed in pylint 3.0.", 

57 DeprecationWarning, 

58 ) 

59 return self._level 

60 

61 @level.setter 

62 def level(self, value: int) -> None: 

63 # TODO: 3.0: Remove deprecated attribute 

64 warnings.warn( 

65 "Setting the level attribute has been deprecated. It was used to display the checker in the help or not," 

66 " and everything is displayed in the help now. It will be removed in pylint 3.0.", 

67 DeprecationWarning, 

68 ) 

69 self._level = value 

70 

71 @property 

72 def config(self) -> argparse.Namespace: 

73 # TODO: 3.0: Remove deprecated attribute 

74 warnings.warn( 

75 "The checker-specific config attribute has been deprecated. Please use " 

76 "'linter.config' to access the global configuration object.", 

77 DeprecationWarning, 

78 ) 

79 return self._arguments_manager.config 

80 

81 def load_defaults(self) -> None: # pragma: no cover 

82 """DEPRECATED: Initialize the provider using default values.""" 

83 warnings.warn( 

84 "load_defaults has been deprecated. Option groups should be " 

85 "registered by initializing an ArgumentsProvider. " 

86 "This automatically registers the group on the ArgumentsManager.", 

87 DeprecationWarning, 

88 ) 

89 for opt, optdict in self.options: 

90 action = optdict.get("action") 

91 if action != "callback": 

92 # callback action have no default 

93 if optdict is None: 

94 with warnings.catch_warnings(): 

95 warnings.filterwarnings("ignore", category=DeprecationWarning) 

96 optdict = self.get_option_def(opt) 

97 default = optdict.get("default") 

98 self.set_option(opt, default, action, optdict) 

99 

100 def option_attrname( 

101 self, opt: str, optdict: OptionDict | None = None 

102 ) -> str: # pragma: no cover 

103 """DEPRECATED: Get the config attribute corresponding to opt.""" 

104 warnings.warn( 

105 "option_attrname has been deprecated. It will be removed " 

106 "in a future release.", 

107 DeprecationWarning, 

108 ) 

109 if optdict is None: 

110 with warnings.catch_warnings(): 

111 warnings.filterwarnings("ignore", category=DeprecationWarning) 

112 optdict = self.get_option_def(opt) 

113 return optdict.get("dest", opt.replace("-", "_")) # type: ignore[return-value] 

114 

115 def option_value(self, opt: str) -> Any: # pragma: no cover 

116 """DEPRECATED: Get the current value for the given option.""" 

117 warnings.warn( 

118 "option_value has been deprecated. It will be removed " 

119 "in a future release.", 

120 DeprecationWarning, 

121 ) 

122 return getattr(self._arguments_manager.config, opt.replace("-", "_"), None) 

123 

124 # pylint: disable-next=unused-argument 

125 def set_option(self, optname, value, action=None, optdict=None): # pragma: no cover 

126 """DEPRECATED: Method called to set an option (registered in the options list).""" 

127 # TODO: 3.0: Remove deprecated method. 

128 warnings.warn( 

129 "set_option has been deprecated. You can use _arguments_manager.set_option " 

130 "or linter.set_option to set options on the global configuration object.", 

131 DeprecationWarning, 

132 ) 

133 self._arguments_manager.set_option(optname, value) 

134 

135 def get_option_def(self, opt: str) -> OptionDict: # pragma: no cover 

136 """DEPRECATED: Return the dictionary defining an option given its name. 

137 

138 :raises OptionError: If the option isn't found. 

139 """ 

140 warnings.warn( 

141 "get_option_def has been deprecated. It will be removed " 

142 "in a future release.", 

143 DeprecationWarning, 

144 ) 

145 assert self.options 

146 for option in self.options: 

147 if option[0] == opt: 

148 return option[1] 

149 raise optparse.OptionError( 

150 f"no such option {opt} in section {self.name!r}", opt # type: ignore[arg-type] 

151 ) 

152 

153 def options_by_section( 

154 self, 

155 ) -> Iterator[ 

156 tuple[str, list[tuple[str, OptionDict, Any]]] 

157 | tuple[None, dict[str, list[tuple[str, OptionDict, Any]]]] 

158 ]: # pragma: no cover 

159 """DEPRECATED: Return an iterator on options grouped by section. 

160 

161 (section, [list of (optname, optdict, optvalue)]) 

162 """ 

163 warnings.warn( 

164 "options_by_section has been deprecated. It will be removed " 

165 "in a future release.", 

166 DeprecationWarning, 

167 ) 

168 sections: dict[str, list[tuple[str, OptionDict, Any]]] = {} 

169 for optname, optdict in self.options: 

170 with warnings.catch_warnings(): 

171 warnings.filterwarnings("ignore", category=DeprecationWarning) 

172 sections.setdefault(optdict.get("group"), []).append( # type: ignore[arg-type] 

173 (optname, optdict, self.option_value(optname)) 

174 ) 

175 if None in sections: 

176 yield None, sections.pop(None) # type: ignore[call-overload] 

177 for section, options in sorted(sections.items()): 

178 yield section.upper(), options 

179 

180 def options_and_values( 

181 self, options: Options | None = None 

182 ) -> Iterator[tuple[str, OptionDict, Any]]: # pragma: no cover 

183 """DEPRECATED.""" 

184 warnings.warn( 

185 "options_and_values has been deprecated. It will be removed " 

186 "in a future release.", 

187 DeprecationWarning, 

188 ) 

189 if options is None: 

190 options = self.options 

191 for optname, optdict in options: 

192 with warnings.catch_warnings(): 

193 warnings.filterwarnings("ignore", category=DeprecationWarning) 

194 yield optname, optdict, self.option_value(optname)