python / python/mypy

`name.py` used as module instead of `name` module directory

Open
#7,711 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I have a module named mdl with the below __init__.py (The contents don't actually matter).

from .document import load_document

Then I have a file called mdl.py:

import mdl
doc = mdl.load_document( "happy" )

mypy produces the error:

mdl.py:2: error: Module has no attribute "load_document"

If I rename the file to anything_else.py this error doesn't happen.

It appears as though mypy is resolving the mdl import to the file mdl.py instead of the mdl module directory. Since the Python interpreter resolve the names as I intended, I'd assume mypy would as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case with the mdl package's init.py and the sibling mdl.py file, then inspect how mypy resolves the import in mdl.py. Compare that result with Python's resolution behavior and identify the relevant import-resolution tests or entry point. Done means mypy recognizes load_document from the package directory without requiring the file to be renamed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.