@@ -708,19 +708,12 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
708
708
return new_id
709
709
710
710
res : List [Tuple [int , str , int ]] = []
711
- delayed_res : List [Tuple [int , str , int ]] = []
712
711
for imp in file .imports :
713
712
if not imp .is_unreachable :
714
713
if isinstance (imp , Import ):
715
714
pri = import_priority (imp , PRI_MED )
716
715
ancestor_pri = import_priority (imp , PRI_LOW )
717
716
for id , _ in imp .ids :
718
- # We append the target (e.g. foo.bar.baz) before the ancestors (e.g. foo
719
- # and foo.bar) so that, if FindModuleCache finds the target module in a
720
- # package marked with py.typed underneath a namespace package installed in
721
- # site-packages, (gasp), that cache's knowledge of the ancestors
722
- # (aka FindModuleCache.ns_ancestors) can be primed when it is asked to find
723
- # the parent.
724
717
res .append ((pri , id , imp .line ))
725
718
ancestor_parts = id .split ("." )[:- 1 ]
726
719
ancestors = []
@@ -729,15 +722,13 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
729
722
res .append ((ancestor_pri , "." .join (ancestors ), imp .line ))
730
723
elif isinstance (imp , ImportFrom ):
731
724
cur_id = correct_rel_imp (imp )
732
- any_are_submodules = False
733
725
all_are_submodules = True
734
726
# Also add any imported names that are submodules.
735
727
pri = import_priority (imp , PRI_MED )
736
728
for name , __ in imp .names :
737
729
sub_id = cur_id + '.' + name
738
730
if self .is_module (sub_id ):
739
731
res .append ((pri , sub_id , imp .line ))
740
- any_are_submodules = True
741
732
else :
742
733
all_are_submodules = False
743
734
# Add cur_id as a dependency, even if all of the
@@ -747,19 +738,18 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
747
738
# if all of the imports are submodules, do the import at a lower
748
739
# priority.
749
740
pri = import_priority (imp , PRI_HIGH if not all_are_submodules else PRI_LOW )
750
- # The imported module goes in after the submodules, for the same namespace
751
- # related reasons discussed in the Import case.
752
- # There is an additional twist: if none of the submodules exist,
753
- # we delay the import in case other imports of other submodules succeed.
754
- if any_are_submodules :
755
- res .append ((pri , cur_id , imp .line ))
756
- else :
757
- delayed_res .append ((pri , cur_id , imp .line ))
741
+ res .append ((pri , cur_id , imp .line ))
758
742
elif isinstance (imp , ImportAll ):
759
743
pri = import_priority (imp , PRI_HIGH )
760
744
res .append ((pri , correct_rel_imp (imp ), imp .line ))
761
745
762
- res .extend (delayed_res )
746
+ # Sort such that module (e.g. foo.bar.baz) comes before its ancestors (e.g. foo
747
+ # and foo.bar) so that, if FindModuleCache finds the target module in a
748
+ # package marked with py.typed underneath a namespace package installed in
749
+ # site-packages, (gasp), that cache's knowledge of the ancestors
750
+ # (aka FindModuleCache.ns_ancestors) can be primed when it is asked to find
751
+ # the parent.
752
+ res .sort (key = lambda x : - x [1 ].count ("." ))
763
753
return res
764
754
765
755
def is_module (self , id : str ) -> bool :
0 commit comments