base_options.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. """Functions that creates the basic options for the Run and PyLinter classes."""
  5. from __future__ import annotations
  6. import re
  7. import sys
  8. from typing import TYPE_CHECKING
  9. from pylint import constants, interfaces
  10. from pylint.config.callback_actions import (
  11. _DisableAction,
  12. _DoNothingAction,
  13. _EnableAction,
  14. _ErrorsOnlyModeAction,
  15. _FullDocumentationAction,
  16. _GenerateConfigFileAction,
  17. _GenerateRCFileAction,
  18. _ListCheckGroupsAction,
  19. _ListConfidenceLevelsAction,
  20. _ListExtensionsAction,
  21. _ListMessagesAction,
  22. _ListMessagesEnabledAction,
  23. _LongHelpAction,
  24. _MessageHelpAction,
  25. _OutputFormatAction,
  26. )
  27. from pylint.typing import Options
  28. if TYPE_CHECKING:
  29. from pylint.lint import PyLinter, Run
  30. def _make_linter_options(linter: PyLinter) -> Options:
  31. """Return the options used in a PyLinter class."""
  32. return (
  33. (
  34. "ignore",
  35. {
  36. "type": "csv",
  37. "metavar": "<file>[,<file>...]",
  38. "dest": "black_list",
  39. "kwargs": {"old_names": ["black_list"]},
  40. "default": constants.DEFAULT_IGNORE_LIST,
  41. "help": "Files or directories to be skipped. "
  42. "They should be base names, not paths.",
  43. },
  44. ),
  45. (
  46. "ignore-patterns",
  47. {
  48. "type": "regexp_csv",
  49. "metavar": "<pattern>[,<pattern>...]",
  50. "dest": "black_list_re",
  51. "default": (re.compile(r"^\.#"),),
  52. "help": "Files or directories matching the regular expression patterns are"
  53. " skipped. The regex matches against base names, not paths. The default value "
  54. "ignores Emacs file locks",
  55. },
  56. ),
  57. (
  58. "ignore-paths",
  59. {
  60. "type": "regexp_paths_csv",
  61. "metavar": "<pattern>[,<pattern>...]",
  62. "default": [],
  63. "help": "Add files or directories matching the regular expressions patterns to the "
  64. "ignore-list. The regex matches against paths and can be in "
  65. "Posix or Windows format. Because '\\\\' represents the directory delimiter "
  66. "on Windows systems, it can't be used as an escape character.",
  67. },
  68. ),
  69. (
  70. "persistent",
  71. {
  72. "default": True,
  73. "type": "yn",
  74. "metavar": "<y or n>",
  75. "help": "Pickle collected data for later comparisons.",
  76. },
  77. ),
  78. (
  79. "load-plugins",
  80. {
  81. "type": "csv",
  82. "metavar": "<modules>",
  83. "default": (),
  84. "help": "List of plugins (as comma separated values of "
  85. "python module names) to load, usually to register "
  86. "additional checkers.",
  87. },
  88. ),
  89. (
  90. "output-format",
  91. {
  92. "default": "text",
  93. "action": _OutputFormatAction,
  94. "callback": lambda x: x,
  95. "metavar": "<format>",
  96. "short": "f",
  97. "group": "Reports",
  98. "help": "Set the output format. Available formats are text,"
  99. " parseable, colorized, json and msvs (visual studio)."
  100. " You can also give a reporter class, e.g. mypackage.mymodule."
  101. "MyReporterClass.",
  102. "kwargs": {"linter": linter},
  103. },
  104. ),
  105. (
  106. "reports",
  107. {
  108. "default": False,
  109. "type": "yn",
  110. "metavar": "<y or n>",
  111. "short": "r",
  112. "group": "Reports",
  113. "help": "Tells whether to display a full report or only the "
  114. "messages.",
  115. },
  116. ),
  117. (
  118. "evaluation",
  119. {
  120. "type": "string",
  121. "metavar": "<python_expression>",
  122. "group": "Reports",
  123. "default": "max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + "
  124. "convention) / statement) * 10))",
  125. "help": "Python expression which should return a score less "
  126. "than or equal to 10. You have access to the variables 'fatal', "
  127. "'error', 'warning', 'refactor', 'convention', and 'info' which "
  128. "contain the number of messages in each category, as well as "
  129. "'statement' which is the total number of statements "
  130. "analyzed. This score is used by the global "
  131. "evaluation report (RP0004).",
  132. },
  133. ),
  134. (
  135. "score",
  136. {
  137. "default": True,
  138. "type": "yn",
  139. "metavar": "<y or n>",
  140. "short": "s",
  141. "group": "Reports",
  142. "help": "Activate the evaluation score.",
  143. },
  144. ),
  145. (
  146. "fail-under",
  147. {
  148. "default": 10,
  149. "type": "float",
  150. "metavar": "<score>",
  151. "help": "Specify a score threshold under which the program will exit with error.",
  152. },
  153. ),
  154. (
  155. "fail-on",
  156. {
  157. "default": "",
  158. "type": "csv",
  159. "metavar": "<msg ids>",
  160. "help": "Return non-zero exit code if any of these messages/categories are detected,"
  161. " even if score is above --fail-under value. Syntax same as enable."
  162. " Messages specified are enabled, while categories only check already-enabled messages.",
  163. },
  164. ),
  165. (
  166. "confidence",
  167. {
  168. "type": "confidence",
  169. "metavar": "<levels>",
  170. "default": interfaces.CONFIDENCE_LEVEL_NAMES,
  171. "group": "Messages control",
  172. "help": "Only show warnings with the listed confidence levels."
  173. f" Leave empty to show all. Valid levels: {', '.join(interfaces.CONFIDENCE_LEVEL_NAMES)}.",
  174. },
  175. ),
  176. (
  177. "enable",
  178. {
  179. "action": _EnableAction,
  180. "callback": lambda x1, x2, x3, x4: x1,
  181. "default": (),
  182. "metavar": "<msg ids>",
  183. "short": "e",
  184. "group": "Messages control",
  185. "help": "Enable the message, report, category or checker with the "
  186. "given id(s). You can either give multiple identifier "
  187. "separated by comma (,) or put this option multiple time "
  188. "(only on the command line, not in the configuration file "
  189. "where it should appear only once). "
  190. 'See also the "--disable" option for examples.',
  191. "kwargs": {"linter": linter},
  192. },
  193. ),
  194. (
  195. "disable",
  196. {
  197. "action": _DisableAction,
  198. "callback": lambda x1, x2, x3, x4: x1,
  199. "metavar": "<msg ids>",
  200. "default": (),
  201. "short": "d",
  202. "group": "Messages control",
  203. "help": "Disable the message, report, category or checker "
  204. "with the given id(s). You can either give multiple identifiers "
  205. "separated by comma (,) or put this option multiple times "
  206. "(only on the command line, not in the configuration file "
  207. "where it should appear only once). "
  208. 'You can also use "--disable=all" to disable everything first '
  209. "and then re-enable specific checks. For example, if you want "
  210. "to run only the similarities checker, you can use "
  211. '"--disable=all --enable=similarities". '
  212. "If you want to run only the classes checker, but have no "
  213. "Warning level messages displayed, use "
  214. '"--disable=all --enable=classes --disable=W".',
  215. "kwargs": {"linter": linter},
  216. },
  217. ),
  218. (
  219. "msg-template",
  220. {
  221. "type": "string",
  222. "default": "",
  223. "metavar": "<template>",
  224. "group": "Reports",
  225. "help": (
  226. "Template used to display messages. "
  227. "This is a python new-style format string "
  228. "used to format the message information. "
  229. "See doc for all details."
  230. ),
  231. },
  232. ),
  233. (
  234. "jobs",
  235. {
  236. "type": "int",
  237. "metavar": "<n-processes>",
  238. "short": "j",
  239. "default": 1,
  240. "help": "Use multiple processes to speed up Pylint. Specifying 0 will "
  241. "auto-detect the number of processors available to use, and will cap "
  242. "the count on Windows to avoid hangs.",
  243. },
  244. ),
  245. (
  246. "unsafe-load-any-extension",
  247. {
  248. "type": "yn",
  249. "metavar": "<y or n>",
  250. "default": False,
  251. "hide": True,
  252. "help": (
  253. "Allow loading of arbitrary C extensions. Extensions"
  254. " are imported into the active Python interpreter and"
  255. " may run arbitrary code."
  256. ),
  257. },
  258. ),
  259. (
  260. "limit-inference-results",
  261. {
  262. "type": "int",
  263. "metavar": "<number-of-results>",
  264. "default": 100,
  265. "help": (
  266. "Control the amount of potential inferred values when inferring "
  267. "a single object. This can help the performance when dealing with "
  268. "large functions or complex, nested conditions."
  269. ),
  270. },
  271. ),
  272. (
  273. "extension-pkg-allow-list",
  274. {
  275. "type": "csv",
  276. "metavar": "<pkg[,pkg]>",
  277. "default": [],
  278. "help": (
  279. "A comma-separated list of package or module names"
  280. " from where C extensions may be loaded. Extensions are"
  281. " loading into the active Python interpreter and may run"
  282. " arbitrary code."
  283. ),
  284. },
  285. ),
  286. (
  287. "extension-pkg-whitelist",
  288. {
  289. "type": "csv",
  290. "metavar": "<pkg[,pkg]>",
  291. "default": [],
  292. "help": (
  293. "A comma-separated list of package or module names"
  294. " from where C extensions may be loaded. Extensions are"
  295. " loading into the active Python interpreter and may run"
  296. " arbitrary code. (This is an alternative name to"
  297. " extension-pkg-allow-list for backward compatibility.)"
  298. ),
  299. },
  300. ),
  301. (
  302. "suggestion-mode",
  303. {
  304. "type": "yn",
  305. "metavar": "<y or n>",
  306. "default": True,
  307. "help": (
  308. "When enabled, pylint would attempt to guess common "
  309. "misconfiguration and emit user-friendly hints instead "
  310. "of false-positive error messages."
  311. ),
  312. },
  313. ),
  314. (
  315. "exit-zero",
  316. {
  317. "action": "store_true",
  318. "default": False,
  319. "metavar": "<flag>",
  320. "help": (
  321. "Always return a 0 (non-error) status code, even if "
  322. "lint errors are found. This is primarily useful in "
  323. "continuous integration scripts."
  324. ),
  325. },
  326. ),
  327. (
  328. "from-stdin",
  329. {
  330. "action": "store_true",
  331. "default": False,
  332. "metavar": "<flag>",
  333. "help": (
  334. "Interpret the stdin as a python script, whose filename "
  335. "needs to be passed as the module_or_package argument."
  336. ),
  337. },
  338. ),
  339. (
  340. "source-roots",
  341. {
  342. "type": "glob_paths_csv",
  343. "metavar": "<path>[,<path>...]",
  344. "default": (),
  345. "help": "Add paths to the list of the source roots. Supports globbing patterns. "
  346. "The source root is an absolute path or a path relative to the current working "
  347. "directory used to determine a package namespace for modules located under the "
  348. "source root.",
  349. },
  350. ),
  351. (
  352. "recursive",
  353. {
  354. "type": "yn",
  355. "metavar": "<yn>",
  356. "default": False,
  357. "help": "Discover python modules and packages in the file system subtree.",
  358. },
  359. ),
  360. (
  361. "py-version",
  362. {
  363. "default": sys.version_info[:2],
  364. "type": "py_version",
  365. "metavar": "<py_version>",
  366. "help": (
  367. "Minimum Python version to use for version dependent checks. "
  368. "Will default to the version used to run pylint."
  369. ),
  370. },
  371. ),
  372. (
  373. "ignored-modules",
  374. {
  375. "default": (),
  376. "type": "csv",
  377. "metavar": "<module names>",
  378. "help": "List of module names for which member attributes "
  379. "should not be checked (useful for modules/projects "
  380. "where namespaces are manipulated during runtime and "
  381. "thus existing member attributes cannot be "
  382. "deduced by static analysis). It supports qualified "
  383. "module names, as well as Unix pattern matching.",
  384. },
  385. ),
  386. (
  387. "analyse-fallback-blocks",
  388. {
  389. "default": False,
  390. "type": "yn",
  391. "metavar": "<y or n>",
  392. "help": "Analyse import fallback blocks. This can be used to "
  393. "support both Python 2 and 3 compatible code, which "
  394. "means that the block might have code that exists "
  395. "only in one or another interpreter, leading to false "
  396. "positives when analysed.",
  397. },
  398. ),
  399. (
  400. "clear-cache-post-run",
  401. {
  402. "default": False,
  403. "type": "yn",
  404. "metavar": "<y or n>",
  405. "help": "Clear in-memory caches upon conclusion of linting. "
  406. "Useful if running pylint in a server-like mode.",
  407. },
  408. ),
  409. )
  410. def _make_run_options(self: Run) -> Options:
  411. """Return the options used in a Run class."""
  412. return (
  413. (
  414. "rcfile",
  415. {
  416. "action": _DoNothingAction,
  417. "kwargs": {},
  418. "group": "Commands",
  419. "help": "Specify a configuration file to load.",
  420. "hide_from_config_file": True,
  421. },
  422. ),
  423. (
  424. "output",
  425. {
  426. "action": _DoNothingAction,
  427. "kwargs": {},
  428. "group": "Commands",
  429. "help": "Specify an output file.",
  430. "hide_from_config_file": True,
  431. },
  432. ),
  433. (
  434. "init-hook",
  435. {
  436. "action": _DoNothingAction,
  437. "kwargs": {},
  438. "help": "Python code to execute, usually for sys.path "
  439. "manipulation such as pygtk.require().",
  440. },
  441. ),
  442. (
  443. "help-msg",
  444. {
  445. "action": _MessageHelpAction,
  446. "kwargs": {"Run": self},
  447. "group": "Commands",
  448. "help": "Display a help message for the given message id and "
  449. "exit. The value may be a comma separated list of message ids.",
  450. "hide_from_config_file": True,
  451. },
  452. ),
  453. (
  454. "list-msgs",
  455. {
  456. "action": _ListMessagesAction,
  457. "kwargs": {"Run": self},
  458. "group": "Commands",
  459. "help": "Display a list of all pylint's messages divided by whether "
  460. "they are emittable with the given interpreter.",
  461. "hide_from_config_file": True,
  462. },
  463. ),
  464. (
  465. "list-msgs-enabled",
  466. {
  467. "action": _ListMessagesEnabledAction,
  468. "kwargs": {"Run": self},
  469. "group": "Commands",
  470. "help": "Display a list of what messages are enabled, "
  471. "disabled and non-emittable with the given configuration.",
  472. "hide_from_config_file": True,
  473. },
  474. ),
  475. (
  476. "list-groups",
  477. {
  478. "action": _ListCheckGroupsAction,
  479. "kwargs": {"Run": self},
  480. "group": "Commands",
  481. "help": "List pylint's message groups.",
  482. "hide_from_config_file": True,
  483. },
  484. ),
  485. (
  486. "list-conf-levels",
  487. {
  488. "action": _ListConfidenceLevelsAction,
  489. "kwargs": {"Run": self},
  490. "group": "Commands",
  491. "help": "Generate pylint's confidence levels.",
  492. "hide_from_config_file": True,
  493. },
  494. ),
  495. (
  496. "list-extensions",
  497. {
  498. "action": _ListExtensionsAction,
  499. "kwargs": {"Run": self},
  500. "group": "Commands",
  501. "help": "List available extensions.",
  502. "hide_from_config_file": True,
  503. },
  504. ),
  505. (
  506. "full-documentation",
  507. {
  508. "action": _FullDocumentationAction,
  509. "kwargs": {"Run": self},
  510. "group": "Commands",
  511. "help": "Generate pylint's full documentation.",
  512. "hide_from_config_file": True,
  513. },
  514. ),
  515. (
  516. "generate-rcfile",
  517. {
  518. "action": _GenerateRCFileAction,
  519. "kwargs": {"Run": self},
  520. "group": "Commands",
  521. "help": "Generate a sample configuration file according to "
  522. "the current configuration. You can put other options "
  523. "before this one to get them in the generated "
  524. "configuration.",
  525. "hide_from_config_file": True,
  526. },
  527. ),
  528. (
  529. "generate-toml-config",
  530. {
  531. "action": _GenerateConfigFileAction,
  532. "kwargs": {"Run": self},
  533. "group": "Commands",
  534. "help": "Generate a sample configuration file according to "
  535. "the current configuration. You can put other options "
  536. "before this one to get them in the generated "
  537. "configuration. The config is in the .toml format.",
  538. "hide_from_config_file": True,
  539. },
  540. ),
  541. (
  542. "errors-only",
  543. {
  544. "action": _ErrorsOnlyModeAction,
  545. "kwargs": {"Run": self},
  546. "short": "E",
  547. "help": "In error mode, messages with a category besides "
  548. "ERROR or FATAL are suppressed, and no reports are done by default. "
  549. "Error mode is compatible with disabling specific errors. ",
  550. "hide_from_config_file": True,
  551. },
  552. ),
  553. (
  554. "verbose",
  555. {
  556. "action": _DoNothingAction,
  557. "kwargs": {},
  558. "short": "v",
  559. "help": "In verbose mode, extra non-checker-related info "
  560. "will be displayed.",
  561. "hide_from_config_file": True,
  562. "metavar": "",
  563. },
  564. ),
  565. (
  566. "enable-all-extensions",
  567. {
  568. "action": _DoNothingAction,
  569. "kwargs": {},
  570. "help": "Load and enable all available extensions. "
  571. "Use --list-extensions to see a list all available extensions.",
  572. "hide_from_config_file": True,
  573. "metavar": "",
  574. },
  575. ),
  576. (
  577. "long-help",
  578. {
  579. "action": _LongHelpAction,
  580. "kwargs": {"Run": self},
  581. "help": "Show more verbose help.",
  582. "group": "Commands",
  583. "hide_from_config_file": True,
  584. },
  585. ),
  586. )