argument.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. """Definition of an Argument class and transformers for various argument types.
  5. An Argument instance represents a pylint option to be handled by an argparse.ArgumentParser
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import os
  10. import pathlib
  11. import re
  12. import sys
  13. from collections.abc import Callable
  14. from glob import glob
  15. from typing import Any, Pattern, Sequence, Tuple, Union
  16. from pylint import interfaces
  17. from pylint import utils as pylint_utils
  18. from pylint.config.callback_actions import _CallbackAction, _ExtendAction
  19. from pylint.config.deprecation_actions import _NewNamesAction, _OldNamesAction
  20. from pylint.constants import PY38_PLUS
  21. if sys.version_info >= (3, 8):
  22. from typing import Literal
  23. else:
  24. from typing_extensions import Literal
  25. _ArgumentTypes = Union[
  26. str,
  27. int,
  28. float,
  29. bool,
  30. Pattern[str],
  31. Sequence[str],
  32. Sequence[Pattern[str]],
  33. Tuple[int, ...],
  34. ]
  35. """List of possible argument types."""
  36. def _confidence_transformer(value: str) -> Sequence[str]:
  37. """Transforms a comma separated string of confidence values."""
  38. if not value:
  39. return interfaces.CONFIDENCE_LEVEL_NAMES
  40. values = pylint_utils._check_csv(value)
  41. for confidence in values:
  42. if confidence not in interfaces.CONFIDENCE_LEVEL_NAMES:
  43. raise argparse.ArgumentTypeError(
  44. f"{value} should be in {*interfaces.CONFIDENCE_LEVEL_NAMES,}"
  45. )
  46. return values
  47. def _csv_transformer(value: str) -> Sequence[str]:
  48. """Transforms a comma separated string."""
  49. return pylint_utils._check_csv(value)
  50. YES_VALUES = {"y", "yes", "true"}
  51. NO_VALUES = {"n", "no", "false"}
  52. def _yn_transformer(value: str) -> bool:
  53. """Transforms a yes/no or stringified bool into a bool."""
  54. value = value.lower()
  55. if value in YES_VALUES:
  56. return True
  57. if value in NO_VALUES:
  58. return False
  59. raise argparse.ArgumentTypeError(
  60. None, f"Invalid yn value '{value}', should be in {*YES_VALUES, *NO_VALUES}"
  61. )
  62. def _non_empty_string_transformer(value: str) -> str:
  63. """Check that a string is not empty and remove quotes."""
  64. if not value:
  65. raise argparse.ArgumentTypeError("Option cannot be an empty string.")
  66. return pylint_utils._unquote(value)
  67. def _path_transformer(value: str) -> str:
  68. """Expand user and variables in a path."""
  69. return os.path.expandvars(os.path.expanduser(value))
  70. def _glob_paths_csv_transformer(value: str) -> Sequence[str]:
  71. """Transforms a comma separated list of paths while expanding user and
  72. variables and glob patterns.
  73. """
  74. paths: list[str] = []
  75. for path in _csv_transformer(value):
  76. paths.extend(glob(_path_transformer(path), recursive=True))
  77. return paths
  78. def _py_version_transformer(value: str) -> tuple[int, ...]:
  79. """Transforms a version string into a version tuple."""
  80. try:
  81. version = tuple(int(val) for val in value.replace(",", ".").split("."))
  82. except ValueError:
  83. raise argparse.ArgumentTypeError(
  84. f"{value} has an invalid format, should be a version string. E.g., '3.8'"
  85. ) from None
  86. return version
  87. def _regex_transformer(value: str) -> Pattern[str]:
  88. """Return `re.compile(value)`."""
  89. try:
  90. return re.compile(value)
  91. except re.error as e:
  92. msg = f"Error in provided regular expression: {value} beginning at index {e.pos}: {e.msg}"
  93. raise argparse.ArgumentTypeError(msg) from e
  94. def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
  95. """Transforms a comma separated list of regular expressions."""
  96. patterns: list[Pattern[str]] = []
  97. for pattern in _csv_transformer(value):
  98. patterns.append(_regex_transformer(pattern))
  99. return patterns
  100. def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
  101. """Transforms a comma separated list of regular expressions paths."""
  102. patterns: list[Pattern[str]] = []
  103. for pattern in _csv_transformer(value):
  104. patterns.append(
  105. re.compile(
  106. str(pathlib.PureWindowsPath(pattern)).replace("\\", "\\\\")
  107. + "|"
  108. + pathlib.PureWindowsPath(pattern).as_posix()
  109. )
  110. )
  111. return patterns
  112. _TYPE_TRANSFORMERS: dict[str, Callable[[str], _ArgumentTypes]] = {
  113. "choice": str,
  114. "csv": _csv_transformer,
  115. "float": float,
  116. "int": int,
  117. "confidence": _confidence_transformer,
  118. "non_empty_string": _non_empty_string_transformer,
  119. "path": _path_transformer,
  120. "glob_paths_csv": _glob_paths_csv_transformer,
  121. "py_version": _py_version_transformer,
  122. "regexp": _regex_transformer,
  123. "regexp_csv": _regexp_csv_transfomer,
  124. "regexp_paths_csv": _regexp_paths_csv_transfomer,
  125. "string": pylint_utils._unquote,
  126. "yn": _yn_transformer,
  127. }
  128. """Type transformers for all argument types.
  129. A transformer should accept a string and return one of the supported
  130. Argument types. It will only be called when parsing 1) command-line,
  131. 2) configuration files and 3) a string default value.
  132. Non-string default values are assumed to be of the correct type.
  133. """
  134. class _Argument:
  135. """Class representing an argument to be parsed by an argparse.ArgumentsParser.
  136. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  137. See:
  138. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  139. """
  140. def __init__(
  141. self,
  142. *,
  143. flags: list[str],
  144. arg_help: str,
  145. hide_help: bool,
  146. section: str | None,
  147. ) -> None:
  148. self.flags = flags
  149. """The name of the argument."""
  150. self.hide_help = hide_help
  151. """Whether to hide this argument in the help message."""
  152. # argparse uses % formatting on help strings, so a % needs to be escaped
  153. self.help = arg_help.replace("%", "%%")
  154. """The description of the argument."""
  155. if hide_help:
  156. self.help = argparse.SUPPRESS
  157. self.section = section
  158. """The section to add this argument to."""
  159. class _BaseStoreArgument(_Argument):
  160. """Base class for store arguments to be parsed by an argparse.ArgumentsParser.
  161. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  162. See:
  163. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  164. """
  165. def __init__(
  166. self,
  167. *,
  168. flags: list[str],
  169. action: str,
  170. default: _ArgumentTypes,
  171. arg_help: str,
  172. hide_help: bool,
  173. section: str | None,
  174. ) -> None:
  175. super().__init__(
  176. flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
  177. )
  178. self.action = action
  179. """The action to perform with the argument."""
  180. self.default = default
  181. """The default value of the argument."""
  182. class _StoreArgument(_BaseStoreArgument):
  183. """Class representing a store argument to be parsed by an argparse.ArgumentsParser.
  184. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  185. See:
  186. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  187. """
  188. def __init__(
  189. self,
  190. *,
  191. flags: list[str],
  192. action: str,
  193. default: _ArgumentTypes,
  194. arg_type: str,
  195. choices: list[str] | None,
  196. arg_help: str,
  197. metavar: str,
  198. hide_help: bool,
  199. section: str | None,
  200. ) -> None:
  201. super().__init__(
  202. flags=flags,
  203. action=action,
  204. default=default,
  205. arg_help=arg_help,
  206. hide_help=hide_help,
  207. section=section,
  208. )
  209. self.type = _TYPE_TRANSFORMERS[arg_type]
  210. """A transformer function that returns a transformed type of the argument."""
  211. self.choices = choices
  212. """A list of possible choices for the argument.
  213. None if there are no restrictions.
  214. """
  215. self.metavar = metavar
  216. """The metavar of the argument.
  217. See:
  218. https://docs.python.org/3/library/argparse.html#metavar
  219. """
  220. class _StoreTrueArgument(_BaseStoreArgument):
  221. """Class representing a 'store_true' argument to be parsed by an
  222. argparse.ArgumentsParser.
  223. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  224. See:
  225. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  226. """
  227. # pylint: disable-next=useless-parent-delegation # We narrow down the type of action
  228. def __init__(
  229. self,
  230. *,
  231. flags: list[str],
  232. action: Literal["store_true"],
  233. default: _ArgumentTypes,
  234. arg_help: str,
  235. hide_help: bool,
  236. section: str | None,
  237. ) -> None:
  238. super().__init__(
  239. flags=flags,
  240. action=action,
  241. default=default,
  242. arg_help=arg_help,
  243. hide_help=hide_help,
  244. section=section,
  245. )
  246. class _DeprecationArgument(_Argument):
  247. """Store arguments while also handling deprecation warnings for old and new names.
  248. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  249. See:
  250. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  251. """
  252. def __init__(
  253. self,
  254. *,
  255. flags: list[str],
  256. action: type[argparse.Action],
  257. default: _ArgumentTypes,
  258. arg_type: str,
  259. choices: list[str] | None,
  260. arg_help: str,
  261. metavar: str,
  262. hide_help: bool,
  263. section: str | None,
  264. ) -> None:
  265. super().__init__(
  266. flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
  267. )
  268. self.action = action
  269. """The action to perform with the argument."""
  270. self.default = default
  271. """The default value of the argument."""
  272. self.type = _TYPE_TRANSFORMERS[arg_type]
  273. """A transformer function that returns a transformed type of the argument."""
  274. self.choices = choices
  275. """A list of possible choices for the argument.
  276. None if there are no restrictions.
  277. """
  278. self.metavar = metavar
  279. """The metavar of the argument.
  280. See:
  281. https://docs.python.org/3/library/argparse.html#metavar
  282. """
  283. class _ExtendArgument(_DeprecationArgument):
  284. """Class for extend arguments to be parsed by an argparse.ArgumentsParser.
  285. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  286. See:
  287. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  288. """
  289. def __init__(
  290. self,
  291. *,
  292. flags: list[str],
  293. action: Literal["extend"],
  294. default: _ArgumentTypes,
  295. arg_type: str,
  296. metavar: str,
  297. arg_help: str,
  298. hide_help: bool,
  299. section: str | None,
  300. choices: list[str] | None,
  301. dest: str | None,
  302. ) -> None:
  303. # The extend action is included in the stdlib from 3.8+
  304. if PY38_PLUS:
  305. action_class = argparse._ExtendAction
  306. else:
  307. action_class = _ExtendAction # type: ignore[assignment]
  308. self.dest = dest
  309. """The destination of the argument."""
  310. super().__init__(
  311. flags=flags,
  312. action=action_class,
  313. default=default,
  314. arg_type=arg_type,
  315. choices=choices,
  316. arg_help=arg_help,
  317. metavar=metavar,
  318. hide_help=hide_help,
  319. section=section,
  320. )
  321. class _StoreOldNamesArgument(_DeprecationArgument):
  322. """Store arguments while also handling old names.
  323. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  324. See:
  325. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  326. """
  327. def __init__(
  328. self,
  329. *,
  330. flags: list[str],
  331. default: _ArgumentTypes,
  332. arg_type: str,
  333. choices: list[str] | None,
  334. arg_help: str,
  335. metavar: str,
  336. hide_help: bool,
  337. kwargs: dict[str, Any],
  338. section: str | None,
  339. ) -> None:
  340. super().__init__(
  341. flags=flags,
  342. action=_OldNamesAction,
  343. default=default,
  344. arg_type=arg_type,
  345. choices=choices,
  346. arg_help=arg_help,
  347. metavar=metavar,
  348. hide_help=hide_help,
  349. section=section,
  350. )
  351. self.kwargs = kwargs
  352. """Any additional arguments passed to the action."""
  353. class _StoreNewNamesArgument(_DeprecationArgument):
  354. """Store arguments while also emitting deprecation warnings.
  355. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  356. See:
  357. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  358. """
  359. def __init__(
  360. self,
  361. *,
  362. flags: list[str],
  363. default: _ArgumentTypes,
  364. arg_type: str,
  365. choices: list[str] | None,
  366. arg_help: str,
  367. metavar: str,
  368. hide_help: bool,
  369. kwargs: dict[str, Any],
  370. section: str | None,
  371. ) -> None:
  372. super().__init__(
  373. flags=flags,
  374. action=_NewNamesAction,
  375. default=default,
  376. arg_type=arg_type,
  377. choices=choices,
  378. arg_help=arg_help,
  379. metavar=metavar,
  380. hide_help=hide_help,
  381. section=section,
  382. )
  383. self.kwargs = kwargs
  384. """Any additional arguments passed to the action."""
  385. class _CallableArgument(_Argument):
  386. """Class representing an callable argument to be parsed by an
  387. argparse.ArgumentsParser.
  388. This is based on the parameters passed to argparse.ArgumentsParser.add_message.
  389. See:
  390. https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
  391. """
  392. def __init__(
  393. self,
  394. *,
  395. flags: list[str],
  396. action: type[_CallbackAction],
  397. arg_help: str,
  398. kwargs: dict[str, Any],
  399. hide_help: bool,
  400. section: str | None,
  401. metavar: str,
  402. ) -> None:
  403. super().__init__(
  404. flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
  405. )
  406. self.action = action
  407. """The action to perform with the argument."""
  408. self.kwargs = kwargs
  409. """Any additional arguments passed to the action."""
  410. self.metavar = metavar
  411. """The metavar of the argument.
  412. See:
  413. https://docs.python.org/3/library/argparse.html#metavar
  414. """