Skip to content

False-positive [attr-defined] when importing classes and functions from a file with the same name as the parent package #19410

Open
@ArrayBolt3

Description

@ArrayBolt3

Bug Report

Suppose you have the following directory tree:

thebug/
thebug/run.py
thebug/__init__.py
thebug/thebug.py

Attempting to import anything from thebug.py (using from thebug import whatever) will cause mypy to complain error: Module "thebug" has no attribute "whatever" [attr-defined].

To Reproduce

  1. mkdir thebug
  2. cd thebug
  3. touch __init__.py
  4. vim thebug.py and type in the following contents:
class A:
    def __init__(self) -> None:
        self.val = "a"
  1. vim run.py and type in the following contents:
from thebug import A
var: A = A()
print(var.val)
  1. Run it to make sure it works:
$ python3 run.py
a
  1. Type-check it:
mypy run.py

Actual Behavior

$ mypy run.py
run.py:1: error: Module "thebug" has no attribute "A"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.16.1
  • Mypy command-line flags: none, as shown above
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions