-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Get correct module info for zipimport modules (to then correctly classify them). #47
Conversation
Looks like the CI is running back on Python 2.7 as well. I likely did something in the tests that isn't Python2.7 compatible (likely assuming path-likes). I'll see if I can decipher the CI return before trying to get a Python2.7 environment setup locally. |
There we go; CI looks happy now. How's this patch @asottile ? I tried to keep it small and to the point. I left the commit history above so you could see the TDD evidence. Happy to squash if you'd prefer. I also left you push rights as well to the branch I believe. Thanks for considering (and for making a very useful tool). |
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.
you might get better mileage if you run reorder-python-imports
without access to your site-packages?
Well that is annoying. Coverage complains about skipping that assert for Python 2.7. I've got about 15 min left to try and get a Python 2.7 environment up to see about making that test pass for Python 2.7. I'll see what I can do. |
I got the Python2.7 environment stood up, but I was having some trouble even getting into a debugger with the While editing, I noticed how you did Python version-specific coverage disables elsewhere, so I went ahead and just used one of those in the tests file. CI is happy now, but I understand you may not be. I've got to go do my day job now, but happy to discuss further or try other things if you like. |
just because I'm curious (and this patch has gotten way complicated) -- what happens if you invoke reorder-python-imports without |
Well, it would be an un-importable module, so I believe this would be the result:
I think you'd always want things importable in a |
And as for the complexity of this pull request, I can pull back out the embedded code path (and tests) to keep it simpler if you prefer. |
actually it's designed for the opposite, pre-commit creates isolated environments :) |
Fair enough on pre-commit, although I am a bit confused on how you would do a In our own Separate from I still think having |
Thanks for that context; I definitely missed those hints in my read-thrus thus far. We've been using various CI checks internally for years, and we're just easing into some So what additional information can I provide to help you evaluate this proposed change? Happy to just give you some space and let you think about it for a day or two. |
Also, after a bit more thought, I think this issue could be viewed as an accidental dynamic inspection bleed into what is meant to be a static inspection tool. |
yeah the main thing is it's not specific to zipimports -- any special importer / $ mkdir -p x && touch x/t.py
$ python3 -i aspy/refactor_imports/classify.py
>>> import sys
>>> sys.path.append('x')
>>> classify_import('t')
'BUILTIN' |
Well, in our case, it's specific to |
hmmm, is there an open source reproduction I can look at, I'd be interested in the not-PYTHONPATH case since it should be |
So when you say "not-PYTHONPATH" case, you mean an example of doing In the meantime, here's a First, before setting PythonPath:
After setting PythonPath:
It identifies |
Here's the PythonPath example with https://github.com/aio-libs/yarl (import failed due to
|
And here's showing that modifying
|
I'll deconstruct the above scenarios a bit more in another comment or two and call it a night then. |
Alright, one step up the chain, here are the outputs of
Actually, other than some path formatting the |
Alright, so there is an explicit After digging through all this, I still think it's a reasonable change to improve the detection of Is there any further information I can provide or tests/scenarios you'd like me to explore? |
hmmmm! that's a good point -- maybe if we return |
I think that's a reasonable solution. I'll go ahead and add an "embedded test" that puts a zipimport on sys.path without putting it on pythonpath. It should return BUILTIN in that scenario. Lemme see what I can do here. |
Alright, I believe I got that switch made. I did enough research I believe I can do the zipimport detection in the python2.7 version of |
Here's a stackoverflow post with the general pattern for I got it working in a REPL. I'll see about getting it into |
would it make it easier if I droped py27? I was planning to do that in the next few weeks anyway |
Hah, well, I just spent the hour to wrangle Python 2.7, so it's a moot point to me now. But for future devs sake I'd suggest dropping it. The difference in the import engines between 2.7 and 3.5+ is indeed jarring. Let's see if CI is happy with the commit I just pushed. |
Alright, CI seems happy. There's parity between Python 2.7 and 3 (although it took far more code lines for Python 2.7). The change is now limited to detecting I personally think this is good to go again. Let me know if you have any more requests/comments/concerns. |
aspy/refactor_imports/classify.py
Outdated
pass | ||
# Sweep for zipimports on sys.path | ||
for path_entry in sys.path: | ||
path_entry_ext = os.path.splitext(path_entry)[-1].lower() |
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.
I just noticed that zipimport.zipimporter
would likely validate that it's a zip file, so we could switch this to "Easier to Ask Forgiveness". Although, for performance reasons, likely faster to check the string suffix since I'm assuming zipimport.zipimporter
will do some file I/O before failing. Let me know if you'd like to remove this to be more pythonic though.
I'll rebase this for ya since #48 is going to conflict -- sorry about that |
Thanks. |
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.
Your rebase and cleanup looks nice and clean. Thanks for being patient with me as well. |
Do this because there are scenarios where zipped up packages end up being imported, and the import classification logic was marking those as part of the standard library.
Trying to do full TDD. So opening with just a commit to add tests to hopefully get a nice CI failure first.
Resolves #46