Skip to content

Restore all_are_submodules import logic as workaround for #4498 #5016

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

Merged
merged 3 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,22 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
elif isinstance(imp, ImportFrom):
cur_id = correct_rel_imp(imp)
pos = len(res)
all_are_submodules = True
# Also add any imported names that are submodules.
pri = import_priority(imp, PRI_MED)
for name, __ in imp.names:
sub_id = cur_id + '.' + name
if self.is_module(sub_id):
res.append((pri, sub_id, imp.line))
else:
all_are_submodules = False
# Add cur_id as a dependency, even if all of the
# imports are submodules. Processing import from will try
# to look through cur_id, so we should depend on it.
pri = import_priority(imp, PRI_HIGH)
# As a workaround for for some bugs in cycle handling (#4498),
# if all of the imports are submodules, do the import at a lower
# priority.
pri = import_priority(imp, PRI_HIGH if not all_are_submodules else PRI_LOW)
res.insert(pos, ((pri, cur_id, imp.line)))
elif isinstance(imp, ImportAll):
pri = import_priority(imp, PRI_HIGH)
Expand Down
42 changes: 42 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,48 @@ from foo import bar
[file foo/bar.py]
pass

[case testImportReExportFromChildrenInCycle1]
# cmd: mypy -m project.root project.study.a project.neighbor
[file project/__init__.py]
from project.study import CustomType
x = 10
[file project/root.py]
[file project/study/__init__.py]
from project.study.a import CustomType
[file project/study/a.py]
from project import root
# TODO (#4498): This test is basically testing the `all_are_submodules` logic
# in build, which skips generating a dependenecy to a module if
# everything in it is a submodule. But that is still all just a
# workaround for bugs in cycle handling. If we uncomment the next
# line, we'll still break:
# from project import x
CustomType = str
[file project/neighbor/__init__.py]
from project.study import CustomType
def m(arg: CustomType) -> str:
return 'test'

[case testImportReExportFromChildrenInCycle2]
# cmd: mypy -m project project.b project.ba project.c
# See comments in above test about this being a workaround.
[file foo.py]
def get_foo() -> int: return 12

[file project/ba.py]
from . import b
b.FOO

[file project/b.py]
import foo
from . import c
FOO = foo.get_foo()

[file project/c.py]

[file project/__init__.py]
from . import ba

[case testSuperclassInImportCycle]
import a
import d
Expand Down