Skip to content

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

Closed
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions