Skip to content

Module X has no attribute Y when using star imports #4930

Closed
@nicktimko

Description

@nicktimko

mypy seems to emit a spurious warning that it can't find an attribute of a module that should be imported via a star import and where another import refers to it.

Input

$ head mypackage/*

  • ==> mypackage/__init__.py <==
from mypackage.submod1 import *     # * -> HELLO and error goes away
from mypackage.submod2 import greet # comment out and error goes away
__all__ = ['HELLO', 'greet'] # does nothing
  • ==> mypackage/submod1.py <==
__all__ = ['HELLO']
HELLO = 'hello there'
  • ==> mypackage/submod2.py <==
from mypackage import HELLO
__all__ = ['greet']
def greet():
    print(HELLO)

Current Output

$ mypy mypackage
mypackage/submod2.py:1: error: Module 'mypackage' has no attribute 'HELLO'
$ python -c "import mypackage;mypackage.greet()" # Python has no problems
hello there
$ python --version; mypy --version
Python 3.6.4
mypy 0.590

(haven't checked with master, but I couldn't find any recently closed issues that seemed to talk about something like this)

Expected Output

No error from mypy.

As per the comments in the __init__.py, if some of the imports are tweaked, then mypy is happy. It doesn't seem to be sated by an __all__ to help the star import. I don't quite understand why not importing mypackage.submod2 causes the error to go away as well; it's not like there's a circular import between 1 and 2 where the names in the module aren't defined before it imports (i.e. if you moved the ...import greet line above the other, Python would choke)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions