Skip to content

"Module has no attribute" when looping over modules #11085

Closed

Description

@l0b0

Bug Report

Even if first.att() and second.att() both exist, for module in [first, second]: module.att() results in 'Module has no attribute "att"'.

To Reproduce

  1. Go to an empty directory
  2. Create two files first.py and second.py, both containing just def att() -> None: pass
  3. Create test_both.py with the following contents:
    from . import first, second
    
    
    def test_both -> None:
        for module in [first, second]:
            module.att()
    
  4. touch __init__.py
  5. Run mypy .

Expected Behavior

This should pass type inspection.

Actual Behavior

test_both.py:6: error: Module has no attribute "att"
Found 1 error in 1 file (checked 4 source files)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.9.6
  • Operating system and version: Arch Linux

Workaround

Unravel the loop:

from . import first, second


def test_first() -> None:
    first.att()


def test_second() -> None:
    second.att()

Additional

Pyright seems to resolve the type to module: Union[first.py, second.py], which is an odd syntax. I don't know what the actual syntax for this would be.

Update: Looks like this might be a dupe of #1741, which was closed due to low priority. Would it be possible to bump the priority? :)

Activity

  1. added
    bugmypy got something wrong
    on Sep 10, 2021
  2. AlexWaygood commented on Mar 31, 2022

    @AlexWaygood
    Member

    Closing as a duplicate of #9841

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions