|
6 | 6 | import re
|
7 | 7 | import sys
|
8 | 8 |
|
9 |
| -from typing import Any, Dict, List, Mapping, Optional, Tuple, TextIO |
| 9 | +from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, TextIO |
10 | 10 | from typing_extensions import Final
|
11 | 11 |
|
12 | 12 | from mypy import defaults
|
@@ -88,7 +88,8 @@ def split_and_match_files(paths: str) -> List[str]:
|
88 | 88 | } # type: Final
|
89 | 89 |
|
90 | 90 |
|
91 |
| -def parse_config_file(options: Options, filename: Optional[str], |
| 91 | +def parse_config_file(options: Options, set_strict_flags: Callable[[], None], |
| 92 | + filename: Optional[str], |
92 | 93 | stdout: Optional[TextIO] = None,
|
93 | 94 | stderr: Optional[TextIO] = None) -> None:
|
94 | 95 | """Parse a config file into an Options object.
|
@@ -127,15 +128,16 @@ def parse_config_file(options: Options, filename: Optional[str],
|
127 | 128 | else:
|
128 | 129 | section = parser['mypy']
|
129 | 130 | prefix = '%s: [%s]: ' % (file_read, 'mypy')
|
130 |
| - updates, report_dirs = parse_section(prefix, options, section, stderr) |
| 131 | + updates, report_dirs = parse_section(prefix, options, set_strict_flags, section, stderr) |
131 | 132 | for k, v in updates.items():
|
132 | 133 | setattr(options, k, v)
|
133 | 134 | options.report_dirs.update(report_dirs)
|
134 | 135 |
|
135 | 136 | for name, section in parser.items():
|
136 | 137 | if name.startswith('mypy-'):
|
137 | 138 | prefix = '%s: [%s]: ' % (file_read, name)
|
138 |
| - updates, report_dirs = parse_section(prefix, options, section, stderr) |
| 139 | + updates, report_dirs = parse_section( |
| 140 | + prefix, options, set_strict_flags, section, stderr) |
139 | 141 | if report_dirs:
|
140 | 142 | print("%sPer-module sections should not specify reports (%s)" %
|
141 | 143 | (prefix, ', '.join(s + '_report' for s in sorted(report_dirs))),
|
@@ -163,6 +165,7 @@ def parse_config_file(options: Options, filename: Optional[str],
|
163 | 165 |
|
164 | 166 |
|
165 | 167 | def parse_section(prefix: str, template: Options,
|
| 168 | + set_strict_flags: Callable[[], None], |
166 | 169 | section: Mapping[str, str],
|
167 | 170 | stderr: TextIO = sys.stderr
|
168 | 171 | ) -> Tuple[Dict[str, object], Dict[str, str]]:
|
@@ -205,9 +208,7 @@ def parse_section(prefix: str, template: Options,
|
205 | 208 | options_key = key[3:]
|
206 | 209 | invert = True
|
207 | 210 | elif key == 'strict':
|
208 |
| - print("%sStrict mode is not supported in configuration files: specify " |
209 |
| - "individual flags instead (see 'mypy -h' for the list of flags enabled " |
210 |
| - "in strict mode)" % prefix, file=stderr) |
| 211 | + set_strict_flags() |
211 | 212 | else:
|
212 | 213 | print("%sUnrecognized option: %s = %s" % (prefix, key, section[key]),
|
213 | 214 | file=stderr)
|
@@ -330,10 +331,23 @@ def parse_mypy_comments(
|
330 | 331 | errors.extend((lineno, x) for x in parse_errors)
|
331 | 332 |
|
332 | 333 | stderr = StringIO()
|
333 |
| - new_sections, reports = parse_section('', template, parser['dummy'], stderr=stderr) |
| 334 | + strict_found = False |
| 335 | + |
| 336 | + def set_strict_flags() -> None: |
| 337 | + nonlocal strict_found |
| 338 | + strict_found = True |
| 339 | + |
| 340 | + new_sections, reports = parse_section( |
| 341 | + '', template, set_strict_flags, parser['dummy'], stderr=stderr) |
334 | 342 | errors.extend((lineno, x) for x in stderr.getvalue().strip().split('\n') if x)
|
335 | 343 | if reports:
|
336 | 344 | errors.append((lineno, "Reports not supported in inline configuration"))
|
| 345 | + if strict_found: |
| 346 | + errors.append((lineno, |
| 347 | + "Setting 'strict' not supported in inline configuration: specify it in " |
| 348 | + "a configuration file instead, or set individual inline flags " |
| 349 | + "(see 'mypy -h' for the list of flags enabled in strict mode)")) |
| 350 | + |
337 | 351 | sections.update(new_sections)
|
338 | 352 |
|
339 | 353 | return sections, errors
|
0 commit comments