Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

jacobtylerwalls
Copy link
Contributor

@jacobtylerwalls jacobtylerwalls commented Oct 8, 2022

double backticks in news entry
@brettcannon brettcannon changed the title gh-93334: Fix homonym edge case in PathFinder.find_spec gh-93334: Fix homonym edge case in PathFinder.find_spec() Jul 10, 2024
Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code should raise ModuleNotFoundError and consider this bug being found due to an invalid call to find_spec when the parent package wasn't imported.

Lib/importlib/_bootstrap_external.py Outdated Show resolved Hide resolved
Lib/test/test_importlib/namespace_pkgs/homonym/README.md Outdated Show resolved Hide resolved
Lib/test/test_importlib/namespace_pkgs/project4/homonym.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Jul 10, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@jacobtylerwalls
Copy link
Contributor Author

I have made the requested changes; please review again

(happy to continue adjusting the test setup to be more minimal, if there's a way)

@bedevere-app
Copy link

bedevere-app bot commented Jul 26, 2024

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in getting to this review! Just a tightening of the test and then I think we are good!

module = sys.modules[parent_module_name]
except KeyError as e:
raise ModuleNotFoundError(
f"{parent_module_name} must be imported before finding {self._name}.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"{parent_module_name} must be imported before finding {self._name}.",
f"{parent_module_name!r} must be imported before finding {self._name!r}",

f"{parent_module_name} must be imported before finding {self._name}.",
name=parent_module_name,
) from e
return getattr(module, path_attr_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return getattr(module, path_attr_name)
else:
return getattr(module, path_attr_name)

Comment on lines 326 to 328
msg = 'project4 must be imported before finding project4.foo.'
with self.assertRaisesRegex(ModuleNotFoundError, msg):
importlib.machinery.PathFinder.find_spec(submodule_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being so specific on the exception message is very brittle, so we tend to avoid it. Instead, test any of the data attached to the exception as that's something that code might work with.

Suggested change
msg = 'project4 must be imported before finding project4.foo.'
with self.assertRaisesRegex(ModuleNotFoundError, msg):
importlib.machinery.PathFinder.find_spec(submodule_path)
with self.assertRaisesRegex(ModuleNotFoundError) as cm:
importlib.machinery.PathFinder.find_spec(submodule_path)
self.assertEqual(cm.excepion.name, 'project4')

@bedevere-app
Copy link

bedevere-app bot commented Sep 13, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@jacobtylerwalls
Copy link
Contributor Author

Thanks for the review! I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Sep 15, 2024

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PathFinder.find_spec() can raise bare KeyError when path=None
3 participants