Open a87580a0-fa61-4695-a31b-f532ecf7185b opened 8 years ago
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/.
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.
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.
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 :)
I left it open so that Ethan would get a chance to see anything related to Enum.
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.
Importing it from a third module indeed works without issues and makes both isinstance() calls return True, yes.
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.
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).
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.
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\
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.
@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!
If you have any sort of implementation of the pep, proving that it does fix this issue would be informative.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at = None created_at =
labels = ['interpreter-core', 'type-bug', '3.11']
title = 'Importing the main module twice leads to two incompatible instances'
updated_at =
user = 'https://github.com/TheLastProject'
```
bugs.python.org fields:
```python
activity =
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation =
creator = 'SylvieLorxu'
dependencies = []
files = ['43877']
hgrepos = []
issue_num = 27607
keywords = []
message_count = 14.0
messages = ['271164', '271184', '271185', '271188', '271189', '271195', '271196', '271210', '271211', '271217', '271243', '271248', '271253', '271264']
nosy_count = 7.0
nosy_names = ['rhettinger', 'cameron', 'r.david.murray', 'martin.panter', 'abarry', 'SylvieLorxu', 'Kwpolska']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue27607'
versions = ['Python 3.11']
```