classification
Title: Importing the main module twice leads to two incompatible instances
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Kwpolska, SylvieLorxu, abarry, cameron, martin.panter, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2016-07-24 15:46 by SylvieLorxu, last changed 2016-08-20 07:03 by ethan.furman.

Files
File name Uploaded Description Edit
issue27607.zip SylvieLorxu, 2016-07-25 10:58 Example test case
Messages (14)
msg271164 - (view) Author: Sylvia van Os (SylvieLorxu) Date: 2016-07-24 15:46
If main imports another file, and this file imports an Enum class from main, isinstance will return false for Enum variables set in this file, causing the Enums to never match.

A proof of concept is added as a zip. I thank Kwpolska for simplifying my initial proof of concept.

To test this, unzip both files into the same directory and launch main.py with Python 3. I have succesfully been able to reproduce this issue on Python 3.4.3.

From discussion on IRC with Vgr, PEP 499 may be related to this issue: https://www.python.org/dev/peps/pep-0499/.
msg271184 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-24 23:58
This is a consequence of the way __main__ works.  You actually have two separate instances of the Enum class.  If there is a fix for this it would be that PEP, so I'm not sure there is any  point in keeping this issue open.
msg271185 - (view) Author: Sylvia van Os (SylvieLorxu) Date: 2016-07-25 00:02
For what it's worth, I am not opposed to having this issue closed. As I am new to reporting anything on the Python issue tracker and was uncertain on how relevant the PEP in question was, I figured it would be better to report this anyway just in case.
msg271188 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-25 00:09
Certainly reporting it is fine, if nothing else it gives a record for someone else searching for the same problem.  I'm not certain why Raymond left the issue open, so I'm not closing it myself :)
msg271189 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-07-25 00:16
I left it open so that Ethan would get a chance to see anything related to Enum.
msg271195 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-07-25 00:49
I would expect isintance() to fail like this for any class, not just Enum. I haven’t looked in your zip file, but a workaround may be to import the Enum class from a third module into both main.py and the other file. The third module should only get executed once, thus only defining one copy of the Enum class.
msg271196 - (view) Author: Sylvia van Os (SylvieLorxu) Date: 2016-07-25 00:51
Importing it from a third module indeed works without issues and makes both isinstance() calls return True, yes.
msg271210 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-07-25 02:23
Sylvia, if you have any control over the name of your zip files, please use something shorter.  ;)

The files inside are a 'main.py' and a 'module.py' -- notice there is no '__main__.py'.  So it can't be executed as 'python3 -m blahblah'.

I tried renaming 'main.py' to '__main__.py' but then I get either

  ImportError: cannot import name 'Module'

or

  ImportError: No module named 'module'

I'm happy to check for strange Enum interactions, but I need a working test case first.
msg271211 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-07-25 02:25
Oh, forgot to mention I also tried testing as just 'main.py' from inside the test directory (that's what accounted for one of the two error messages).
msg271217 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-07-25 02:56
I'm fairly sure enums aren't related to this issue, so I'm unassigning Ethan (but feel free to self-assign back if you so desire :). As I said on IRC (I'm Vgr), I think that importing a module twice is the problem here, and that PEP 499 is the way to properly solve this and related issues. Thanks for opening this issue!

I added Cameron Simpson (PEP 499's author) to the nosy list, hoping he still wants to work on this (and let us know otherwise). I would personally like to see the PEP accepted and implemented for 3.6.
msg271243 - (view) Author: Cameron Simpson (cameron) * Date: 2016-07-25 09:05
On 25Jul2016 02:56, Python Bug Reports <report@bugs.python.org> wrote:
>
>Emanuel Barry added the comment:
>
>I'm fairly sure enums aren't related to this issue, so I'm unassigning Ethan (but feel free to self-assign back if you so desire :). As I said on IRC (I'm Vgr), I think that importing a module twice is the problem here, and that PEP 499 is the way to properly solve this and related issues. Thanks for opening this issue!
>
>I added Cameron Simpson (PEP 499's author) to the nosy list, hoping he still 
>wants to work on this (and let us know otherwise). I would personally like to 
>see the PEP accepted and implemented for 3.6.

Thank you; I do want to finish off PEP 499 and get it accepted. I have been 
very remiss in pursuing that.

What can I do to assist here?

Cheers,
Cameron Simpson <cs@zip.com.au>
msg271248 - (view) Author: Sylvia van Os (SylvieLorxu) Date: 2016-07-25 10:58
My apologies, I can't get that test case to run here either. Here is a slightly older one that does reproduce the issue too.

I specifically run it with python3 main.py.
msg271253 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-07-25 12:16
@Cameron - I don't think this issue can be solved without the PEP, so possibly that the best move would be to work on the PEP and submit it for another round on Python-Dev, to get it reviewed. Thanks!
msg271264 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-25 13:06
If you have any sort of implementation of the pep, proving that it does fix this issue would be informative.
History
Date User Action Args
2016-08-20 07:03:35ethan.furmansetnosy: - ethan.furman
2016-07-25 13:06:57r.david.murraysetmessages: + msg271264
2016-07-25 12:16:42abarrysetmessages: + msg271253
2016-07-25 10:58:12SylvieLorxusetfiles: + issue27607.zip

messages: + msg271248
2016-07-25 10:56:40SylvieLorxusetfiles: - a040de8b978dcc4c272c0571d456a382-ea6d8fb8acc988a1f060c94070f70a6ed8439069.zip
2016-07-25 09:05:40cameronsetmessages: + msg271243
2016-07-25 02:56:20abarrysettitle: Enums never match if imported from another file -> Importing the main module twice leads to two incompatible instances
nosy: + abarry, cameron

messages: + msg271217

assignee: ethan.furman ->
versions: + Python 3.6, - Python 3.4
2016-07-25 02:25:25ethan.furmansetmessages: + msg271211
2016-07-25 02:23:55ethan.furmansetmessages: + msg271210
2016-07-25 00:51:16SylvieLorxusetmessages: + msg271196
2016-07-25 00:49:50martin.pantersetnosy: + martin.panter
messages: + msg271195
2016-07-25 00:16:07rhettingersetnosy: + rhettinger
messages: + msg271189
2016-07-25 00:09:21r.david.murraysetmessages: + msg271188
2016-07-25 00:02:45SylvieLorxusetmessages: + msg271185
2016-07-24 23:58:37r.david.murraysetnosy: + r.david.murray
messages: + msg271184
2016-07-24 16:28:57rhettingersetassignee: ethan.furman
2016-07-24 16:27:59brett.cannonsetnosy: + ethan.furman
2016-07-24 15:46:17SylvieLorxucreate