Parse a collection of inline mypy: configuration comments. Returns a dictionary of options to be applied and a list of error messages generated.
(
args: list[tuple[int, str]], template: Options
)
| 654 | |
| 655 | |
| 656 | def parse_mypy_comments( |
| 657 | args: list[tuple[int, str]], template: Options |
| 658 | ) -> tuple[dict[str, object], list[tuple[int, str]]]: |
| 659 | """Parse a collection of inline mypy: configuration comments. |
| 660 | |
| 661 | Returns a dictionary of options to be applied and a list of error messages |
| 662 | generated. |
| 663 | """ |
| 664 | errors: list[tuple[int, str]] = [] |
| 665 | sections: dict[str, object] = {"enable_error_code": [], "disable_error_code": []} |
| 666 | |
| 667 | for lineno, line in args: |
| 668 | # In order to easily match the behavior for bools, we abuse configparser. |
| 669 | # Oddly, the only way to get the SectionProxy object with the getboolean |
| 670 | # method is to create a config parser. |
| 671 | parser = configparser.RawConfigParser() |
| 672 | options, parse_errors = mypy_comments_to_config_map(line, template) |
| 673 | if "python_version" in options: |
| 674 | errors.append((lineno, "python_version not supported in inline configuration")) |
| 675 | del options["python_version"] |
| 676 | |
| 677 | parser["dummy"] = options |
| 678 | errors.extend((lineno, x) for x in parse_errors) |
| 679 | |
| 680 | stderr = StringIO() |
| 681 | strict_found = False |
| 682 | |
| 683 | def set_strict_flags() -> None: |
| 684 | nonlocal strict_found |
| 685 | strict_found = True |
| 686 | |
| 687 | new_sections, reports = parse_section( |
| 688 | "", template, set_strict_flags, parser["dummy"], ini_config_types, stderr=stderr |
| 689 | ) |
| 690 | errors.extend((lineno, x) for x in stderr.getvalue().strip().split("\n") if x) |
| 691 | if reports: |
| 692 | errors.append((lineno, "Reports not supported in inline configuration")) |
| 693 | if strict_found: |
| 694 | errors.append( |
| 695 | ( |
| 696 | lineno, |
| 697 | 'Setting "strict" not supported in inline configuration: specify it in ' |
| 698 | "a configuration file instead, or set individual inline flags " |
| 699 | '(see "mypy -h" for the list of flags enabled in strict mode)', |
| 700 | ) |
| 701 | ) |
| 702 | # Because this is currently special-cased |
| 703 | # (the new_sections for an inline config *always* includes 'disable_error_code' and |
| 704 | # 'enable_error_code' fields, usually empty, which overwrite the old ones), |
| 705 | # we have to manipulate them specially. |
| 706 | # This could use a refactor, but so could the whole subsystem. |
| 707 | if ( |
| 708 | "enable_error_code" in new_sections |
| 709 | and isinstance(neec := new_sections["enable_error_code"], list) |
| 710 | and isinstance(eec := sections.get("enable_error_code", []), list) |
| 711 | ): |
| 712 | new_sections["enable_error_code"] = sorted(set(neec + eec)) |
| 713 | if ( |
searching dependent graphs…