pythonpython-typingmypy

Module does not explicitly export attribute [attr-defined]


In bar.py foo is imported

# bar.py
from path import foo

In my current file bar is imported and I use the get_id function of foo:

from path import bar
bar.foo.get_id()

Mypy is complaining

error: Module "bar" does not explicitly export attribute "foo"  [attr-defined]

Solution

  • Is it your own module?

    Use one of these two options that are suggested in the mypy docs; the __all__ is generally preferred

    # For type-checkers...
    # This will re-export it as bar and allow other modules to import it
    from foo import bar as bar
    
    # This will also re-export bar
    from foo import bar
    __all__ = ['bar']
    

    If its a 3rd party; but also valid for your on modules

    Not all modules are designed and follow these suggestions you can disable/not check for the error, but first check: