Skip to content
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
17 changes: 11 additions & 6 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import configparser
from functools import partial
import glob as fileglob
from io import StringIO
import os
Expand Down Expand Up @@ -153,7 +154,8 @@ def check_follow_imports(choice: str) -> str:
})


def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
def parse_config_file(options: Options,
set_strict_flags: Callable[[bool, Dict[str, object]], None],
filename: Optional[str],
stdout: Optional[TextIO] = None,
stderr: Optional[TextIO] = None) -> None:
Expand Down Expand Up @@ -205,14 +207,17 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
os.environ['MYPY_CONFIG_FILE_DIR'] = os.path.dirname(
os.path.abspath(config_file))

set_strict_flags_toplevel = partial(set_strict_flags, True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can create a real function instead? 🤔

Copy link
Author

@icgood icgood Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a small refactor using inline functions with a stronger unit test.

set_strict_flags_module = partial(set_strict_flags, False)

if 'mypy' not in parser:
if filename or file_read not in defaults.SHARED_CONFIG_FILES:
print("%s: No [mypy] section in config file" % file_read, file=stderr)
else:
section = parser['mypy']
prefix = '%s: [%s]: ' % (file_read, 'mypy')
updates, report_dirs = parse_section(
prefix, options, set_strict_flags, section, config_types, stderr)
prefix, options, set_strict_flags_toplevel, section, config_types, stderr)
for k, v in updates.items():
setattr(options, k, v)
options.report_dirs.update(report_dirs)
Expand All @@ -221,7 +226,7 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
if name.startswith('mypy-'):
prefix = get_prefix(file_read, name)
updates, report_dirs = parse_section(
prefix, options, set_strict_flags, section, config_types, stderr)
prefix, options, set_strict_flags_module, section, config_types, stderr)
if report_dirs:
print("%sPer-module sections should not specify reports (%s)" %
(prefix, ', '.join(s + '_report' for s in sorted(report_dirs))),
Expand Down Expand Up @@ -336,7 +341,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_flags: Callable[[Dict[str, object]], None],
section: Mapping[str, Any],
config_types: Dict[str, Any],
stderr: TextIO = sys.stderr
Expand Down Expand Up @@ -416,7 +421,7 @@ def parse_section(prefix: str, template: Options,
continue
if key == 'strict':
if v:
set_strict_flags()
set_strict_flags(results)
continue
if key == 'silent_imports':
print("%ssilent_imports has been replaced by "
Expand Down Expand Up @@ -523,7 +528,7 @@ def parse_mypy_comments(
stderr = StringIO()
strict_found = False

def set_strict_flags() -> None:
def set_strict_flags(ignored: Any) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer having reasonable name/type even if ignored.

nonlocal strict_found
strict_found = True

Expand Down
10 changes: 6 additions & 4 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from mypy.fscache import FileSystemCache
from mypy.errors import CompileError
from mypy.errorcodes import error_codes
from mypy.options import Options, BuildType
from mypy.options import Options, BuildType, PER_MODULE_OPTIONS
from mypy.config_parser import get_config_module_names, parse_version, parse_config_file
from mypy.split_namespace import SplitNamespace

Expand Down Expand Up @@ -911,17 +911,19 @@ def add_invertible_flag(flag: str,

options = Options()

def set_strict_flags() -> None:
def set_strict_flags(toplevel: bool, results: Dict[str, object]) -> None:
for dest, value in strict_flag_assignments:
setattr(options, dest, value)
if toplevel or dest in PER_MODULE_OPTIONS:
results[dest] = value

# Parse config file first, so command line can override.
parse_config_file(options, set_strict_flags, config_file, stdout, stderr)

# Set strict flags before parsing (if strict mode enabled), so other command
# line options can override.
if getattr(dummy, 'special-opts:strict'): # noqa
set_strict_flags()
for dest, value in strict_flag_assignments:
setattr(options, dest, value)

# Override cache_dir if provided in the environment
environ_cache_dir = os.getenv('MYPY_CACHE_DIR', '')
Expand Down
4 changes: 2 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,8 @@ def test_stubs(args: argparse.Namespace, use_builtins_fixtures: bool = False) ->
options.use_builtins_fixtures = use_builtins_fixtures

if options.config_file:
def set_strict_flags() -> None: # not needed yet
return
def set_strict_flags(toplevel: bool, results: Dict[str, object]) -> None: # not needed yet
print("note: set_strict_flags called with toplevel={}".format(toplevel))
parse_config_file(options, set_strict_flags, options.config_file, sys.stdout, sys.stderr)

try:
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testfinegrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_options(self,

for name, _ in testcase.files:
if 'mypy.ini' in name or 'pyproject.toml' in name:
parse_config_file(options, lambda: None, name)
parse_config_file(options, lambda t, r: None, name)
break

return options
Expand Down
14 changes: 14 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,17 @@ def test_config_file(self) -> None:
)
output = run_stubtest(stub=stub, runtime=runtime, options=[], config_file=config_file)
assert output == ""

def test_config_file_strict(self) -> None:
config_file = (
"[mypy]\n"
"strict = True\n"
)
output = run_stubtest(stub="", runtime="", options=[], config_file=config_file)
assert output == "note: set_strict_flags called with toplevel=True\n"
config_file = (
"[mypy-submodule.*]\n"
"strict = True\n"
)
output = run_stubtest(stub="", runtime="", options=[], config_file=config_file)
assert output == "note: set_strict_flags called with toplevel=False\n"