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: import class not isinstance of the class
Type: behavior Stage: resolved
Components: macOS Versions: Python 3.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, ronaldoussoren, 邱伟略
Priority: normal Keywords:

Created on 2017-07-10 02:13 by 邱伟略, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg298018 - (view) Author: 邱伟略 (邱伟略) Date: 2017-07-10 02:13
the working directory is like below:

bug/
    dirc/
        __init__.py
        foo.py
        foo2.py
    __init__.py
    foo1.py
    

in foo.py:
```
class Event(object):
    pass
```

in foo2.py:
```
from a.foo import Event

def fun():
    return Event()
```

in foo1.py:
```
from bug.a.foo import Event
from bug.a.foo2 import fun

assert isinstance(fun(), Event)
```

when i try to execute the code in foo1.py, i got an assertion error.

but if i change foo2.py to:
```
from ..a.foo import Event

def fun():
    return Event()

```

the code in foo1.py can be done well without the assertion error.

i think it's about the import mechanism. 

i have checked pep328

I think 
when i do "from a.foo import Event", python import Event by the package name "a.foo.Event", but "from ..a.foo import Event" is going to import Event by "bug.a.foo.Event". and it is totally different in python.

is it kind of bug python should prevent?
msg298029 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2017-07-10 07:01
In foo2.py you import "a.foo", which refers to module 'a' that isn't included in the directory structure you describe.  I'm assuming 'dirc' should be 'a' to match the code.

How did you run foo1.py?  

When running with 'python3.6 -m bug.foo1' I get an error about not being able to import 'a' (as expected, the package is 'bug.a').

When you run with "PYTHONPATH=. python3.6 bug/foo1.py" I get the assertion failure you mention, and that's expected behavior.  This gets clearer when you print type(fun()) and Event, the output will be:

  <class 'a.foo.Event'>
  <class 'bug.a.foo.Event'>

As you can see these refer to two different classes, not the same class.

The reason for this is that  'python3.6 bug/foo1.py' adds the 'bug' directory to the start of sys.path, hence the "import a.foo" in foo2 succeeds. The script foo1.py imports 'bug.a.foo', which is a different name that happens to refer to the same file on the filesystem.

All in all this is expected behavior and not a bug.
msg298568 - (view) Author: 邱伟略 (邱伟略) Date: 2017-07-18 01:07
ok. i see what you mean.
so python mark the class from the import way
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 75071
2017-07-18 01:07:47邱伟略setstatus: pending -> closed
resolution: not a bug -> works for me
messages: + msg298568

stage: resolved
2017-07-10 07:01:17ronaldoussorensetstatus: open -> pending
resolution: not a bug
messages: + msg298029
2017-07-10 02:13:00邱伟略create