File tree Expand file tree Collapse file tree 5 files changed +26
-1
lines changed Expand file tree Collapse file tree 5 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -1116,7 +1116,15 @@ def _find_parent_path_names(self):
11161116
11171117 def _get_parent_path (self ):
11181118 parent_module_name , path_attr_name = self ._find_parent_path_names ()
1119- return getattr (sys .modules [parent_module_name ], path_attr_name )
1119+ try :
1120+ module = sys .modules [parent_module_name ]
1121+ except KeyError as e :
1122+ raise ModuleNotFoundError (
1123+ f"{ parent_module_name !r} must be imported before finding { self ._name !r} ." ,
1124+ name = parent_module_name ,
1125+ ) from e
1126+ else :
1127+ return getattr (module , path_attr_name )
11201128
11211129 def _recalculate (self ):
11221130 # If the parent's path has changed, recalculate _path
Original file line number Diff line number Diff line change 1+ This directory should not be a package, but should share a name with an
2+ unrelated subpackage.
Original file line number Diff line number Diff line change @@ -318,6 +318,17 @@ def test_module_before_namespace_package(self):
318318 self .assertEqual (a_test .attr , 'in module' )
319319
320320
321+ class NamespaceSubpackageSameName (NamespacePackageTest ):
322+ paths = ['' ]
323+
324+ def test_namespace_subpackage_shares_name_with_directory (self ):
325+ submodule_path = 'project4.foo'
326+ with self .assertRaises (ModuleNotFoundError ) as cm :
327+ importlib .machinery .PathFinder .find_spec (submodule_path )
328+
329+ self .assertEqual (cm .exception .name , 'project4' )
330+
331+
321332class ReloadTests (NamespacePackageTest ):
322333 paths = ['portion1' ]
323334
Original file line number Diff line number Diff line change @@ -2648,6 +2648,7 @@ TESTSUBDIRS= idlelib/idle_test \
26482648 test/test_importlib/namespace_pkgs \
26492649 test/test_importlib/namespace_pkgs/both_portions \
26502650 test/test_importlib/namespace_pkgs/both_portions/foo \
2651+ test/test_importlib/namespace_pkgs/foo \
26512652 test/test_importlib/namespace_pkgs/module_and_namespace_package \
26522653 test/test_importlib/namespace_pkgs/module_and_namespace_package/a_test \
26532654 test/test_importlib/namespace_pkgs/not_a_namespace_pkg \
Original file line number Diff line number Diff line change 1+ Reraise :exc: `KeyError ` as :exc: `ModuleNotFoundError ` when
2+ :meth: `importlib.machinery.PathFinder.find_spec ` is called on a submodule
3+ without importing the parent (and without a ``path `` argument).
You can’t perform that action at this time.
0 commit comments