Parse a config file into an Options object. Errors are written to stderr but are not fatal. If filename is None, fall back to default config files.
(
options: Options,
set_strict_flags: Callable[[], None],
filename: str | None,
stdout: TextIO | None = None,
stderr: TextIO | None = None,
)
| 300 | |
| 301 | |
| 302 | def parse_config_file( |
| 303 | options: Options, |
| 304 | set_strict_flags: Callable[[], None], |
| 305 | filename: str | None, |
| 306 | stdout: TextIO | None = None, |
| 307 | stderr: TextIO | None = None, |
| 308 | ) -> None: |
| 309 | """Parse a config file into an Options object. |
| 310 | |
| 311 | Errors are written to stderr but are not fatal. |
| 312 | |
| 313 | If filename is None, fall back to default config files. |
| 314 | """ |
| 315 | stdout = stdout or sys.stdout |
| 316 | stderr = stderr or sys.stderr |
| 317 | |
| 318 | ret = ( |
| 319 | _parse_individual_file(filename, stderr) |
| 320 | if filename is not None |
| 321 | else _find_config_file(stderr) |
| 322 | ) |
| 323 | if ret is None: |
| 324 | return |
| 325 | parser, config_types, file_read = ret |
| 326 | |
| 327 | options.config_file = file_read |
| 328 | os.environ["MYPY_CONFIG_FILE_DIR"] = os.path.dirname(os.path.abspath(file_read)) |
| 329 | |
| 330 | if "mypy" not in parser: |
| 331 | if filename or os.path.basename(file_read) not in defaults.SHARED_CONFIG_NAMES: |
| 332 | print(f"{file_read}: No [mypy] section in config file", file=stderr) |
| 333 | else: |
| 334 | section = parser["mypy"] |
| 335 | prefix = f"{file_read}: [mypy]: " |
| 336 | updates, report_dirs = parse_section( |
| 337 | prefix, options, set_strict_flags, section, config_types, stderr |
| 338 | ) |
| 339 | for k, v in updates.items(): |
| 340 | setattr(options, k, v) |
| 341 | options.report_dirs.update(report_dirs) |
| 342 | |
| 343 | for name, section in parser.items(): |
| 344 | if name.startswith("mypy-"): |
| 345 | prefix = get_prefix(file_read, name) |
| 346 | updates, report_dirs = parse_section( |
| 347 | prefix, options, set_strict_flags, section, config_types, stderr |
| 348 | ) |
| 349 | if report_dirs: |
| 350 | print( |
| 351 | prefix, |
| 352 | "Per-module sections should not specify reports ({})".format( |
| 353 | ", ".join(s + "_report" for s in sorted(report_dirs)) |
| 354 | ), |
| 355 | file=stderr, |
| 356 | ) |
| 357 | if set(updates) - PER_MODULE_OPTIONS: |
| 358 | print( |
| 359 | prefix, |
searching dependent graphs…