Skip to content

Commit f1fbb1e

Browse files
gvanrossumddfisher
authored andcommitted
Add --warn-unused-configs flag (#3865)
This is handy for debugging misspellings in mypy.ini, and other mishaps due to e.g. file moves.
1 parent d96b2cf commit f1fbb1e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

docs/source/config_file.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ characters.
3434
separated by commas. These sections specify additional flags that
3535
only apply to *modules* whose name matches at least one of the patterns.
3636

37+
.. note::
38+
39+
The ``warn_unused_configs`` flag may be useful to debug misspelled
40+
section names.
41+
3742
Global flags
3843
************
3944

@@ -72,6 +77,10 @@ The following global flags may only be set in the global section
7277
- ``warn_unused_ignores`` (Boolean, default False) warns about
7378
unneeded ``# type: ignore`` comments.
7479

80+
- ``warn_unused_configs`` (Boolean, default False) warns about
81+
per-module sections in the config file that didn't match any
82+
files processed in the current run.
83+
7584
- ``strict_optional`` (Boolean, default False) enables experimental
7685
strict Optional checks.
7786

mypy/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def main(script_path: Optional[str], args: Optional[List[str]] = None) -> None:
5353
a = e.messages
5454
if not e.use_stdout:
5555
serious = True
56+
if options.warn_unused_configs and options.unused_configs:
57+
print("Warning: unused section(s) in %s: %s" %
58+
(options.config_file,
59+
", ".join("[mypy-%s]" % glob for glob in options.unused_configs.values())),
60+
file=sys.stderr)
5661
if options.junit_xml:
5762
t1 = time.time()
5863
util.write_junit_xml(t1 - t0, serious, a, options.junit_xml)
@@ -281,6 +286,8 @@ def add_invertible_flag(flag: str,
281286
" from non-Any typed functions")
282287
add_invertible_flag('--warn-unused-ignores', default=False, strict_flag=True,
283288
help="warn about unneeded '# type: ignore' comments")
289+
add_invertible_flag('--warn-unused-configs', default=False, strict_flag=True,
290+
help="warn about unnused '[mypy-<pattern>]' config sections")
284291
add_invertible_flag('--show-error-context', default=False,
285292
dest='show_error_context',
286293
help='Precede errors with "note:" messages explaining context')
@@ -695,6 +702,7 @@ def parse_config_file(options: Options, filename: Optional[str]) -> None:
695702
glob = glob.replace(os.altsep, '.')
696703
pattern = re.compile(fnmatch.translate(glob))
697704
options.per_module_options[pattern] = updates
705+
options.unused_configs[pattern] = glob
698706

699707

700708
def parse_section(prefix: str, template: Options,

mypy/options.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pprint
44
import sys
55

6-
from typing import Mapping, MutableMapping, Optional, Tuple, List, Pattern, Dict
6+
from typing import Dict, List, Mapping, MutableMapping, Optional, Pattern, Set, Tuple
77

88
from mypy import defaults
99

@@ -87,6 +87,9 @@ def __init__(self) -> None:
8787
# Warn about unused '# type: ignore' comments
8888
self.warn_unused_ignores = False
8989

90+
# Warn about unused '[mypy-<pattern>] config sections
91+
self.warn_unused_configs = False
92+
9093
# Files in which to ignore all non-fatal errors
9194
self.ignore_errors = False
9295

@@ -131,6 +134,8 @@ def __init__(self) -> None:
131134
# Per-module options (raw)
132135
pm_opts = OrderedDict() # type: OrderedDict[Pattern[str], Dict[str, object]]
133136
self.per_module_options = pm_opts
137+
# Map pattern back to glob
138+
self.unused_configs = OrderedDict() # type: OrderedDict[Pattern[str], str]
134139

135140
# -- development options --
136141
self.verbosity = 0 # More verbose messages (for troubleshooting)
@@ -164,6 +169,8 @@ def clone_for_module(self, module: str) -> 'Options':
164169
updates = {}
165170
for pattern in self.per_module_options:
166171
if self.module_matches_pattern(module, pattern):
172+
if pattern in self.unused_configs:
173+
del self.unused_configs[pattern]
167174
updates.update(self.per_module_options[pattern])
168175
if not updates:
169176
return self

0 commit comments

Comments
 (0)