Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to resolve a specific import structure #4144

Open
elprans opened this issue Oct 20, 2017 · 5 comments
Open

Failure to resolve a specific import structure #4144

elprans opened this issue Oct 20, 2017 · 5 comments
Labels

Comments

@elprans
Copy link
Contributor

elprans commented Oct 20, 2017

Given the following module structure:

top
|-- __init__.py
|-- b
|   |-- __init__.py
|   `-- _b.py
`-- c.py

And the following module contents:

  • top/__init__.py:
from .b import *
from .c import *
  • top/b/__init__.py:
from ._b import *
  • top/b/_b.py:
import top
class Cls:
    pass
  • top/c.py:
from . import b
foo: b.Cls = None

mypy -m top fails to resolve b.Cls:

top/c.py:2: error: Name 'b.Cls' is not defined

@gvanrossum
Copy link
Member

What does mypy --dump-graph -m top print?

@elprans
Copy link
Contributor Author

elprans commented Oct 20, 2017

[["n0", 91875, ["_importlib_modulespec", "mypy_extensions", "types", "abc", "typing", "builtins", "collections", "sys", "collections.abc"],
     {"_importlib_modulespec": 1810, "mypy_extensions": 731, "types": 7066, "abc": 809, "typing": 18315, "builtins": 43416, "collections": 13579, "sys": 5019, "collections.abc": 1130},
     {}],
 ["n1", 120, ["top.b._b", "top.c", "top.b", "top"],
     {"top.b._b": 33, "top.c": 35, "top.b": 18, "top": 34},
     {}]
]

@gvanrossum
Copy link
Member

gvanrossum commented Oct 20, 2017 via email

@elprans
Copy link
Contributor Author

elprans commented Oct 20, 2017

Yes, that workaround would work in this case.

That said, python itself has no problem with cycles like this. You can import a module which is currently being loaded, you're just not guaranteed that it'll have all the attributes you want.

More specifically, an import of top from top.b._b should be short-circuited because top itself has not been fully loaded yet. importlib just does return sys.modules['top'] in such case.

@gvanrossum
Copy link
Member

gvanrossum commented Oct 20, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants