3
3
import argparse
4
4
import ast
5
5
import collections
6
- import difflib
7
6
import enum
8
7
import functools
9
8
import io
@@ -314,8 +313,6 @@ def remove_duplicated_imports(
314
313
315
314
def apply_import_sorting (
316
315
partitions : Iterable [CodePartition ],
317
- separate_relative : bool = False ,
318
- separate_from_import : bool = False ,
319
316
** sort_kwargs : Any ,
320
317
) -> list [CodePartition ]:
321
318
pre_import_code : list [CodePartition ] = []
@@ -344,51 +341,14 @@ def apply_import_sorting(
344
341
}
345
342
346
343
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
- )
363
344
364
345
sorted_blocks = sort (import_obj_to_partition .keys (), ** sort_kwargs )
365
346
for block in sorted_blocks :
366
- last_import_obj = None
367
-
368
347
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
+
392
352
# XXX: I want something like [x].join(...) (like str join) but for now
393
353
# this works
394
354
if new_imports :
@@ -404,7 +364,7 @@ def _import_type_switches(
404
364
else :
405
365
rest = []
406
366
407
- return pre_import_code + new_imports + relative_imports + rest
367
+ return pre_import_code + new_imports + rest
408
368
409
369
410
370
def _get_steps (
@@ -475,47 +435,22 @@ def _fix_file(filename: str, args: argparse.Namespace) -> int:
475
435
imports_to_add = args .add_import ,
476
436
imports_to_remove = args .remove_import ,
477
437
imports_to_replace = args .replace_import ,
478
- separate_relative = args .separate_relative ,
479
- separate_from_import = args .separate_from_import ,
480
438
application_directories = args .application_directories .split (':' ),
481
439
unclassifiable_application_modules = args .unclassifiable ,
482
440
)
483
441
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 = '' )
488
443
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 ())
498
447
499
448
if args .exit_zero_even_if_changed :
500
449
return 0
501
450
else :
502
451
return contents != new_contents
503
452
504
453
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
-
519
454
REMOVALS : dict [tuple [int , ...], set [str ]] = collections .defaultdict (set )
520
455
REPLACES : dict [tuple [int , ...], set [str ]] = collections .defaultdict (set )
521
456
@@ -806,19 +741,6 @@ def main(argv: Sequence[str] | None = None) -> int:
806
741
'filenames' , nargs = '*' ,
807
742
help = 'If `-` is given, reads from stdin and writes to stdout.' ,
808
743
)
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
- )
822
744
parser .add_argument ('--exit-zero-even-if-changed' , action = 'store_true' )
823
745
parser .add_argument (
824
746
'--add-import' , action = 'append' , default = [], type = _validate_import ,
@@ -860,39 +782,10 @@ def main(argv: Sequence[str] | None = None) -> int:
860
782
),
861
783
)
862
784
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
-
879
785
_add_version_options (parser )
880
786
881
787
args = parser .parse_args (argv )
882
788
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
-
896
789
for k , v in REMOVALS .items ():
897
790
if args .min_version >= k :
898
791
args .remove_import .extend (v )
0 commit comments