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
rewrite of as imports changes the behavior of the imports #1280
Comments
We're also seeing this issue on our code base. Thanks for the great rework! |
I would find a setting to disable the rewrite very helpful. |
Related to this, the rewrite can be made a bit smarter: it currently rewrites |
On Wed, 8 Jul 2020 at 12:15, Yuxin Wu ***@***.***> wrote:
Related to this, the rewrite can be made a bit smarter: it currently
rewrites import xx.yy as yy into from xx import yy as yy, instead of from
xx import yy.
That's #1281
|
@keewis the example you gave is actually a bug (or at least should be considered a bug not a feature). I recently was made aware of Traps for the Unwary in Python’s Import System, but I personally discovered the issue you are presenting about a year ago or maybe more ago and I had to fix my code. Given the tree you have: mkdir test
touch test/module.py
echo 'def module(): pass' > test/__init__.py While the examples you have do show that there's a name collision and you can pick one or the other by importing different ways, you're actually falling into The submodules are added to the package namespace trap (second example) Using your tree you will see: >>> import test
>>> test
<module 'test' from '.../test/__init__.py'>
>>> test.module
<function module at 0x7fabca5c8820>
>>> import test.module as module
>>> test.module
<module 'test.module' from '.../test/module.py'> As you will notice, even though you performed the right import you have now shadowed |
For use cases: I can imagine someone using this to provide a computationally less expensive but also less useful version of a module. I agree that this should be avoided as much as possible, and that's why I stated that this might indicate poor design. This issue is not so much that I want to use that behavior, but rather that I don't know (or usually can't control) what my dependencies might be doing. That's why I'd like to have a setting to control the rewrite. All technical issues aside, |
A fix for this has been deployed to PyPI in the 5.1.0 release, Thanks everyone for reporting! ~Timothy |
#1107 introduced a rewrite of
import test.module as m
styled imports to something likefrom test import module as m
.These changes are actually not equivalent (even though the name clash probably indicates poor design): using the standard import machinery,
import test.module
always tries to import the moduletest/module.py
from somewhere onsys.path
whilefrom test import module
will take anything it can get.Consider a package like
with that, we can see the difference:
which makes me a bit nervous since I usually don't look that deep into the code of dependencies.
Is there a setting to disable the rewrite?
The text was updated successfully, but these errors were encountered: