Skip to content

Variable assignment on module reference reported as "Module has no attribute <variable_name>" #7171

Closed
@timmwagener

Description

@timmwagener

Please provide more information to help us understand the issue:

  • Problem
  • Are you reporting a bug, or opening a feature request?
    • Bug
  • Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.
# a.py
import sys
this = sys.modules[__name__]
this.serializable_classes = {}  # a.py:15: error: Module has no attribute "serializable_classes"
setattr(this, "serializable_classes", {})  # alternative without mypy error
  • What is the actual behavior/output?
    • error: Module has no attribute "serializable_classes"
  • What is the behavior/output you expect?
    • No error. It seems mypy applies rules for accessing a name from a module, while it really is an assignment. Why would the previous presence of a variable name in a module namespace matter under these circumstances?
  • What are the versions of mypy and Python you are using?
$ tox -e mypy
mypy installed: mypackage==0.0.1,mypy==0.711,mypy-extensions==0.4.1,typed-ast==1.4.0
mypy run-test-pre: PYTHONHASHSEED='349'
mypy run-test: commands[0] | python --version
Python 3.7.3
  • What are the mypy flags you are using? (For example --strict-optional)
# mypy.ini
[mypy]
warn_return_any = True
warn_unused_configs = True
# and some import ignores below

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