Parse command line arguments. If a FileSystemCache is passed in, and package_root options are given, call fscache.set_package_root() to set the cache's package root. Returns a tuple of: a list of source files, an Options collected from flags.
(
args: list[str],
stdout: TextIO | None = None,
stderr: TextIO | None = None,
require_targets: bool = True,
server_options: bool = False,
fscache: FileSystemCache | None = None,
program: str = "mypy",
header: str = HEADER,
mypyc: bool = False,
)
| 1406 | |
| 1407 | |
| 1408 | def process_options( |
| 1409 | args: list[str], |
| 1410 | stdout: TextIO | None = None, |
| 1411 | stderr: TextIO | None = None, |
| 1412 | require_targets: bool = True, |
| 1413 | server_options: bool = False, |
| 1414 | fscache: FileSystemCache | None = None, |
| 1415 | program: str = "mypy", |
| 1416 | header: str = HEADER, |
| 1417 | mypyc: bool = False, |
| 1418 | ) -> tuple[list[BuildSource], Options]: |
| 1419 | """Parse command line arguments. |
| 1420 | |
| 1421 | If a FileSystemCache is passed in, and package_root options are given, |
| 1422 | call fscache.set_package_root() to set the cache's package root. |
| 1423 | |
| 1424 | Returns a tuple of: a list of source files, an Options collected from flags. |
| 1425 | """ |
| 1426 | stdout = stdout if stdout is not None else sys.stdout |
| 1427 | stderr = stderr if stderr is not None else sys.stderr |
| 1428 | |
| 1429 | parser, _, strict_flag_assignments = define_options( |
| 1430 | program, header, stdout, stderr, server_options |
| 1431 | ) |
| 1432 | |
| 1433 | # Parse arguments once into a dummy namespace so we can get the |
| 1434 | # filename for the config file and know if the user requested all strict options. |
| 1435 | dummy = argparse.Namespace() |
| 1436 | parser.parse_args(args, dummy) |
| 1437 | config_file = dummy.config_file |
| 1438 | # Don't explicitly test if "config_file is not None" for this check. |
| 1439 | # This lets `--config-file=` (an empty string) be used to disable all config files. |
| 1440 | if config_file and not os.path.exists(config_file): |
| 1441 | parser.error(f"Cannot find config file '{config_file}'") |
| 1442 | |
| 1443 | options = Options() |
| 1444 | strict_option_set = False |
| 1445 | if mypyc: |
| 1446 | # Mypyc has strict_bytes enabled by default |
| 1447 | options.strict_bytes = True |
| 1448 | |
| 1449 | def set_strict_flags() -> None: |
| 1450 | nonlocal strict_option_set |
| 1451 | strict_option_set = True |
| 1452 | for dest, value in strict_flag_assignments: |
| 1453 | setattr(options, dest, value) |
| 1454 | |
| 1455 | # Parse config file first, so command line can override. |
| 1456 | parse_config_file(options, set_strict_flags, config_file, stdout, stderr) |
| 1457 | |
| 1458 | # Set strict flags before parsing (if strict mode enabled), so other command |
| 1459 | # line options can override. |
| 1460 | if getattr(dummy, "special-opts:strict"): |
| 1461 | set_strict_flags() |
| 1462 | |
| 1463 | # Override cache_dir if provided in the environment |
| 1464 | environ_cache_dir = os.getenv("MYPY_CACHE_DIR", "") |
| 1465 | if environ_cache_dir.strip(): |
searching dependent graphs…