@@ -708,19 +708,12 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
708708 return new_id
709709
710710 res : List [Tuple [int , str , int ]] = []
711- delayed_res : List [Tuple [int , str , int ]] = []
712711 for imp in file .imports :
713712 if not imp .is_unreachable :
714713 if isinstance (imp , Import ):
715714 pri = import_priority (imp , PRI_MED )
716715 ancestor_pri = import_priority (imp , PRI_LOW )
717716 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.
724717 res .append ((pri , id , imp .line ))
725718 ancestor_parts = id .split ("." )[:- 1 ]
726719 ancestors = []
@@ -729,15 +722,13 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
729722 res .append ((ancestor_pri , "." .join (ancestors ), imp .line ))
730723 elif isinstance (imp , ImportFrom ):
731724 cur_id = correct_rel_imp (imp )
732- any_are_submodules = False
733725 all_are_submodules = True
734726 # Also add any imported names that are submodules.
735727 pri = import_priority (imp , PRI_MED )
736728 for name , __ in imp .names :
737729 sub_id = cur_id + '.' + name
738730 if self .is_module (sub_id ):
739731 res .append ((pri , sub_id , imp .line ))
740- any_are_submodules = True
741732 else :
742733 all_are_submodules = False
743734 # Add cur_id as a dependency, even if all of the
@@ -747,19 +738,18 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
747738 # if all of the imports are submodules, do the import at a lower
748739 # priority.
749740 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 ))
758742 elif isinstance (imp , ImportAll ):
759743 pri = import_priority (imp , PRI_HIGH )
760744 res .append ((pri , correct_rel_imp (imp ), imp .line ))
761745
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 ("." ))
763753 return res
764754
765755 def is_module (self , id : str ) -> bool :
0 commit comments