Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Classes from Modules Not Loading Correctly #130

Open
cclamb opened this issue Feb 1, 2017 · 7 comments
Open

Classes from Modules Not Loading Correctly #130

cclamb opened this issue Feb 1, 2017 · 7 comments

Comments

@cclamb
Copy link

cclamb commented Feb 1, 2017

tl;dr: from import causes a TypeError when classes are autoreloaded

Okay, so whenever I try to use 'from import ' notation in a notebook I get this error:

TypeError: super(type, obj): obj must be an instance or subtype of type

Now, interestingly, when I attempt to re-execute the reloaded file, isinstance(self, ) returns false, causing this error.

I've been able to recreate this in a couple of ways. The first way involves using a package and importing classes from that package.

I've attached three testcases to this issue. The first, class-test-works.zip, shows an example that works in a notebook. You'll note that I'm importing from a file (package.py) and I'm using full package import notation (i.e. 'import package as p'). Autoreloading works here - make changes to the package.py file, and re-run the second cell. Everything is fine.

The second, class-test-fail-I.zip, uses python directory modules with an init.py in the package directory. Here, the init.py file imports the public classes via class-specific notation (i.e. 'from package import A, B, C'). Run the packaged notebook. The first time, everything is fine. Now make a change to the package/package.py file, and save it. Then re-run the second cell. You'll get a TypeError on the call to super(...) in the emit(.) method in B.

The third, clas-test-fail-II.zip, uses a file instead of a directory, and loads the file into the notebook with class-specific notation (i.e. 'from package import B'). Repeat the steps from above - load and run, make a change to package.py and save, then rerun the second cell. You'll see the TypeError failure.

class-test-works.zip
class-test-fail-II.zip
class-test-fail-I.zip

@takluyver
Copy link
Member

Just tested with class-test-fail-II, and it worked for me with both Python 3 and 2.

Autoreload is part of IPython; what version of IPython do you have? If it's not the latest, try upgrading.

@cclamb
Copy link
Author

cclamb commented Feb 1, 2017 via email

@cclamb
Copy link
Author

cclamb commented Feb 1, 2017

Sorry, I didn't save the project file prior to zipping with class-test-fail-II. I have updated ipython and can still recreate the error with this case (see new attached zip).

The notebook should have this code in it:

(cell 1)
from package import B

(cell 2)
b = B()
b.emit()

sorry for screwing up the first submission there. Also, this does fail in ipython too; should I submit the report there?

class-test-fail-II.zip

@takluyver
Copy link
Member

Aha, yes, I can reproduce it too now. I should have compared the issue description with the sample notebook. It will be in IPython, because that's where the autoreload code is, and it will be that that's going wrong.

@takluyver
Copy link
Member

I think I see roughly what's happening: the classes inside the module are being updated when it is reloaded, but the class B that you brought into the main namespace is still a reference to the old class B before the module was reloaded.

To be honest, when I look at autoreload, I'm amazed that it works as well as it does.

@cclamb
Copy link
Author

cclamb commented Feb 1, 2017 via email

@takluyver
Copy link
Member

The IPython folks look a lot like us. ;-) There's a good chance it's me debugging it either way.

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

No branches or pull requests

2 participants