-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add AssertImportHook.is_package() method #136
Conversation
Implementing .is_package() is optional according to PEP 302, but it makes several Python codes that assume import hook has this method (e.g. Flask) to work well with Attest.
I'll have a look over this. |
After reading PEP 302 (thanks for the link) it appears that your patch doesn't properly implement the spec. It says "All three methods should raise ImportError if the module wasn't found.". If you have a look at the existing code for
I think this pattern should be repeated for It would also seem that we should add support for |
Thanks for your review. |
From reading the PEP, that definitely seems to be the case. I wonder why it was originally written to return |
As I guessed from 5109379, this method was originally written for internal use, not for implementing the loader protocol, and it chose the name We can simply rename this method. Any suggestions for its new name? |
I think we could probably get away with just implementing the other methods ( |
Now get_source() method returns only a source string, and load_module() uses get_filename() instead.
Implemented |
@@ -300,19 +307,50 @@ def get_source(self, name): | |||
except KeyError: | |||
raise ImportError(name) | |||
|
|||
code = filename = newpath = None | |||
code = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing to code
and returning at the end, you could just return
from within each if
/elif
, would save a couple of lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I firstly wrote to return from within each case, but when I saw the original code it was written in such way (returning at th end), so I followed the way. Okay, I’ll change this anyway. Thank you!
Overall I really like this, my only criticism now is the lack of tests. I'm not sure the best strategy for testing it, any thoughts? |
Actually I have no idea how to test it correctly and well. |
try: | ||
(fd, fn, info), path = self._cache[name] | ||
except KeyError: | ||
return ImportError(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be raise
, not return
.
How should we handle this? How do we determine if a |
So where should we go from here? I haven't had a chance to test this with anything that's likely to expose problems (e.g. with Flask) and I'd like to do that before merging. Have you had any great ideas on how to automated test this? |
I wrote a small test for |
I resolve this problem by adding the method is_package to the AssertImportHook in the hook.py file, this is my snippet: def is_package(self, name):
try:
info = self._cache[name][0][2]
except KeyError:
raise ImportError(name)
return info[2] == imp.PKG_DIRECTORY It's work fine with flask, please add it to the next Attest version. |
…to, but at lease I can work around some test failures > AttributeError: AssertImportHook.is_package() method is missing but is required by Flask of PEP 302 import hooks. If you do not use import hooks and you encounter this error please file a bug against Flask. See also: pallets/flask#569 dag/attest#136
Although implementing
.is_package()
is optional according to PEP 302, it makes several Python codes that assume every import hook implements this method (e.g. Flask) to work well with Attest.