33import argparse
44import ast
55import collections
6- import difflib
76import enum
87import functools
98import io
@@ -314,8 +313,6 @@ def remove_duplicated_imports(
314313
315314def apply_import_sorting (
316315 partitions : Iterable [CodePartition ],
317- separate_relative : bool = False ,
318- separate_from_import : bool = False ,
319316 ** sort_kwargs : Any ,
320317) -> list [CodePartition ]:
321318 pre_import_code : list [CodePartition ] = []
@@ -344,51 +341,14 @@ def apply_import_sorting(
344341 }
345342
346343 new_imports = []
347- relative_imports = []
348-
349- def _import_type_switches (
350- last_import_obj : AbstractImportObj | None ,
351- import_obj : AbstractImportObj ,
352- ) -> bool :
353- """Returns True if separate_from_import is True and `import_obj` is
354- :class:`aspy.refactor_imports.import_obj.FromImport`
355- and ``last_import_obj`` is
356- :class:`aspy.refactor_imports.import_obj.ImportImport`
357- """
358- return (
359- separate_from_import and
360- last_import_obj is not None and
361- type (last_import_obj ) is not type (import_obj )
362- )
363344
364345 sorted_blocks = sort (import_obj_to_partition .keys (), ** sort_kwargs )
365346 for block in sorted_blocks :
366- last_import_obj = None
367-
368347 for import_obj in block :
369- if separate_relative and import_obj .is_explicit_relative :
370- relative_imports .append (import_obj_to_partition [import_obj ])
371- else :
372- if _import_type_switches (last_import_obj , import_obj ):
373- new_imports .append (CodePartition (CodeType .NON_CODE , '\n ' ))
374-
375- last_import_obj = import_obj
376- new_imports .append (import_obj_to_partition [import_obj ])
377-
378- # There's an edge case if both --separate-relative and
379- # --separate-from-import are passed where the first-party imports
380- # will *all* be explicit relative imports and sorted into the special
381- # block. In this case, we don't want the first-party block to just
382- # be a single newline. See #23
383- if last_import_obj is not None :
384- new_imports .append (CodePartition (CodeType .NON_CODE , '\n ' ))
385-
386- # There is another edge case if --separate-relative is passed while all the
387- # imports are relative. In that case we don't want an empty new line at the
388- # beginning of the block. We should insert the new line only if there are
389- # additional imports. See #134
390- if relative_imports and len (relative_imports ) != len (imports ):
391- relative_imports .insert (0 , CodePartition (CodeType .NON_CODE , '\n ' ))
348+ new_imports .append (import_obj_to_partition [import_obj ])
349+
350+ new_imports .append (CodePartition (CodeType .NON_CODE , '\n ' ))
351+
392352 # XXX: I want something like [x].join(...) (like str join) but for now
393353 # this works
394354 if new_imports :
@@ -404,7 +364,7 @@ def _import_type_switches(
404364 else :
405365 rest = []
406366
407- return pre_import_code + new_imports + relative_imports + rest
367+ return pre_import_code + new_imports + rest
408368
409369
410370def _get_steps (
@@ -475,47 +435,22 @@ def _fix_file(filename: str, args: argparse.Namespace) -> int:
475435 imports_to_add = args .add_import ,
476436 imports_to_remove = args .remove_import ,
477437 imports_to_replace = args .replace_import ,
478- separate_relative = args .separate_relative ,
479- separate_from_import = args .separate_from_import ,
480438 application_directories = args .application_directories .split (':' ),
481439 unclassifiable_application_modules = args .unclassifiable ,
482440 )
483441 if filename == '-' :
484- if args .diff_only :
485- _report_diff (contents , new_contents , '' )
486- else :
487- print (new_contents , end = '' )
442+ print (new_contents , end = '' )
488443 elif contents != new_contents :
489- if args .diff_only :
490- _report_diff (contents , new_contents , filename )
491- elif args .print_only :
492- print (f'==> { filename } <==' , file = sys .stderr )
493- print (new_contents , end = '' )
494- else :
495- print (f'Reordering imports in { filename } ' , file = sys .stderr )
496- with open (filename , 'wb' ) as f :
497- f .write (new_contents .encode ())
444+ print (f'Reordering imports in { filename } ' , file = sys .stderr )
445+ with open (filename , 'wb' ) as f :
446+ f .write (new_contents .encode ())
498447
499448 if args .exit_zero_even_if_changed :
500449 return 0
501450 else :
502451 return contents != new_contents
503452
504453
505- def _report_diff (contents : str , new_contents : str , filename : str ) -> None :
506- diff = '' .join (
507- difflib .unified_diff (
508- io .StringIO (contents ).readlines (),
509- io .StringIO (new_contents ).readlines (),
510- fromfile = filename , tofile = filename ,
511- ),
512- )
513- if diff and not diff .endswith ('\n ' ):
514- diff += '\n \\ No newline at end of file\n '
515-
516- print (diff , end = '' )
517-
518-
519454REMOVALS : dict [tuple [int , ...], set [str ]] = collections .defaultdict (set )
520455REPLACES : dict [tuple [int , ...], set [str ]] = collections .defaultdict (set )
521456
@@ -806,19 +741,6 @@ def main(argv: Sequence[str] | None = None) -> int:
806741 'filenames' , nargs = '*' ,
807742 help = 'If `-` is given, reads from stdin and writes to stdout.' ,
808743 )
809- group = parser .add_mutually_exclusive_group (required = False )
810- group .add_argument (
811- '--diff-only' , action = 'store_true' ,
812- help = '(Deprecated) Show unified diff instead of applying reordering.' ,
813- )
814- group .add_argument (
815- '--print-only' , action = 'store_true' ,
816- help = (
817- '(Deprecated) '
818- 'Print the output of a single file after reordering. '
819- 'Consider using `-` for the filename instead.'
820- ),
821- )
822744 parser .add_argument ('--exit-zero-even-if-changed' , action = 'store_true' )
823745 parser .add_argument (
824746 '--add-import' , action = 'append' , default = [], type = _validate_import ,
@@ -860,39 +782,10 @@ def main(argv: Sequence[str] | None = None) -> int:
860782 ),
861783 )
862784
863- parser .add_argument (
864- '--separate-relative' , action = 'store_true' ,
865- help = (
866- '(Deprecated) Separate explicit relative (`from . import ...`) '
867- 'imports into a separate block.'
868- ),
869- )
870-
871- parser .add_argument (
872- '--separate-from-import' , action = 'store_true' ,
873- help = (
874- '(Deprecated) Separate `from xx import xx` imports from '
875- '`import xx` imports with a new line.'
876- ),
877- )
878-
879785 _add_version_options (parser )
880786
881787 args = parser .parse_args (argv )
882788
883- for option in (
884- 'diff_only' ,
885- 'print_only' ,
886- 'separate_relative' ,
887- 'separate_from_import' ,
888- ):
889- if getattr (args , option ):
890- print (
891- f'warning: --{ option .replace ("_" , "-" )} is deprecated '
892- f'and will be removed' ,
893- file = sys .stderr ,
894- )
895-
896789 for k , v in REMOVALS .items ():
897790 if args .min_version >= k :
898791 args .remove_import .extend (v )
0 commit comments