@@ -72,13 +72,18 @@ def main(script_path: Optional[str],
72
72
73
73
if options .install_types and (stdout is not sys .stdout or stderr is not sys .stderr ):
74
74
# Since --install-types performs user input, we want regular stdout and stderr.
75
- fail ("--install-types not supported in this mode of running mypy" , stderr , options )
75
+ fail ("Error: --install-types not supported in this mode of running mypy" , stderr , options )
76
+
77
+ if options .non_interactive and not options .install_types :
78
+ fail ("Error: --non-interactive is only supported with --install-types" , stderr , options )
76
79
77
80
if options .install_types and not sources :
78
- install_types (options .cache_dir , formatter )
81
+ install_types (options .cache_dir , formatter , non_interactive = options . non_interactive )
79
82
return
80
83
81
84
def flush_errors (new_messages : List [str ], serious : bool ) -> None :
85
+ if options .non_interactive :
86
+ return
82
87
if options .pretty :
83
88
new_messages = formatter .fit_in_terminal (new_messages )
84
89
messages .extend (new_messages )
@@ -100,7 +105,10 @@ def flush_errors(new_messages: List[str], serious: bool) -> None:
100
105
blockers = True
101
106
if not e .use_stdout :
102
107
serious = True
103
- if options .warn_unused_configs and options .unused_configs and not options .incremental :
108
+ if (options .warn_unused_configs
109
+ and options .unused_configs
110
+ and not options .incremental
111
+ and not options .non_interactive ):
104
112
print ("Warning: unused section(s) in %s: %s" %
105
113
(options .config_file ,
106
114
get_config_module_names (options .config_file ,
@@ -116,7 +124,7 @@ def flush_errors(new_messages: List[str], serious: bool) -> None:
116
124
code = 0
117
125
if messages :
118
126
code = 2 if blockers else 1
119
- if options .error_summary :
127
+ if options .error_summary and not options . non_interactive :
120
128
if messages :
121
129
n_errors , n_files = util .count_stats (messages )
122
130
if n_errors :
@@ -130,7 +138,8 @@ def flush_errors(new_messages: List[str], serious: bool) -> None:
130
138
stdout .flush ()
131
139
132
140
if options .install_types :
133
- install_types (options .cache_dir , formatter , after_run = True )
141
+ install_types (options .cache_dir , formatter , after_run = True ,
142
+ non_interactive = options .non_interactive )
134
143
return
135
144
136
145
if options .fast_exit :
@@ -751,6 +760,10 @@ def add_invertible_flag(flag: str,
751
760
add_invertible_flag ('--install-types' , default = False , strict_flag = False ,
752
761
help = "Install detected missing library stub packages using pip" ,
753
762
group = other_group )
763
+ add_invertible_flag ('--non-interactive' , default = False , strict_flag = False ,
764
+ help = ("Install stubs without asking for confirmation and hide " +
765
+ "errors, with --install-types" ),
766
+ group = other_group , inverse = "--interactive" )
754
767
755
768
if server_options :
756
769
# TODO: This flag is superfluous; remove after a short transition (2018-03-16)
@@ -1072,7 +1085,9 @@ def fail(msg: str, stderr: TextIO, options: Options) -> None:
1072
1085
1073
1086
def install_types (cache_dir : str ,
1074
1087
formatter : util .FancyFormatter ,
1075
- after_run : bool = False ) -> None :
1088
+ * ,
1089
+ after_run : bool = False ,
1090
+ non_interactive : bool = False ) -> None :
1076
1091
"""Install stub packages using pip if some missing stubs were detected."""
1077
1092
if not os .path .isdir (cache_dir ):
1078
1093
sys .stderr .write (
@@ -1084,15 +1099,16 @@ def install_types(cache_dir: str,
1084
1099
return
1085
1100
with open (fnam ) as f :
1086
1101
packages = [line .strip () for line in f .readlines ()]
1087
- if after_run :
1102
+ if after_run and not non_interactive :
1088
1103
print ()
1089
1104
print ('Installing missing stub packages:' )
1090
1105
cmd = [sys .executable , '-m' , 'pip' , 'install' ] + packages
1091
1106
print (formatter .style (' ' .join (cmd ), 'none' , bold = True ))
1092
1107
print ()
1093
- x = input ('Install? [yN] ' )
1094
- if not x .strip () or not x .lower ().startswith ('y' ):
1095
- print (formatter .style ('mypy: Skipping installation' , 'red' , bold = True ))
1096
- sys .exit (2 )
1097
- print ()
1108
+ if not non_interactive :
1109
+ x = input ('Install? [yN] ' )
1110
+ if not x .strip () or not x .lower ().startswith ('y' ):
1111
+ print (formatter .style ('mypy: Skipping installation' , 'red' , bold = True ))
1112
+ sys .exit (2 )
1113
+ print ()
1098
1114
subprocess .run (cmd )
0 commit comments