gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100)

This commit is contained in:
Jacob Walls 2025-09-05 18:41:01 -04:00 committed by GitHub
parent 8368895049
commit 92b2a8a04d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 1 deletions

View file

@ -1116,7 +1116,15 @@ def _find_parent_path_names(self):
def _get_parent_path(self):
parent_module_name, path_attr_name = self._find_parent_path_names()
return getattr(sys.modules[parent_module_name], path_attr_name)
try:
module = sys.modules[parent_module_name]
except KeyError as e:
raise ModuleNotFoundError(
f"{parent_module_name!r} must be imported before finding {self._name!r}.",
name=parent_module_name,
) from e
else:
return getattr(module, path_attr_name)
def _recalculate(self):
# If the parent's path has changed, recalculate _path