Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add config key to extend config files #19135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Account for strict flag override
  • Loading branch information
hasier committed May 28, 2025
commit a0c4d6c862c270c6033003a6fe23a6b46f9fc99e
30 changes: 19 additions & 11 deletions 30 mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,15 @@ def parse_config_file(
stdout = stdout or sys.stdout
stderr = stderr or sys.stderr

strict_found = False

def set_strict(value: bool) -> None:
nonlocal strict_found
strict_found = value

ret = _parse_and_extend_config_file(
template=options,
set_strict_flags=set_strict_flags,
set_strict=set_strict,
filename=filename,
stdout=stdout,
stderr=stderr,
Expand All @@ -315,6 +321,9 @@ def parse_config_file(

file_read, mypy_updates, mypy_report_dirs, module_updates = ret

if strict_found:
set_strict_flags()

options.config_file = file_read

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

def _parse_and_extend_config_file(
template: Options,
set_strict_flags: Callable[[], None],
set_strict: Callable[[bool], None],
filename: str | None,
stdout: TextIO,
stderr: TextIO,
Expand Down Expand Up @@ -384,7 +393,7 @@ def _parse_and_extend_config_file(
if extend:
parse_ret = _parse_and_extend_config_file(
template=template,
set_strict_flags=set_strict_flags,
set_strict=set_strict,
# refer to extend relative to directory where we found current config
filename=os.path.relpath(
os.path.normpath(
Expand All @@ -403,7 +412,7 @@ def _parse_and_extend_config_file(

prefix = f"{file_read}: [mypy]: "
updates, report_dirs = parse_section(
prefix, template, set_strict_flags, section, config_types, stderr
prefix, template, set_strict, section, config_types, stderr
)
# extend and overwrite existing values with new ones
_merge_updates(mypy_updates, updates)
Expand All @@ -413,7 +422,7 @@ def _parse_and_extend_config_file(
if name.startswith("mypy-"):
prefix = get_prefix(file_read, name)
updates, report_dirs = parse_section(
prefix, template, set_strict_flags, section, config_types, stderr
prefix, template, set_strict, section, config_types, stderr
)
if report_dirs:
print(
Expand Down Expand Up @@ -555,7 +564,7 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
def parse_section(
prefix: str,
template: Options,
set_strict_flags: Callable[[], None],
set_strict: Callable[[bool], None],
section: Mapping[str, Any],
config_types: dict[str, Any],
stderr: TextIO = sys.stderr,
Expand Down Expand Up @@ -644,8 +653,7 @@ def parse_section(
print(f"{prefix}{key}: {err}", file=stderr)
continue
if key == "strict":
if v:
set_strict_flags()
set_strict(v)
continue
results[options_key] = v

Expand Down Expand Up @@ -746,12 +754,12 @@ def parse_mypy_comments(
stderr = StringIO()
strict_found = False

def set_strict_flags() -> None:
def set_strict(value: bool) -> None:
nonlocal strict_found
strict_found = True
strict_found = value

new_sections, reports = parse_section(
"", template, set_strict_flags, parser["dummy"], ini_config_types, stderr=stderr
"", template, set_strict, parser["dummy"], ini_config_types, stderr=stderr
)
errors.extend((lineno, x) for x in stderr.getvalue().strip().split("\n") if x)
if reports:
Expand Down
29 changes: 29 additions & 0 deletions 29 mypy/test/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,32 @@ def test_extend_cyclic(self) -> None:
f"Circular extend detected: {pyproject}\n"
f"../pyproject.toml is not a valid path to extend from {ini}\n"
)

def test_extend_strict_override(self) -> None:
with tempfile.TemporaryDirectory() as _tmpdir:
tmpdir = Path(_tmpdir)
with chdir(tmpdir):
pyproject = tmpdir / "pyproject.toml"
write_config(
pyproject, '[tool.mypy]\nextend = "./folder/mypy.ini"\nstrict = True\n'
)

folder = tmpdir / "folder"
folder.mkdir()
ini = folder / "mypy.ini"
write_config(ini, "[mypy]\nstrict = false\n")

options = Options()

stdout = io.StringIO()
stderr = io.StringIO()

strict_called = False

def set_strict_flags() -> None:
nonlocal strict_called
strict_called = True

parse_config_file(options, set_strict_flags, None, stdout, stderr)

assert strict_called is False
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.