Skip to content

Module has no attribute error #9841

Closed
Closed

Description

Bug Report

Mypy throws the Module has no attribute error when trying to get a module attribute in a for loop by iterating the list of modules

To Reproduce

Try it yourself: https://repl.it/@vanburgerberg/Mypy-test

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: no
  • Mypy configuration options from mypy.ini (and other config files): no config files
  • Python version used: 3.8.2, 3.9.0
  • Operating system and version: Linux

Activity

  1. ghost added
    bugmypy got something wrong
    on Dec 28, 2020
  2. gvanrossum commented on Dec 28, 2020

    @gvanrossum
    Member

    FWIW, the example consists of two files:

    • foo.py
    import bar
    
    for element in [bar]:
      element.test()
    • bar.py
    def test():
      print("Hello world")

    The problem here is that the type of [bar] is not "a list containing the module bar" but just "a list containing modules".

    I believe we have seen other reports of this, but I can't recall specific issues. I think you can fix it by defining a Protocol representing the module's interface, and using that to declare the type of the list.

  3. aryansingla commented on Dec 30, 2020

    @aryansingla

    I want to contribute in it.

  4. erictraut commented on Aug 12, 2023

    @erictraut

    This bug appears to have been fixed. It no longer repros in mypy 1.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions