@@ -135,6 +135,23 @@ def process_options(args: List[str],
135
135
fromfile_prefix_chars = '@' ,
136
136
formatter_class = AugmentedHelpFormatter )
137
137
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
+
138
155
# Unless otherwise specified, arguments will be parsed directly onto an
139
156
# Options object. Options that require further processing should have
140
157
# their `dest` prefixed with `special-opts:`, which will cause them to be
@@ -154,37 +171,44 @@ def process_options(args: List[str],
154
171
help = "silently ignore imports of missing modules" )
155
172
parser .add_argument ('--follow-imports' , choices = ['normal' , 'silent' , 'skip' , 'error' ],
156
173
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 ,
158
176
help = "disallow calling functions without type annotations"
159
177
" 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 ,
161
180
help = "disallow defining functions without type annotations"
162
181
" 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 ,
164
184
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 ,
166
187
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 ,
168
190
help = "warn if missing type annotation in typeshed, only relevant with"
169
191
" --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 ,
171
194
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 ,
173
196
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 ,
175
199
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 ,
177
201
dest = 'hide_error_context' ,
178
202
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)" )
181
205
parser .add_argument ('-i' , '--incremental' , action = 'store_true' ,
182
206
help = "enable experimental module cache" )
183
207
parser .add_argument ('--cache-dir' , action = 'store' , metavar = 'DIR' ,
184
208
help = "store module cache info in the given folder in incremental mode "
185
209
"(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 ,
188
212
help = "enable experimental strict Optional checks" )
189
213
parser .add_argument ('--strict-optional-whitelist' , metavar = 'GLOB' , nargs = '*' ,
190
214
help = "suppress strict Optional errors in all but the provided files "
@@ -207,12 +231,15 @@ def process_options(args: List[str],
207
231
parser .add_argument ('--config-file' ,
208
232
help = "Configuration file, must have a [mypy] section "
209
233
"(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 ,
212
235
help = "Show column numbers in error messages" )
213
236
parser .add_argument ('--find-occurrences' , metavar = 'CLASS.MEMBER' ,
214
237
dest = 'special-opts:find_occurrences' ,
215
238
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 )
216
243
# hidden options
217
244
# --shadow-file a.py tmp.py will typecheck tmp.py in place of a.py.
218
245
# Useful for tools to make transformations to a file to get more
@@ -226,9 +253,6 @@ def process_options(args: List[str],
226
253
parser .add_argument ('--debug-cache' , action = 'store_true' , help = argparse .SUPPRESS )
227
254
# --dump-graph will dump the contents of the graph of SCCs and exit.
228
255
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 )
232
256
# deprecated options
233
257
parser .add_argument ('-f' , '--dirty-stubs' , action = 'store_true' ,
234
258
dest = 'special-opts:dirty_stubs' ,
@@ -321,6 +345,11 @@ def process_options(args: List[str],
321
345
elif code_methods > 1 :
322
346
parser .error ("May only specify one of: module, package, files, or command." )
323
347
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
+
324
353
# Set build flags.
325
354
if options .strict_optional_whitelist is not None :
326
355
# TODO: Deprecate, then kill this flag
0 commit comments