[issue44922] isinstance breaks on imported dataclasses

2021-08-16 Thread Eric V. Smith


Eric V. Smith  added the comment:

You're importing main.py twice. The first is when you run "python main.py", 
where it has the module name '__main__'. The second time is in codegen.py, 
where it has the name 'main'. You create the AtomX instance as __main__.AtomX, 
but the isinstance check is against main.AtomX. Because the objects have 
different modules, they're not equal.

You can see this if you change the isinstance check to:

=
from main import AtomX
import sys

def inheritance_map(candidate):
assert isinstance(candidate, sys.modules['__main__'].AtomX)
==

Another way to see this if you add a print statement to the original codegen.py:

==
from main import AtomX

def inheritance_map(candidate):
print(f'{candidate=} {type(candidate)=} {AtomX=}')
assert isinstance(candidate, AtomX)
==

Which will print:
candidate=AtomX(my_symbol='qwerty', quantity='') type(candidate)= AtomX=
Notice the types refer to different modules, so they are distinct and unequal.

The easiest way around this is to not do any work in main.py, but instead 
create another module, import it from main.py, and do the work there.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44922] isinstance breaks on imported dataclasses

2021-08-15 Thread Oleg Baskakov


Oleg Baskakov  added the comment:

Sorry, I forgot to add if __name__ line:
```
import codegen
from dataclasses import dataclass

@dataclass
class AtomX:
my_symbol: str
quantity: str = ""


if __name__ == "__main__":
codegen.inheritance_map(AtomX("qwerty"))

```

So the output:
```
Traceback (most recent call last):
  File "/Users/baskakov/PycharmProjects/main.py", line 11, in 
codegen.inheritance_map(AtomX("qwerty"))
  File "/Users/baskakov/PycharmProjects/codegen.py", line 5, in inheritance_map
assert isinstance(candidate, AtomX)
AssertionError

```

--
Added file: https://bugs.python.org/file50221/main.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44922] isinstance breaks on imported dataclasses

2021-08-15 Thread Eric V. Smith


Eric V. Smith  added the comment:

I get a circular import error:

Traceback (most recent call last):
  File "main.py", line 1, in 
import codegen
  File "/cygdrive/c/home/eric/codegen.py", line 1, in 
from main import AtomX
  File "/cygdrive/c/home/eric/main.py", line 9, in 
codegen.inheritance_map(AtomX("qwerty"))
AttributeError: partially initialized module 'codegen' has no attribute 
'inheritance_map' (most likely due to a circular import)

So I can't reproduce this problem.

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44922] isinstance breaks on imported dataclasses

2021-08-15 Thread Oleg Baskakov


New submission from Oleg Baskakov :

Hey I was trying to import dataclasses from another file and somehow isinstance 
doesn't work anymore:
main.py:
```
import codegen
from dataclasses import dataclass

@dataclass
class AtomX:
my_symbol: str
quantity: str = ""

codegen.inheritance_map(AtomX("qwerty"))

```
codegen.py:
```
from main import AtomX

def inheritance_map(candidate):
assert isinstance(candidate, AtomX)
```

PS the same code with `assert candidate.__class__.__name__ == "AtomX"` works 
fine


Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53) 
[Clang 6.0 (clang-600.0.57)] on darwin
I'm running inside of PyCharm

--
messages: 399628
nosy: baskakov
priority: normal
severity: normal
status: open
title: isinstance breaks on imported dataclasses
type: behavior
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com