argument.py 15 KB

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