Skip to content

Commit a0c4d6c

Browse files
committed
Account for strict flag override
1 parent ed41583 commit a0c4d6c

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

mypy/config_parser.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,15 @@ def parse_config_file(
301301
stdout = stdout or sys.stdout
302302
stderr = stderr or sys.stderr
303303

304+
strict_found = False
305+
306+
def set_strict(value: bool) -> None:
307+
nonlocal strict_found
308+
strict_found = value
309+
304310
ret = _parse_and_extend_config_file(
305311
template=options,
306-
set_strict_flags=set_strict_flags,
312+
set_strict=set_strict,
307313
filename=filename,
308314
stdout=stdout,
309315
stderr=stderr,
@@ -315,6 +321,9 @@ def parse_config_file(
315321

316322
file_read, mypy_updates, mypy_report_dirs, module_updates = ret
317323

324+
if strict_found:
325+
set_strict_flags()
326+
318327
options.config_file = file_read
319328

320329
for k, v in mypy_updates.items():
@@ -344,7 +353,7 @@ def _merge_updates(existing: dict[str, object], new: dict[str, object]) -> None:
344353

345354
def _parse_and_extend_config_file(
346355
template: Options,
347-
set_strict_flags: Callable[[], None],
356+
set_strict: Callable[[bool], None],
348357
filename: str | None,
349358
stdout: TextIO,
350359
stderr: TextIO,
@@ -384,7 +393,7 @@ def _parse_and_extend_config_file(
384393
if extend:
385394
parse_ret = _parse_and_extend_config_file(
386395
template=template,
387-
set_strict_flags=set_strict_flags,
396+
set_strict=set_strict,
388397
# refer to extend relative to directory where we found current config
389398
filename=os.path.relpath(
390399
os.path.normpath(
@@ -403,7 +412,7 @@ def _parse_and_extend_config_file(
403412

404413
prefix = f"{file_read}: [mypy]: "
405414
updates, report_dirs = parse_section(
406-
prefix, template, set_strict_flags, section, config_types, stderr
415+
prefix, template, set_strict, section, config_types, stderr
407416
)
408417
# extend and overwrite existing values with new ones
409418
_merge_updates(mypy_updates, updates)
@@ -413,7 +422,7 @@ def _parse_and_extend_config_file(
413422
if name.startswith("mypy-"):
414423
prefix = get_prefix(file_read, name)
415424
updates, report_dirs = parse_section(
416-
prefix, template, set_strict_flags, section, config_types, stderr
425+
prefix, template, set_strict, section, config_types, stderr
417426
)
418427
if report_dirs:
419428
print(
@@ -555,7 +564,7 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
555564
def parse_section(
556565
prefix: str,
557566
template: Options,
558-
set_strict_flags: Callable[[], None],
567+
set_strict: Callable[[bool], None],
559568
section: Mapping[str, Any],
560569
config_types: dict[str, Any],
561570
stderr: TextIO = sys.stderr,
@@ -644,8 +653,7 @@ def parse_section(
644653
print(f"{prefix}{key}: {err}", file=stderr)
645654
continue
646655
if key == "strict":
647-
if v:
648-
set_strict_flags()
656+
set_strict(v)
649657
continue
650658
results[options_key] = v
651659

@@ -746,12 +754,12 @@ def parse_mypy_comments(
746754
stderr = StringIO()
747755
strict_found = False
748756

749-
def set_strict_flags() -> None:
757+
def set_strict(value: bool) -> None:
750758
nonlocal strict_found
751-
strict_found = True
759+
strict_found = value
752760

753761
new_sections, reports = parse_section(
754-
"", template, set_strict_flags, parser["dummy"], ini_config_types, stderr=stderr
762+
"", template, set_strict, parser["dummy"], ini_config_types, stderr=stderr
755763
)
756764
errors.extend((lineno, x) for x in stderr.getvalue().strip().split("\n") if x)
757765
if reports:

mypy/test/test_config_parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,32 @@ def test_extend_cyclic(self) -> None:
209209
f"Circular extend detected: {pyproject}\n"
210210
f"../pyproject.toml is not a valid path to extend from {ini}\n"
211211
)
212+
213+
def test_extend_strict_override(self) -> None:
214+
with tempfile.TemporaryDirectory() as _tmpdir:
215+
tmpdir = Path(_tmpdir)
216+
with chdir(tmpdir):
217+
pyproject = tmpdir / "pyproject.toml"
218+
write_config(
219+
pyproject, '[tool.mypy]\nextend = "./folder/mypy.ini"\nstrict = True\n'
220+
)
221+
222+
folder = tmpdir / "folder"
223+
folder.mkdir()
224+
ini = folder / "mypy.ini"
225+
write_config(ini, "[mypy]\nstrict = false\n")
226+
227+
options = Options()
228+
229+
stdout = io.StringIO()
230+
stderr = io.StringIO()
231+
232+
strict_called = False
233+
234+
def set_strict_flags() -> None:
235+
nonlocal strict_called
236+
strict_called = True
237+
238+
parse_config_file(options, set_strict_flags, None, stdout, stderr)
239+
240+
assert strict_called is False

0 commit comments

Comments
 (0)