Skip to content

"Module has no attribute" error with import cycle #2016

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

Closed
JukkaL opened this issue Aug 12, 2016 · 3 comments
Closed

"Module has no attribute" error with import cycle #2016

JukkaL opened this issue Aug 12, 2016 · 3 comments
Labels
bug mypy got something wrong topic-import-cycles

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 12, 2016

The following code generates an unexpected error message:

$ head *
==> part1.py <==
from part3 import part3_thing
class FirstThing: pass

==> part2.py <==
from part4 import part4_thing

from socket import socket as Thing

==> part3.py <==
from part2 import Thing
def part3_thing(x, *y): pass
_thing = Thing()

==> part4.py <==
MYPY = False
if MYPY:
    from part1 import FirstThing
part4_thing = 1

Here's what I get when I try to type check it:

$ mypy part{1,2,3,4}.py
part3.py:1: error: Module has no attribute 'Thing'

If I change the order of modules the error won't occur (part{1,3,2,4}.py), as it depends on the order in which we process the cycle.

This is related to #2015.

A potential fix would be to flag the import in part4 as low priority, resulting in a more consistently correct order of processing of the cycle (see #2015 for a discussion of this).

Another, likely less nice fix would be to run the semantic analysis pass that resolves from ... import ... statements within a cycle multiple times somehow, until all imports have been resolved.

@JukkaL JukkaL added bug mypy got something wrong topic-import-cycles labels Aug 12, 2016
@ddfisher ddfisher added this to the 0.5 milestone Aug 18, 2016
@elazarg
Copy link
Contributor

elazarg commented Sep 25, 2016

I can reproduce this, but not in check-modules.test. Do you know why is that?

@gvanrossum
Copy link
Member

This will be fixed by #2167.

@gvanrossum
Copy link
Member

Fixed by #2167

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-import-cycles
Projects
None yet
Development

No branches or pull requests

4 participants