77from pkg_resources import get_distribution , DistributionNotFound
88
99from python_minifier import minify
10+ from python_minifier .transforms .remove_annotations_options import RemoveAnnotationsOptions
1011
1112try :
1213 version = get_distribution ('python_minifier' ).version
@@ -113,12 +114,6 @@ def parse_args():
113114 help = 'Enable removing statements that are just a literal (including docstrings)' ,
114115 dest = 'remove_literal_statements' ,
115116 )
116- minification_options .add_argument (
117- '--no-remove-annotations' ,
118- action = 'store_false' ,
119- help = 'Disable removing function and variable annotations' ,
120- dest = 'remove_annotations' ,
121- )
122117 minification_options .add_argument (
123118 '--no-hoist-literals' ,
124119 action = 'store_false' ,
@@ -189,6 +184,39 @@ def parse_args():
189184 help = 'Replace explicit return None with a bare return' ,
190185 dest = 'remove_explicit_return_none' ,
191186 )
187+
188+ annotation_options = parser .add_argument_group ('remove annotations options' , 'Options that affect how annotations are removed' )
189+ annotation_options .add_argument (
190+ '--no-remove-annotations' ,
191+ action = 'store_false' ,
192+ help = 'Disable removing all annotations' ,
193+ dest = 'remove_annotations' ,
194+ )
195+ annotation_options .add_argument (
196+ '--no-remove-variable-annotations' ,
197+ action = 'store_false' ,
198+ help = 'Disable removing variable annotations' ,
199+ dest = 'remove_variable_annotations' ,
200+ )
201+ annotation_options .add_argument (
202+ '--no-remove-return-annotations' ,
203+ action = 'store_false' ,
204+ help = 'Disable removing function return annotations' ,
205+ dest = 'remove_return_annotations' ,
206+ )
207+ annotation_options .add_argument (
208+ '--no-remove-argument-annotations' ,
209+ action = 'store_false' ,
210+ help = 'Disable removing function argument annotations' ,
211+ dest = 'remove_argument_annotations' ,
212+ )
213+ annotation_options .add_argument (
214+ '--remove-class-attribute-annotations' ,
215+ action = 'store_true' ,
216+ help = 'Enable removing class attribute annotations' ,
217+ dest = 'remove_class_attribute_annotations' ,
218+ )
219+
192220 parser .add_argument ('--version' , '-v' , action = 'version' , version = version )
193221
194222 args = parser .parse_args ()
@@ -207,6 +235,10 @@ def parse_args():
207235 sys .stderr .write ('error: path ' + args .path [0 ] + ' is a directory, --in-place required\n ' )
208236 sys .exit (1 )
209237
238+ if args .remove_class_attribute_annotations and not args .remove_annotations :
239+ sys .stderr .write ('error: --remove-class-attribute-annotations would do nothing when used with --no-remove-annotations\n ' )
240+ sys .exit (1 )
241+
210242 return args
211243
212244def source_modules (args ):
@@ -237,12 +269,27 @@ def do_minify(source, filename, minification_args):
237269 names = [name .strip () for name in arg .split (',' ) if name ]
238270 preserve_locals .extend (names )
239271
272+ if minification_args .remove_annotations is False :
273+ remove_annotations = RemoveAnnotationsOptions (
274+ remove_variable_annotations = False ,
275+ remove_return_annotations = False ,
276+ remove_argument_annotations = False ,
277+ remove_class_attribute_annotations = False ,
278+ )
279+ else :
280+ remove_annotations = RemoveAnnotationsOptions (
281+ remove_variable_annotations = minification_args .remove_variable_annotations ,
282+ remove_return_annotations = minification_args .remove_return_annotations ,
283+ remove_argument_annotations = minification_args .remove_argument_annotations ,
284+ remove_class_attribute_annotations = minification_args .remove_class_attribute_annotations ,
285+ )
286+
240287 return minify (
241288 source ,
242289 filename = filename ,
243290 combine_imports = minification_args .combine_imports ,
244291 remove_pass = minification_args .remove_pass ,
245- remove_annotations = minification_args . remove_annotations ,
292+ remove_annotations = remove_annotations ,
246293 remove_literal_statements = minification_args .remove_literal_statements ,
247294 hoist_literals = minification_args .hoist_literals ,
248295 rename_locals = minification_args .rename_locals ,
0 commit comments