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

superclass information can be lost in import cycles #1394

Closed
rwbarton opened this issue Apr 17, 2016 · 0 comments
Closed

superclass information can be lost in import cycles #1394

rwbarton opened this issue Apr 17, 2016 · 0 comments

Comments

@rwbarton
Copy link
Contributor

The following test case fails:

[case testSuperclassInImportCycle]
import a
import d
a.A().f(d.D())
[file a.py]
if 0:
    import d
class B: pass
class C(B): pass
class A:
    def f(self, x: B) -> None: pass
[file d.py]
import a
class D(a.C): pass

with the error Argument 1 to "f" of "A" has incompatible type "D"; expected "B".

This test case is a reduction from mypy itself (a = mypy.types, A = Type, B = TypeVisitor, C = TypeTranslator, D is any type transformation defined in another module d). The test does not fail if the mypy-import of module d in module a is removed. Nor does it fail if the order of the imports in the main module is reversed. I found this counterintuitive at first, but upon reflection I guess it's because the order of traversing imports changes from main(a(d())) to main(d(a())), so whichever module is imported first is actually processed second.

I got as far as determining that something causes the MRO of D to be computed before the base classes of C are known. So the MRO of D ends up being computed as [D, C, object], which does not contain B. Seems that we could know when processing D that it has a base class in a module a that we are still in the middle of processing, and update the MRO of D when we finish processing a.

(#481 is the general ticket for issues involving import cycles.)

rwbarton added a commit to rwbarton/mypy that referenced this issue Apr 17, 2016
Fixes python#1394. This is a bit of a hack, but at least it's simple.
rwbarton added a commit to rwbarton/mypy that referenced this issue Apr 19, 2016
Fixes python#1394. This is a bit of a hack, but at least it's simple.
JukkaL pushed a commit that referenced this issue Apr 19, 2016
Fixes #1394. This is a bit of a hack, but at least it's simple.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant