From 4bf20a65c9c36fe5eb1ef58f4feb2cd61b0f1eac Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 23 Aug 2017 14:44:47 -0700 Subject: [PATCH 1/2] Add --warn-unused-configs flag --- docs/source/config_file.rst | 9 +++++++++ mypy/main.py | 8 ++++++++ mypy/options.py | 9 ++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 9a60f6de7815..9f5c832387f1 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -34,6 +34,11 @@ characters. separated by commas. These sections specify additional flags that only apply to *modules* whose name matches at least one of the patterns. +.. note:: + + The ``warn_unused_configs`` flag may be useful to debug misspelled + section names. + Global flags ************ @@ -72,6 +77,10 @@ The following global flags may only be set in the global section - ``warn_unused_ignores`` (Boolean, default False) warns about unneeded ``# type: ignore`` comments. +- ``warn_unused_configs`` (Boolean, default False) warns about + per-module sections in the config file that didn't match any + files processed in the current run. + - ``strict_optional`` (Boolean, default False) enables experimental strict Optional checks. diff --git a/mypy/main.py b/mypy/main.py index 0950a4d0b67d..878d3ccf65ad 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -53,6 +53,11 @@ def main(script_path: Optional[str], args: Optional[List[str]] = None) -> None: a = e.messages if not e.use_stdout: serious = True + if options.warn_unused_configs and options.unused_configs: + print("warning: unused section(s) in %s: %s" % + (options.config_file, + ", ".join("[mypy-%s]" % glob for glob in options.unused_configs.values())), + file=sys.stderr) if options.junit_xml: t1 = time.time() util.write_junit_xml(t1 - t0, serious, a, options.junit_xml) @@ -280,6 +285,8 @@ def add_invertible_flag(flag: str, " from non-Any typed functions") add_invertible_flag('--warn-unused-ignores', default=False, strict_flag=True, help="warn about unneeded '# type: ignore' comments") + add_invertible_flag('--warn-unused-configs', default=False, strict_flag=True, + help="warn about unnused '[mypy-]' config sections") add_invertible_flag('--show-error-context', default=False, dest='show_error_context', help='Precede errors with "note:" messages explaining context') @@ -694,6 +701,7 @@ def parse_config_file(options: Options, filename: Optional[str]) -> None: glob = glob.replace(os.altsep, '.') pattern = re.compile(fnmatch.translate(glob)) options.per_module_options[pattern] = updates + options.unused_configs[pattern] = glob def parse_section(prefix: str, template: Options, diff --git a/mypy/options.py b/mypy/options.py index f9bc327d90e0..c6ee84c1f64f 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -3,7 +3,7 @@ import pprint import sys -from typing import Mapping, MutableMapping, Optional, Tuple, List, Pattern, Dict +from typing import Dict, List, Mapping, MutableMapping, Optional, Pattern, Set, Tuple from mypy import defaults @@ -87,6 +87,9 @@ def __init__(self) -> None: # Warn about unused '# type: ignore' comments self.warn_unused_ignores = False + # Warn about unused '[mypy-] config sections + self.warn_unused_configs = False + # Files in which to ignore all non-fatal errors self.ignore_errors = False @@ -131,6 +134,8 @@ def __init__(self) -> None: # Per-module options (raw) pm_opts = OrderedDict() # type: OrderedDict[Pattern[str], Dict[str, object]] self.per_module_options = pm_opts + # Map pattern back to glob + self.unused_configs = OrderedDict() # type: OrderedDict[Pattern[str], str] # -- development options -- self.verbosity = 0 # More verbose messages (for troubleshooting) @@ -164,6 +169,8 @@ def clone_for_module(self, module: str) -> 'Options': updates = {} for pattern in self.per_module_options: if self.module_matches_pattern(module, pattern): + if pattern in self.unused_configs: + del self.unused_configs[pattern] updates.update(self.per_module_options[pattern]) if not updates: return self From 26ed09f877239f29c6159517520b6df80cef9aef Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 23 Aug 2017 15:03:56 -0700 Subject: [PATCH 2/2] Use capitalized 'Warning', like other warnings here --- mypy/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index 878d3ccf65ad..a38249a86147 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -54,7 +54,7 @@ def main(script_path: Optional[str], args: Optional[List[str]] = None) -> None: if not e.use_stdout: serious = True if options.warn_unused_configs and options.unused_configs: - print("warning: unused section(s) in %s: %s" % + print("Warning: unused section(s) in %s: %s" % (options.config_file, ", ".join("[mypy-%s]" % glob for glob in options.unused_configs.values())), file=sys.stderr)