Skip to content

Commit 68c893d

Browse files
committed
Add inverted options and strict mode
1 parent 6bf6c1e commit 68c893d

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

docs/source/command_line.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ flag (or its long form ``--help``)::
1616
[--warn-incomplete-stub] [--warn-redundant-casts]
1717
[--warn-no-return] [--warn-unused-ignores] [--show-error-context]
1818
[--fast-parser] [-i] [--cache-dir DIR] [--strict-optional]
19-
[--strict-optional-whitelist [GLOB [GLOB ...]]]
19+
[--strict-optional-whitelist [GLOB [GLOB ...]]] [--strict]
2020
[--junit-xml JUNIT_XML] [--pdb] [--show-traceback] [--stats]
2121
[--inferstats] [--custom-typing MODULE]
2222
[--custom-typeshed-dir DIR] [--scripts-are-modules]
@@ -366,7 +366,8 @@ Here are some more useful flags:
366366
also currently ignores functions with an empty body or a body that is
367367
just ellipsis (``...``), since these can be valid as abstract methods.
368368

369-
For the remaining flags you can read the full ``mypy -h`` output.
369+
- ``--strict`` mode enables all optional error checking flags. You can see the
370+
list of flags enabled by strict mode in the full ``mypy -h`` output.
370371

371372
.. note::
372373

mypy/main.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ def process_options(args: List[str],
135135
fromfile_prefix_chars='@',
136136
formatter_class=AugmentedHelpFormatter)
137137

138+
strict_flag_names = [] # type: List[str]
139+
strict_flag_assignments = [] # type: List[Tuple[str, bool]]
140+
141+
def add_invertable_flag(flag, *, inverse, default, dest=None, help, strict_flag=False):
142+
arg = parser.add_argument(flag,
143+
action='store_false' if default else 'store_true',
144+
dest=dest,
145+
help=help + " (inverse: {})".format(inverse))
146+
dest = arg.dest
147+
arg = parser.add_argument(inverse,
148+
action='store_true' if default else 'store_false',
149+
dest=dest,
150+
help=argparse.SUPPRESS)
151+
if strict_flag:
152+
strict_flag_names.append(flag)
153+
strict_flag_assignments.append((dest, not default))
154+
138155
# Unless otherwise specified, arguments will be parsed directly onto an
139156
# Options object. Options that require further processing should have
140157
# their `dest` prefixed with `special-opts:`, which will cause them to be
@@ -154,37 +171,44 @@ def process_options(args: List[str],
154171
help="silently ignore imports of missing modules")
155172
parser.add_argument('--follow-imports', choices=['normal', 'silent', 'skip', 'error'],
156173
default='normal', help="how to treat imports (default normal)")
157-
parser.add_argument('--disallow-untyped-calls', action='store_true',
174+
add_invertable_flag('--disallow-untyped-calls', inverse='--allow-untyped-calls',
175+
default=False, strict_flag=True,
158176
help="disallow calling functions without type annotations"
159177
" from functions with type annotations")
160-
parser.add_argument('--disallow-untyped-defs', action='store_true',
178+
add_invertable_flag('--disallow-untyped-defs', inverse='--allow-untyped-defs',
179+
default=False, strict_flag=True,
161180
help="disallow defining functions without type annotations"
162181
" or with incomplete type annotations")
163-
parser.add_argument('--check-untyped-defs', action='store_true',
182+
add_invertable_flag('--check-untyped-defs', inverse='--ignore-untyped-defs',
183+
default=False, strict_flag=True,
164184
help="type check the interior of functions without type annotations")
165-
parser.add_argument('--disallow-subclassing-any', action='store_true',
185+
add_invertable_flag('--disallow-subclassing-any', inverse='--allow-subclassing-any',
186+
default=False, strict_flag=True,
166187
help="disallow subclassing values of type 'Any' when defining classes")
167-
parser.add_argument('--warn-incomplete-stub', action='store_true',
188+
add_invertable_flag('--warn-incomplete-stub', inverse='--no-warn-incomplete-stub',
189+
default=False,
168190
help="warn if missing type annotation in typeshed, only relevant with"
169191
" --check-untyped-defs enabled")
170-
parser.add_argument('--warn-redundant-casts', action='store_true',
192+
add_invertable_flag('--warn-redundant-casts', inverse='--no-warn-redundant-casts',
193+
default=False, strict_flag=True,
171194
help="warn about casting an expression to its inferred type")
172-
parser.add_argument('--warn-no-return', action='store_true',
195+
add_invertable_flag('--warn-no-return', inverse='--no-warn-no-return', default=False,
173196
help="warn about functions that end without returning")
174-
parser.add_argument('--warn-unused-ignores', action='store_true',
197+
add_invertable_flag('--warn-unused-ignores', inverse='--no-warn-unused-ignores',
198+
default=False, strict_flag=True,
175199
help="warn about unneeded '# type: ignore' comments")
176-
parser.add_argument('--show-error-context', action='store_false',
200+
add_invertable_flag('--show-error-context', inverse='--hide-error-context', default=True,
177201
dest='hide_error_context',
178202
help='Precede errors with "note:" messages explaining context')
179-
parser.add_argument('--fast-parser', action='store_true',
180-
help="enable fast parser (recommended except on Windows)")
203+
add_invertable_flag('--fast-parser', inverse='--old-parser', default=False,
204+
help="enable fast parser (recommended)")
181205
parser.add_argument('-i', '--incremental', action='store_true',
182206
help="enable experimental module cache")
183207
parser.add_argument('--cache-dir', action='store', metavar='DIR',
184208
help="store module cache info in the given folder in incremental mode "
185209
"(defaults to '{}')".format(defaults.CACHE_DIR))
186-
parser.add_argument('--strict-optional', action='store_true',
187-
dest='strict_optional',
210+
add_invertable_flag('--strict-optional', inverse='--no-strict-optional',
211+
default=False, strict_flag=True,
188212
help="enable experimental strict Optional checks")
189213
parser.add_argument('--strict-optional-whitelist', metavar='GLOB', nargs='*',
190214
help="suppress strict Optional errors in all but the provided files "
@@ -207,12 +231,15 @@ def process_options(args: List[str],
207231
parser.add_argument('--config-file',
208232
help="Configuration file, must have a [mypy] section "
209233
"(defaults to {})".format(defaults.CONFIG_FILE))
210-
parser.add_argument('--show-column-numbers', action='store_true',
211-
dest='show_column_numbers',
234+
add_invertable_flag('--show-column-numbers', inverse='--hide-column-numbers', default=False,
212235
help="Show column numbers in error messages")
213236
parser.add_argument('--find-occurrences', metavar='CLASS.MEMBER',
214237
dest='special-opts:find_occurrences',
215238
help="print out all usages of a class member (experimental)")
239+
strict_help = "Strict mode. Enables the following flags: {}".format(
240+
", ".join(strict_flag_names))
241+
parser.add_argument('--strict', action='store_true', dest='special-opts:strict',
242+
help=strict_help)
216243
# hidden options
217244
# --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
218245
# Useful for tools to make transformations to a file to get more
@@ -226,9 +253,6 @@ def process_options(args: List[str],
226253
parser.add_argument('--debug-cache', action='store_true', help=argparse.SUPPRESS)
227254
# --dump-graph will dump the contents of the graph of SCCs and exit.
228255
parser.add_argument('--dump-graph', action='store_true', help=argparse.SUPPRESS)
229-
parser.add_argument('--hide-error-context', action='store_true',
230-
dest='hide_error_context',
231-
help=argparse.SUPPRESS)
232256
# deprecated options
233257
parser.add_argument('-f', '--dirty-stubs', action='store_true',
234258
dest='special-opts:dirty_stubs',
@@ -321,6 +345,11 @@ def process_options(args: List[str],
321345
elif code_methods > 1:
322346
parser.error("May only specify one of: module, package, files, or command.")
323347

348+
# Set strict flags if strict mode enabled
349+
if special_opts.strict:
350+
for dest, value in strict_flag_assignments:
351+
setattr(options, dest, value)
352+
324353
# Set build flags.
325354
if options.strict_optional_whitelist is not None:
326355
# TODO: Deprecate, then kill this flag

0 commit comments

Comments
 (0)