Skip to content

Accessing attributes in modules imported by import_module #5059

@pawelswiecki

Description

@pawelswiecki

Are you reporting a bug, or opening a feature request?
I believe I found a bug in mypy (as far as I understand what's going on, this is not a typeshed issue).

The code

# my_file.py
from importlib import import_module
import os

settings_module_path = os.getenv('PRODUCTION_SETTINGS_MODULE')
settings = import_module(settings_module_path)

if settings and hasattr(settings, 'LOGGING'):
    log_config = settings.LOGGING

Behaviour

$ mypy 
my_file.py:8: error: Module has no attribute "LOGGING"

Expected behavior
No mypy error. Especially, when the attribute is explicitly asserted to be there (analogically as with narrowing types using isinstance()).

Versions
Python 3.6.5
mypy==0.600

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions