config_parser.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. from __future__ import annotations
  2. import argparse
  3. import configparser
  4. import glob as fileglob
  5. import os
  6. import re
  7. import sys
  8. from io import StringIO
  9. from mypy.errorcodes import error_codes
  10. if sys.version_info >= (3, 11):
  11. import tomllib
  12. else:
  13. import tomli as tomllib
  14. from typing import (
  15. Any,
  16. Callable,
  17. Dict,
  18. Iterable,
  19. List,
  20. Mapping,
  21. MutableMapping,
  22. Sequence,
  23. TextIO,
  24. Tuple,
  25. Union,
  26. )
  27. from typing_extensions import Final, TypeAlias as _TypeAlias
  28. from mypy import defaults
  29. from mypy.options import PER_MODULE_OPTIONS, Options
  30. _CONFIG_VALUE_TYPES: _TypeAlias = Union[
  31. str, bool, int, float, Dict[str, str], List[str], Tuple[int, int]
  32. ]
  33. _INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
  34. def parse_version(v: str | float) -> tuple[int, int]:
  35. m = re.match(r"\A(\d)\.(\d+)\Z", str(v))
  36. if not m:
  37. raise argparse.ArgumentTypeError(f"Invalid python version '{v}' (expected format: 'x.y')")
  38. major, minor = int(m.group(1)), int(m.group(2))
  39. if major == 2 and minor == 7:
  40. pass # Error raised elsewhere
  41. elif major == 3:
  42. if minor < defaults.PYTHON3_VERSION_MIN[1]:
  43. msg = "Python 3.{} is not supported (must be {}.{} or higher)".format(
  44. minor, *defaults.PYTHON3_VERSION_MIN
  45. )
  46. if isinstance(v, float):
  47. msg += ". You may need to put quotes around your Python version"
  48. raise argparse.ArgumentTypeError(msg)
  49. else:
  50. raise argparse.ArgumentTypeError(
  51. f"Python major version '{major}' out of range (must be 3)"
  52. )
  53. return major, minor
  54. def try_split(v: str | Sequence[str], split_regex: str = "[,]") -> list[str]:
  55. """Split and trim a str or list of str into a list of str"""
  56. if isinstance(v, str):
  57. return [p.strip() for p in re.split(split_regex, v)]
  58. return [p.strip() for p in v]
  59. def validate_codes(codes: list[str]) -> list[str]:
  60. invalid_codes = set(codes) - set(error_codes.keys())
  61. if invalid_codes:
  62. raise argparse.ArgumentTypeError(
  63. f"Invalid error code(s): {', '.join(sorted(invalid_codes))}"
  64. )
  65. return codes
  66. def expand_path(path: str) -> str:
  67. """Expand the user home directory and any environment variables contained within
  68. the provided path.
  69. """
  70. return os.path.expandvars(os.path.expanduser(path))
  71. def str_or_array_as_list(v: str | Sequence[str]) -> list[str]:
  72. if isinstance(v, str):
  73. return [v.strip()] if v.strip() else []
  74. return [p.strip() for p in v if p.strip()]
  75. def split_and_match_files_list(paths: Sequence[str]) -> list[str]:
  76. """Take a list of files/directories (with support for globbing through the glob library).
  77. Where a path/glob matches no file, we still include the raw path in the resulting list.
  78. Returns a list of file paths
  79. """
  80. expanded_paths = []
  81. for path in paths:
  82. path = expand_path(path.strip())
  83. globbed_files = fileglob.glob(path, recursive=True)
  84. if globbed_files:
  85. expanded_paths.extend(globbed_files)
  86. else:
  87. expanded_paths.append(path)
  88. return expanded_paths
  89. def split_and_match_files(paths: str) -> list[str]:
  90. """Take a string representing a list of files/directories (with support for globbing
  91. through the glob library).
  92. Where a path/glob matches no file, we still include the raw path in the resulting list.
  93. Returns a list of file paths
  94. """
  95. return split_and_match_files_list(paths.split(","))
  96. def check_follow_imports(choice: str) -> str:
  97. choices = ["normal", "silent", "skip", "error"]
  98. if choice not in choices:
  99. raise argparse.ArgumentTypeError(
  100. "invalid choice '{}' (choose from {})".format(
  101. choice, ", ".join(f"'{x}'" for x in choices)
  102. )
  103. )
  104. return choice
  105. def split_commas(value: str) -> list[str]:
  106. # Uses a bit smarter technique to allow last trailing comma
  107. # and to remove last `""` item from the split.
  108. items = value.split(",")
  109. if items and items[-1] == "":
  110. items.pop(-1)
  111. return items
  112. # For most options, the type of the default value set in options.py is
  113. # sufficient, and we don't have to do anything here. This table
  114. # exists to specify types for values initialized to None or container
  115. # types.
  116. ini_config_types: Final[dict[str, _INI_PARSER_CALLABLE]] = {
  117. "python_version": parse_version,
  118. "custom_typing_module": str,
  119. "custom_typeshed_dir": expand_path,
  120. "mypy_path": lambda s: [expand_path(p.strip()) for p in re.split("[,:]", s)],
  121. "files": split_and_match_files,
  122. "quickstart_file": expand_path,
  123. "junit_xml": expand_path,
  124. "follow_imports": check_follow_imports,
  125. "no_site_packages": bool,
  126. "plugins": lambda s: [p.strip() for p in split_commas(s)],
  127. "always_true": lambda s: [p.strip() for p in split_commas(s)],
  128. "always_false": lambda s: [p.strip() for p in split_commas(s)],
  129. "enable_incomplete_feature": lambda s: [p.strip() for p in split_commas(s)],
  130. "disable_error_code": lambda s: validate_codes([p.strip() for p in split_commas(s)]),
  131. "enable_error_code": lambda s: validate_codes([p.strip() for p in split_commas(s)]),
  132. "package_root": lambda s: [p.strip() for p in split_commas(s)],
  133. "cache_dir": expand_path,
  134. "python_executable": expand_path,
  135. "strict": bool,
  136. "exclude": lambda s: [s.strip()],
  137. "packages": try_split,
  138. "modules": try_split,
  139. }
  140. # Reuse the ini_config_types and overwrite the diff
  141. toml_config_types: Final[dict[str, _INI_PARSER_CALLABLE]] = ini_config_types.copy()
  142. toml_config_types.update(
  143. {
  144. "python_version": parse_version,
  145. "mypy_path": lambda s: [expand_path(p) for p in try_split(s, "[,:]")],
  146. "files": lambda s: split_and_match_files_list(try_split(s)),
  147. "follow_imports": lambda s: check_follow_imports(str(s)),
  148. "plugins": try_split,
  149. "always_true": try_split,
  150. "always_false": try_split,
  151. "enable_incomplete_feature": try_split,
  152. "disable_error_code": lambda s: validate_codes(try_split(s)),
  153. "enable_error_code": lambda s: validate_codes(try_split(s)),
  154. "package_root": try_split,
  155. "exclude": str_or_array_as_list,
  156. "packages": try_split,
  157. "modules": try_split,
  158. }
  159. )
  160. def parse_config_file(
  161. options: Options,
  162. set_strict_flags: Callable[[], None],
  163. filename: str | None,
  164. stdout: TextIO | None = None,
  165. stderr: TextIO | None = None,
  166. ) -> None:
  167. """Parse a config file into an Options object.
  168. Errors are written to stderr but are not fatal.
  169. If filename is None, fall back to default config files.
  170. """
  171. stdout = stdout or sys.stdout
  172. stderr = stderr or sys.stderr
  173. if filename is not None:
  174. config_files: tuple[str, ...] = (filename,)
  175. else:
  176. config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
  177. config_files = tuple(config_files_iter)
  178. config_parser = configparser.RawConfigParser()
  179. for config_file in config_files:
  180. if not os.path.exists(config_file):
  181. continue
  182. try:
  183. if is_toml(config_file):
  184. with open(config_file, "rb") as f:
  185. toml_data = tomllib.load(f)
  186. # Filter down to just mypy relevant toml keys
  187. toml_data = toml_data.get("tool", {})
  188. if "mypy" not in toml_data:
  189. continue
  190. toml_data = {"mypy": toml_data["mypy"]}
  191. parser: MutableMapping[str, Any] = destructure_overrides(toml_data)
  192. config_types = toml_config_types
  193. else:
  194. config_parser.read(config_file)
  195. parser = config_parser
  196. config_types = ini_config_types
  197. except (tomllib.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
  198. print(f"{config_file}: {err}", file=stderr)
  199. else:
  200. if config_file in defaults.SHARED_CONFIG_FILES and "mypy" not in parser:
  201. continue
  202. file_read = config_file
  203. options.config_file = file_read
  204. break
  205. else:
  206. return
  207. os.environ["MYPY_CONFIG_FILE_DIR"] = os.path.dirname(os.path.abspath(config_file))
  208. if "mypy" not in parser:
  209. if filename or file_read not in defaults.SHARED_CONFIG_FILES:
  210. print(f"{file_read}: No [mypy] section in config file", file=stderr)
  211. else:
  212. section = parser["mypy"]
  213. prefix = f"{file_read}: [mypy]: "
  214. updates, report_dirs = parse_section(
  215. prefix, options, set_strict_flags, section, config_types, stderr
  216. )
  217. for k, v in updates.items():
  218. setattr(options, k, v)
  219. options.report_dirs.update(report_dirs)
  220. for name, section in parser.items():
  221. if name.startswith("mypy-"):
  222. prefix = get_prefix(file_read, name)
  223. updates, report_dirs = parse_section(
  224. prefix, options, set_strict_flags, section, config_types, stderr
  225. )
  226. if report_dirs:
  227. print(
  228. "%sPer-module sections should not specify reports (%s)"
  229. % (prefix, ", ".join(s + "_report" for s in sorted(report_dirs))),
  230. file=stderr,
  231. )
  232. if set(updates) - PER_MODULE_OPTIONS:
  233. print(
  234. "%sPer-module sections should only specify per-module flags (%s)"
  235. % (prefix, ", ".join(sorted(set(updates) - PER_MODULE_OPTIONS))),
  236. file=stderr,
  237. )
  238. updates = {k: v for k, v in updates.items() if k in PER_MODULE_OPTIONS}
  239. globs = name[5:]
  240. for glob in globs.split(","):
  241. # For backwards compatibility, replace (back)slashes with dots.
  242. glob = glob.replace(os.sep, ".")
  243. if os.altsep:
  244. glob = glob.replace(os.altsep, ".")
  245. if any(c in glob for c in "?[]!") or any(
  246. "*" in x and x != "*" for x in glob.split(".")
  247. ):
  248. print(
  249. "%sPatterns must be fully-qualified module names, optionally "
  250. "with '*' in some components (e.g spam.*.eggs.*)" % prefix,
  251. file=stderr,
  252. )
  253. else:
  254. options.per_module_options[glob] = updates
  255. def get_prefix(file_read: str, name: str) -> str:
  256. if is_toml(file_read):
  257. module_name_str = 'module = "%s"' % "-".join(name.split("-")[1:])
  258. else:
  259. module_name_str = name
  260. return f"{file_read}: [{module_name_str}]: "
  261. def is_toml(filename: str) -> bool:
  262. return filename.lower().endswith(".toml")
  263. def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
  264. """Take the new [[tool.mypy.overrides]] section array in the pyproject.toml file,
  265. and convert it back to a flatter structure that the existing config_parser can handle.
  266. E.g. the following pyproject.toml file:
  267. [[tool.mypy.overrides]]
  268. module = [
  269. "a.b",
  270. "b.*"
  271. ]
  272. disallow_untyped_defs = true
  273. [[tool.mypy.overrides]]
  274. module = 'c'
  275. disallow_untyped_defs = false
  276. Would map to the following config dict that it would have gotten from parsing an equivalent
  277. ini file:
  278. {
  279. "mypy-a.b": {
  280. disallow_untyped_defs = true,
  281. },
  282. "mypy-b.*": {
  283. disallow_untyped_defs = true,
  284. },
  285. "mypy-c": {
  286. disallow_untyped_defs: false,
  287. },
  288. }
  289. """
  290. if "overrides" not in toml_data["mypy"]:
  291. return toml_data
  292. if not isinstance(toml_data["mypy"]["overrides"], list):
  293. raise ConfigTOMLValueError(
  294. "tool.mypy.overrides sections must be an array. Please make "
  295. "sure you are using double brackets like so: [[tool.mypy.overrides]]"
  296. )
  297. result = toml_data.copy()
  298. for override in result["mypy"]["overrides"]:
  299. if "module" not in override:
  300. raise ConfigTOMLValueError(
  301. "toml config file contains a [[tool.mypy.overrides]] "
  302. "section, but no module to override was specified."
  303. )
  304. if isinstance(override["module"], str):
  305. modules = [override["module"]]
  306. elif isinstance(override["module"], list):
  307. modules = override["module"]
  308. else:
  309. raise ConfigTOMLValueError(
  310. "toml config file contains a [[tool.mypy.overrides]] "
  311. "section with a module value that is not a string or a list of "
  312. "strings"
  313. )
  314. for module in modules:
  315. module_overrides = override.copy()
  316. del module_overrides["module"]
  317. old_config_name = f"mypy-{module}"
  318. if old_config_name not in result:
  319. result[old_config_name] = module_overrides
  320. else:
  321. for new_key, new_value in module_overrides.items():
  322. if (
  323. new_key in result[old_config_name]
  324. and result[old_config_name][new_key] != new_value
  325. ):
  326. raise ConfigTOMLValueError(
  327. "toml config file contains "
  328. "[[tool.mypy.overrides]] sections with conflicting "
  329. "values. Module '%s' has two different values for '%s'"
  330. % (module, new_key)
  331. )
  332. result[old_config_name][new_key] = new_value
  333. del result["mypy"]["overrides"]
  334. return result
  335. def parse_section(
  336. prefix: str,
  337. template: Options,
  338. set_strict_flags: Callable[[], None],
  339. section: Mapping[str, Any],
  340. config_types: dict[str, Any],
  341. stderr: TextIO = sys.stderr,
  342. ) -> tuple[dict[str, object], dict[str, str]]:
  343. """Parse one section of a config file.
  344. Returns a dict of option values encountered, and a dict of report directories.
  345. """
  346. results: dict[str, object] = {}
  347. report_dirs: dict[str, str] = {}
  348. for key in section:
  349. invert = False
  350. options_key = key
  351. if key in config_types:
  352. ct = config_types[key]
  353. else:
  354. dv = None
  355. # We have to keep new_semantic_analyzer in Options
  356. # for plugin compatibility but it is not a valid option anymore.
  357. assert hasattr(template, "new_semantic_analyzer")
  358. if key != "new_semantic_analyzer":
  359. dv = getattr(template, key, None)
  360. if dv is None:
  361. if key.endswith("_report"):
  362. report_type = key[:-7].replace("_", "-")
  363. if report_type in defaults.REPORTER_NAMES:
  364. report_dirs[report_type] = str(section[key])
  365. else:
  366. print(f"{prefix}Unrecognized report type: {key}", file=stderr)
  367. continue
  368. if key.startswith("x_"):
  369. pass # Don't complain about `x_blah` flags
  370. elif key.startswith("no_") and hasattr(template, key[3:]):
  371. options_key = key[3:]
  372. invert = True
  373. elif key.startswith("allow") and hasattr(template, "dis" + key):
  374. options_key = "dis" + key
  375. invert = True
  376. elif key.startswith("disallow") and hasattr(template, key[3:]):
  377. options_key = key[3:]
  378. invert = True
  379. elif key.startswith("show_") and hasattr(template, "hide_" + key[5:]):
  380. options_key = "hide_" + key[5:]
  381. invert = True
  382. elif key == "strict":
  383. pass # Special handling below
  384. else:
  385. print(f"{prefix}Unrecognized option: {key} = {section[key]}", file=stderr)
  386. if invert:
  387. dv = getattr(template, options_key, None)
  388. else:
  389. continue
  390. ct = type(dv)
  391. v: Any = None
  392. try:
  393. if ct is bool:
  394. if isinstance(section, dict):
  395. v = convert_to_boolean(section.get(key))
  396. else:
  397. v = section.getboolean(key) # type: ignore[attr-defined] # Until better stub
  398. if invert:
  399. v = not v
  400. elif callable(ct):
  401. if invert:
  402. print(f"{prefix}Can not invert non-boolean key {options_key}", file=stderr)
  403. continue
  404. try:
  405. v = ct(section.get(key))
  406. except argparse.ArgumentTypeError as err:
  407. print(f"{prefix}{key}: {err}", file=stderr)
  408. continue
  409. else:
  410. print(f"{prefix}Don't know what type {key} should have", file=stderr)
  411. continue
  412. except ValueError as err:
  413. print(f"{prefix}{key}: {err}", file=stderr)
  414. continue
  415. if key == "strict":
  416. if v:
  417. set_strict_flags()
  418. continue
  419. results[options_key] = v
  420. # These two flags act as per-module overrides, so store the empty defaults.
  421. if "disable_error_code" not in results:
  422. results["disable_error_code"] = []
  423. if "enable_error_code" not in results:
  424. results["enable_error_code"] = []
  425. return results, report_dirs
  426. def convert_to_boolean(value: Any | None) -> bool:
  427. """Return a boolean value translating from other types if necessary."""
  428. if isinstance(value, bool):
  429. return value
  430. if not isinstance(value, str):
  431. value = str(value)
  432. if value.lower() not in configparser.RawConfigParser.BOOLEAN_STATES:
  433. raise ValueError(f"Not a boolean: {value}")
  434. return configparser.RawConfigParser.BOOLEAN_STATES[value.lower()]
  435. def split_directive(s: str) -> tuple[list[str], list[str]]:
  436. """Split s on commas, except during quoted sections.
  437. Returns the parts and a list of error messages."""
  438. parts = []
  439. cur: list[str] = []
  440. errors = []
  441. i = 0
  442. while i < len(s):
  443. if s[i] == ",":
  444. parts.append("".join(cur).strip())
  445. cur = []
  446. elif s[i] == '"':
  447. i += 1
  448. while i < len(s) and s[i] != '"':
  449. cur.append(s[i])
  450. i += 1
  451. if i == len(s):
  452. errors.append("Unterminated quote in configuration comment")
  453. cur.clear()
  454. else:
  455. cur.append(s[i])
  456. i += 1
  457. if cur:
  458. parts.append("".join(cur).strip())
  459. return parts, errors
  460. def mypy_comments_to_config_map(line: str, template: Options) -> tuple[dict[str, str], list[str]]:
  461. """Rewrite the mypy comment syntax into ini file syntax."""
  462. options = {}
  463. entries, errors = split_directive(line)
  464. for entry in entries:
  465. if "=" not in entry:
  466. name = entry
  467. value = None
  468. else:
  469. name, value = (x.strip() for x in entry.split("=", 1))
  470. name = name.replace("-", "_")
  471. if value is None:
  472. value = "True"
  473. options[name] = value
  474. return options, errors
  475. def parse_mypy_comments(
  476. args: list[tuple[int, str]], template: Options
  477. ) -> tuple[dict[str, object], list[tuple[int, str]]]:
  478. """Parse a collection of inline mypy: configuration comments.
  479. Returns a dictionary of options to be applied and a list of error messages
  480. generated.
  481. """
  482. errors: list[tuple[int, str]] = []
  483. sections = {}
  484. for lineno, line in args:
  485. # In order to easily match the behavior for bools, we abuse configparser.
  486. # Oddly, the only way to get the SectionProxy object with the getboolean
  487. # method is to create a config parser.
  488. parser = configparser.RawConfigParser()
  489. options, parse_errors = mypy_comments_to_config_map(line, template)
  490. parser["dummy"] = options
  491. errors.extend((lineno, x) for x in parse_errors)
  492. stderr = StringIO()
  493. strict_found = False
  494. def set_strict_flags() -> None:
  495. nonlocal strict_found
  496. strict_found = True
  497. new_sections, reports = parse_section(
  498. "", template, set_strict_flags, parser["dummy"], ini_config_types, stderr=stderr
  499. )
  500. errors.extend((lineno, x) for x in stderr.getvalue().strip().split("\n") if x)
  501. if reports:
  502. errors.append((lineno, "Reports not supported in inline configuration"))
  503. if strict_found:
  504. errors.append(
  505. (
  506. lineno,
  507. 'Setting "strict" not supported in inline configuration: specify it in '
  508. "a configuration file instead, or set individual inline flags "
  509. '(see "mypy -h" for the list of flags enabled in strict mode)',
  510. )
  511. )
  512. sections.update(new_sections)
  513. return sections, errors
  514. def get_config_module_names(filename: str | None, modules: list[str]) -> str:
  515. if not filename or not modules:
  516. return ""
  517. if not is_toml(filename):
  518. return ", ".join(f"[mypy-{module}]" for module in modules)
  519. return "module = ['%s']" % ("', '".join(sorted(modules)))
  520. class ConfigTOMLValueError(ValueError):
  521. pass