classification
Title: Modify IMPORT_FROM to fallback on sys.modules
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Pascal.Chambon, barry, brett.cannon, eric.snow, inada.naoki, isoschiz, kristjan.jonsson, ncoghlan, pconnell, pitrou, pje, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-04-05 01:15 by brett.cannon, last changed 2014-12-13 10:05 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
circular_import_tests.diff brett.cannon, 2013-05-03 18:24
import_from_tests.diff brett.cannon, 2013-05-05 12:57
import_from_tests2.diff pitrou, 2014-10-04 21:36
import_from_tests2.diff pitrou, 2014-10-04 22:09
circrelimports.patch pitrou, 2014-10-04 22:10
Messages (34)
msg186059 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-04-05 01:15
To help with circular imports, IMPORT_FROM should be modified to check sys.modules if a getattr() check fails.

http://mail.python.org/pipermail/python-dev/2013-April/125123.html
msg186073 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-04-05 11:30
Like I mentioned on python-dev, it worries me a bit that this could be considered "unexpected", i.e. when there is a conflict between a legitimage attribute member of a module, and a submodule of the same name.

Also, I wonder if this isn't a bigger change to the import mechanism, than simply:
| Another would be
| to always require an 'as' clause in this case, so that you would have 
| to write'
|    import .foo as foo
which would possibly only require a change to the syntax.
msg186078 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-04-05 12:36
It won't conflict with attributes. Only if the attribute does not exist on the module already will it fall back to sys.modules. If the import finished then any attribute created from the import will already be there and thus not be an issue.

But to make sure this isn't a problem we can make sure there is a test in this instance.
msg186079 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-04-05 13:07
While I'm happy that this is being ackowledged as a problem, I'm not sure changing the "import x from y" semantics is necessarily a good idea.  I mean, it is obvious to me that it means "import x, then getattr(x, "y")".  I presume that is the meaning most people associate with it.  Certainly, "y" can be any old module attribute.

To change it to actually fall back to a submodule, well. I suppose if you could explain it roughly like "y = getattr(x, 'y', x.y)"  then it would be ok.

But I can think of contrived examples where this could break things:
#a.py:
from .b import util

#b.py
from . import a
from .util import util

Because of the circular import order, b.util will not exist as an attribute on b when a does its import.  So a.util will end up being util instead of util.util as one might expect.

I'm basically saying that it is possible that the fallback to submodule will occur, where the successful getattr would occur later and return something different than the submodule.  Possible. But perhaps very much an edge case :)
msg186080 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-04-05 13:52
By declaring what the semantics will be to make the case even possible we are not breaking anything but making something possible. IOW it isn't even an edge case to me since there is no working case to compare against. =)
msg186093 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-04-05 17:23
On Fri, Apr 5, 2013 at 9:07 AM, Kristján Valur Jónsson
<report@bugs.python.org> wrote:
> But I can think of contrived examples where this could break things:
> #a.py:
> from .b import util
>
> #b.py
> from . import a
> from .util import util
>
> Because of the circular import order, b.util will not exist as an attribute on b when a does its import.  So a.util will end up being util instead of util.util as one might expect.

Not quite.  It will only do this if the '.b.util' module is *in
sys.modules* at the time that a is being imported, which also means
that .b.util has to be imported somewhere *before* .a, in order to end
up with a stale value. As written, your example actually works
correctly if .a is imported first, and fails with an ImportError if .b
is imported first.

In fact, this example is kind of useful for proving the change
*correct*, not broken.   At the very least, it shows that you'll have
to be more inventive to come up with a breaking case.  ;-)

Consider that for any module x.y, x must be in sys.modules before x.y
can.  Therefore, any "from x import" taking place before x is fully
loaded will either happen before x.y is fully loaded, during the load,
or after it, and the following cases apply:

1. If it happens before, then it fails with an ImportError as is the case today.
2. If it happens during (i.e. there is a cycle with x.y rather than
with just x),
   then the import returns the module.
3. If it happens after, then either the x.y attribute is bound to the submodule,
   or has been rebound to something else, or deleted.
4. If after and deleted, the import returns the module.
5. If after and rebound, the import returns the changed attribute
(just like today)
6. If after and normally bound, the import returns the module (just like today)

The only cases in which the behavior changes from today are cases 2
and 4, which would both fail today with an ImportError.  Case 4 also
doesn't make much sense, since 'import x.y' would still permit access
to the module -- so it'd have to be an odd situation in which you
didn't want 'from import' (and *only* from import) to fail.

So let's consider case 2, which would have to be written something like:

#a.py
from .b import util

#b.py
from .util import util

#b/util.py
from .. import a
def util(): pass

#__main__.py
import b

So, import b leads to starting the load of b.util, which leads to
importing a, which succeeds and sees the b.util module instead of the
b.util:util function.

But, because of the circularity, this will *also* happen if you import
a first. So 'a' will *always* see b.util rather than b.util:util,
unless you remove the circularity.  If you remove the circularity,
then 'a' will always see b.util:util as the value of util.

So case 2 does not lead to a hard-to-debug ordering dependency, it
leads to an immediately discoverable change in behavior the moment you
start importing a from b.util.

The tl;dr version of the above is that you will only receive a module
instead of an attribute if the module that's about to be replaced just
imported *you* during its initial loading, and if it does that, it'll
do it no matter what order the imports occur in, making the problem
occur immediately and consistently as soon as you introduce the
circularity.
msg186095 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-04-05 17:31
Actually, after a little reflection, I can see that there are more
complex conditions to analyze, if 'b' doesn't import 'b.util', but
some other module imports b and sets b.util.  But that's just freaking
insane and whoever does that probably deserves whatever they get,
especially since a later 'import b.util' would blow away the modified
attribute.

So, this note is just to satisfy myself that the change doesn't
introduce any *new* weirdness unless you are in a case where the
parent imports and replaces the child, and the child is involved in an
import cycle.  If the parent doesn't import the child but just assigns
an attribute, it's already broken the minute somebody else imports the
child, and the same thing applies if something else sets the attribute
without importing the child first, and if it imports the child first,
then the previous analysis (mostly) applies, but either way that code
is broken and will have plenty of other ordering dependencies to debug
on its own.  ;-)

(No wonder Nick said nobody wanted to touch this...  the analysis
alone is a killer.  ;-)   Luckily, it seems we're good to go unless
somebody can find a case I missed.)
msg186096 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-04-05 17:49
...and I thought of *one* more way to trigger the changed behavior,
which looks like:

#b.py
from .b import util
import .a
util = util.util

#b/util.py
def util(): pass

(with the other files the same as before).

I'm including it only for completeness' sake, because my original
enumeration of cases ignored the possibility that 'a' could be doing
the import *after* b.util is loaded and bound, but *before* it's
rebound.  However, it doesn't produce any new or problematic effects:
it's essentially the same as if 'a' were imported from 'b.util'.  Once
again, regardless of the order in which imports happen, 'a' ends up
with 'b.util' the moment the circularity is introduced, and it stays
that way.

It's also hard to argue that a case written this way isn't getting
exactly what it *says* it wants.  In fact, it looks like code that was
deliberately written to *force* a to end up with the original b.util
instead of the replaced one.  ;-)

Final (hopefully) conclusion: this change replaces the FAQ of "Don't
use 'from-import' for circular imports" with the hopefully-less-FA'd Q
of "when from-import is part of an import cycle, it works *exactly*
like regular import, so you're going to get a submodule rather than an
attribute.  If you need the attribute instead, move the import so that
it happens after the attribute is set up."  (Which is exactly the same
advice that would apply in a cycle with any other unitialized
attribute, whether you were using from-import or not.)
msg186098 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-04-05 17:58
Wow, Good analysis Phillip.
So, we agree that the fallback still is a sensible fallback?  Then everything is fine and I'll put my official +1 stamp of approval on this.

Now... should we consider the current behavious to be a bug?  2.7 sure could do with some fixing :)
msg186102 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-04-05 19:16
I don't care much one way or the other about it being considered a bug
in 2.x, but it might be worth considering a bug in 3.x.

Either way, though, the language reference for "from import" should
reflect the change, and "alternative implementations" should be
informed of the update.
msg186103 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-04-05 19:47
It is not a bug but a change in semantics to accommodate a use-case so this will only be going into Python 3.4.
msg186887 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-04-14 07:51
Thanks for working through that Phillip - falling back to sys.modules when the expected attribute was missing is actually something I suggested as a possibility years ago, and Guido's response at the time was "If it was that easy, someone would have done it already".

Your analysis is one of the pieces that was missing, along with Brett's insight that the code that needs the fallback is the IMPORT_FROM bytecode rather than the import implementation.

I'm going to close the original circular import bug as being superseded by this one.
msg186922 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-04-14 14:34
On Sun, Apr 14, 2013 at 3:51 AM, Nick Coghlan <report@bugs.python.org> wrote:
> Your analysis is one of the pieces that was missing,

Unfortunately, I just noticed it's actually incorrect in a pretty
important part  In my original example, I said, "because of the
circularity, this will *also* happen if you import 'a' first."  This
statement is actually false.  Importing 'a' first in that example will
result in a.util == b.util:util, not a.util=b.util.  I made the
mistake because I was for some reason thinking that 'a' was going to
execute its import while being imported from b.util, and in that
scenario it will not.

That means there *is* an ordering dependency, and an ambiguity like
this one can lie dormant until long after you've introduced the
circularity.  :-(
msg188319 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-05-03 18:24
I have uploaded a patch with failing tests that should work after this is all said and done. Philip, please make sure I covered your tests as expected (I tweaked one because it already was working the way I did it). This way we at least know what we are aiming for in terms of results.
msg188417 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-05-04 22:49
It looks like maybe basic2 should be importing basic, not basic2.
Otherwise, you might as well just import basic2 directly and be done
with it.  ;-)  Likewise, I think indirect should be importing from
indirect2, not from itself?  i.e., I'd expect that test to fail even
with the change.  In addition, even if that is fixed, it's still not
testing a cycle involving util; it's really just testing the same
thing as "basic" is supposed to be testing.

It also looks as though like the rebinding test doesn't actually test
any rebinding, since nobody ever imports the thing that's rebound.
It's failing for the same reason as the subpackage test.  The
subpackage test looks like a valid test, though - i.e., it's the
"basic" case correctly implemented as a parent-child cycle.  It's
actually the only one of the tests that tests what it says it does.
msg188441 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-05-05 12:57
Don't be distracted when trying to write tests is the lesson learned. Fixed basic and rebinding and just deleted indirect.
msg190667 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-06-05 15:42
One question.
when doing "Programmatic" import, i.e. using the __import__ function, how do we do the same?

#module foo.B
A = __import__(".", fromlist=["A"]).A

#module foo.A
B = __import__(".", fromlist=["B"]).B

Clearly the above won't work.  Can we extend __import__ to allow a full path, including relative?  The objection about which name to bind to is no longer valid, since the binding is explicit.
So, could we do:

#module foo.B
A = __import__(".A")

#module foo.A
B = __import__(".B")


?
msg190670 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-06-05 15:49
sorry, the last example really needs to be:
#module foo.B
A = __import__(".A", fromlist=["dummy"])

to invoke the "return final module" behaviour.
Hm, maybe this simply works...  I didn't test....
Nope, I get
ValueError: Empty module name (in 2.7)
msg190671 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-05 15:57
You use importlib.import_module to do programmatic imports (and FYI dots never go into the first argument of __import__).
msg190673 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2013-06-05 16:07
Interesting.
And I can report that it works, in 2.7, with code such as
b = importlib.import_module("..b", __name__)
and
a = importlib.import_module("..a", __name__)

Still, it seems odd that a whole "importlib" is requried ot resolve the relative import, and that it doesn"t work with __import__, given how "supposedly" the import statement is supposed to translate to a __import__ call internally.  One would think that __import__ had access to the relative path machinery somehow.
msg190679 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-06-05 16:45
If you look at the importlib source code in 2.7 it's actually really small and simply translates the call into an __import__ call under the hood. So __import__ can completely handle relative imports, but you were not using the level argument to __import__ nor passing the necessary globals to make the relative name computation work (see the importlib source if you want to see how it works).
msg206434 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-12-17 13:52
With PEP 451 implemented, note that I have reopened issue 992389 - the patch to set the parent module attribute at the same time as setting the sys.module attribute is actually pretty trivial for the PEP 451 loader case, and that now includes all the standard loaders.

I also think that approach will have fewer weird edge cases than this version.
msg206476 - (view) Author: PJ Eby (pje) * (Python committer) Date: 2013-12-17 17:34
Unfortunately, this is not quite true: the weird edge cases for that approach are simply *different*, and harder to diagnose.  ;-)

The approach here can only affect execution paths that would currently raise ImportError; that one can break execution paths that are *currently working*.

As Brett said above, "By declaring what the semantics will be to make the case even possible we are not breaking anything but making something possible."  That is not the case for the approach proposed in the other issue.
msg228497 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-04 21:36
This is Brett's tests patch updated against default branch.
msg228501 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-04 22:09
And this is a full patch.
msg228502 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-04 22:11
(full patch is actually the latest upload: circrelimports.patch. Sorry for the glitch)
msg228822 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-08 23:10
By the way, is there any documentation that would need to be updated for this?
msg228833 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-10-09 06:23
The language reference - at least the section on the import statement, and potentially the section on the overall operation of the import system.

I don't *think* it would affect anywhere in the tutorial or the importlib docs, but they may be worth skimming to see if anything leaps out.
msg228915 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 21:21
By the way, my patch is currently using the package's __name__ attribute to build the fully-qualified name. Should it use the package's __package__ attribute instead?
msg228927 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-10-09 22:11
If I understand your question correctly, then yes, __package__ should be
used when converting explicit relative imports to absolute ones.

To write a test, run a submodule that needs the new fallback feature via
the -m switch (it will fail if using __name__)
msg228928 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-09 22:13
Le 10/10/2014 00:11, Nick Coghlan a écrit :
> 
> Nick Coghlan added the comment:
> 
> If I understand your question correctly, then yes, __package__ should be
> used when converting explicit relative imports to absolute ones.
> 
> To write a test, run a submodule that needs the new fallback feature via
> the -m switch (it will fail if using __name__)

I'm not sure it would. __package__ is not looked up on the module (since
precisely we failed importing it) but on the parent package (which is
passed to IMPORT_FROM).
msg229257 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-13 18:15
Actually, looking up __package__ would be wrong.

Say I have: from pack.module import foo

and "foo" doesn't exist in pack.module but exists in pack.
Since pack.module.__package__ == "pack", using __package__ would wrongly find the "foo" in pack.
msg229258 - (view) Author: Roundup Robot (python-dev) Date: 2014-10-13 18:21
New changeset fded07a2d616 by Antoine Pitrou in branch 'default':
Issue #17636: Circular imports involving relative imports are now supported.
https://hg.python.org/cpython/rev/fded07a2d616
msg232601 - (view) Author: Roundup Robot (python-dev) Date: 2014-12-13 10:05
New changeset 3a35638bce66 by Ned Deily in branch 'default':
Issue #17636: Install new test directories.
https://hg.python.org/cpython/rev/3a35638bce66
History
Date User Action Args
2014-12-13 10:05:40python-devsetmessages: + msg232601
2014-11-14 01:56:17ncoghlanlinkissue992389 superseder
2014-10-13 18:22:42pitrousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-10-13 18:21:34python-devsetnosy: + python-dev
messages: + msg229258
2014-10-13 18:15:32pitrousetmessages: + msg229257
2014-10-09 22:13:26pitrousetmessages: + msg228928
2014-10-09 22:11:46ncoghlansetmessages: + msg228927
2014-10-09 21:21:49pitrousetmessages: + msg228915
2014-10-09 06:23:10ncoghlansetmessages: + msg228833
2014-10-08 23:10:15pitrousetmessages: + msg228822
2014-10-05 12:38:31pitrousetnosy: + serhiy.storchaka
2014-10-04 22:11:34pitrousetmessages: + msg228502
2014-10-04 22:11:00pitrousetfiles: + circrelimports.patch
2014-10-04 22:09:38pitrousetfiles: + import_from_tests2.diff

messages: + msg228501
stage: needs patch -> patch review
2014-10-04 21:36:57pitrousetfiles: + import_from_tests2.diff
nosy: + pitrou
messages: + msg228497

2014-10-04 21:27:52pitrousetstage: test needed -> needs patch
versions: + Python 3.5, - Python 3.4
2013-12-17 17:34:54pjesetmessages: + msg206476
2013-12-17 13:52:41ncoghlansetmessages: + msg206434
2013-12-17 13:47:35ncoghlanunlinkissue992389 superseder
2013-06-05 16:45:20brett.cannonsetmessages: + msg190679
2013-06-05 16:07:26kristjan.jonssonsetmessages: + msg190673
2013-06-05 15:57:03brett.cannonsetmessages: + msg190671
2013-06-05 15:49:57kristjan.jonssonsetmessages: + msg190670
2013-06-05 15:42:30kristjan.jonssonsetmessages: + msg190667
2013-05-05 12:57:54brett.cannonsetfiles: + import_from_tests.diff

messages: + msg188441
2013-05-04 22:49:25pjesetmessages: + msg188417
2013-05-03 18:24:05brett.cannonsetfiles: + circular_import_tests.diff
keywords: + patch
messages: + msg188319
2013-04-15 12:42:10Pascal.Chambonsetnosy: + Pascal.Chambon
2013-04-14 14:34:58pjesetmessages: + msg186922
2013-04-14 07:59:21ncoghlanlinkissue992389 superseder
2013-04-14 07:51:52ncoghlansetnosy: + ncoghlan
messages: + msg186887
2013-04-13 17:00:57brett.cannonlinkissue17716 dependencies
2013-04-09 19:01:11isoschizsetnosy: + isoschiz
2013-04-05 19:47:50brett.cannonsetmessages: + msg186103
2013-04-05 19:16:32pjesetmessages: + msg186102
2013-04-05 17:58:30kristjan.jonssonsetmessages: + msg186098
2013-04-05 17:49:24pjesetmessages: + msg186096
2013-04-05 17:31:18pjesetmessages: + msg186095
2013-04-05 17:23:41pjesetmessages: + msg186093
2013-04-05 14:22:27barrysetnosy: + barry
2013-04-05 13:52:22brett.cannonsetmessages: + msg186080
2013-04-05 13:07:42kristjan.jonssonsetmessages: + msg186079
2013-04-05 12:36:06brett.cannonsetmessages: + msg186078
2013-04-05 11:30:23kristjan.jonssonsetnosy: + kristjan.jonsson
messages: + msg186073
2013-04-05 06:14:09pconnellsetnosy: + pconnell
2013-04-05 03:44:21eric.snowsetnosy: + eric.snow
2013-04-05 01:31:21inada.naokisetnosy: + inada.naoki
2013-04-05 01:15:02brett.cannoncreate