@@ -736,14 +736,6 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
736
736
pri = import_priority (imp , PRI_MED )
737
737
ancestor_pri = import_priority (imp , PRI_LOW )
738
738
for id , _ in imp .ids :
739
- # We append the target (e.g. foo.bar.baz)
740
- # before the ancestors (e.g. foo and foo.bar)
741
- # so that, if FindModuleCache finds the target
742
- # module in a package marked with py.typed
743
- # underneath a namespace package installed in
744
- # site-packages, (gasp), that cache's
745
- # knowledge of the ancestors can be primed
746
- # when it is asked to find the target.
747
739
res .append ((pri , id , imp .line ))
748
740
ancestor_parts = id .split ("." )[:- 1 ]
749
741
ancestors = []
@@ -768,14 +760,17 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
768
760
# if all of the imports are submodules, do the import at a lower
769
761
# priority.
770
762
pri = import_priority (imp , PRI_HIGH if not all_are_submodules else PRI_LOW )
771
- # The imported module goes in after the
772
- # submodules, for the same namespace related
773
- # reasons discussed in the Import case.
774
763
res .append ((pri , cur_id , imp .line ))
775
764
elif isinstance (imp , ImportAll ):
776
765
pri = import_priority (imp , PRI_HIGH )
777
766
res .append ((pri , correct_rel_imp (imp ), imp .line ))
778
767
768
+ # Ensure that foo.bar.baz is always before foo.bar and foo
769
+ # This ensures that if FindModuleCache finds a module in a package marked with py.typed
770
+ # underneath a namespace package installed in site-packages, (gasp), that cache's knowledge
771
+ # of the ancestors (FindModuleCache.ns_ancestors) is primed when it is asked to find the
772
+ # parent.
773
+ res .sort (key = lambda x : - len (x [1 ]))
779
774
return res
780
775
781
776
def is_module (self , id : str ) -> bool :
0 commit comments