This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance breaks on imported dataclasses
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: baskakov, eric.smith
Priority: normal Keywords:

Created on 2021-08-15 22:19 by baskakov, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
main.py baskakov, 2021-08-16 05:20
Messages (4)
msg399628 - (view) Author: Oleg Baskakov (baskakov) Date: 2021-08-15 22:19
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
msg399631 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-08-16 00:55
I get a circular import error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import codegen
  File "/cygdrive/c/home/eric/codegen.py", line 1, in <module>
    from main import AtomX
  File "/cygdrive/c/home/eric/main.py", line 9, in <module>
    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.
msg399633 - (view) Author: Oleg Baskakov (baskakov) Date: 2021-08-16 05:20
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 <module>
    codegen.inheritance_map(AtomX("qwerty"))
  File "/Users/baskakov/PycharmProjects/codegen.py", line 5, in inheritance_map
    assert isinstance(candidate, AtomX)
AssertionError

```
msg399634 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-08-16 06:05
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)=<class '__main__.AtomX'> AtomX=<class 'main.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.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89085
2021-08-16 06:05:16eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg399634

stage: resolved
2021-08-16 05:20:26baskakovsetfiles: + main.py

messages: + msg399633
2021-08-16 00:55:26eric.smithsetnosy: + eric.smith
messages: + msg399631
2021-08-15 22:19:35baskakovcreate