Skip to content

Circular import + reexport = "error: Module '...' has no attribute '..."? #4049

@jstasiak

Description

@jstasiak

This is a minimal test case replicating what I'm experiencing in a larger project:

% tree
.
└── asd
    ├── __init__.py
    ├── one.py
    └── two.py

2 directories, 6 files

% cat asd/__init__.py 
from asd.one import One
from asd.two import Two

% cat asd/one.py 
class One:
    pass

% cat asd/two.py 
from asd import One

class Two:
    pass

Python can work with it:

% python -c 'from asd import Two; print(Two)'
<class 'asd.two.Two'>

mypy, however, doesn't like it:

% mypy .
asd/two.py:1: error: Module 'asd' has no attribute 'One'

It fails all the same when I change __init__.py to use import .. as .. (I saw this pattern mentioned in #3981):

% cat asd/__init__.py 
from asd.one import One as One
from asd.two import Two as Two

Apologies if there's already an issue filled for this, I searched for one briefly and haven't found any.

mypy 0.521, CPython 3.6.2

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions