python / python/mypy

'Name already defined' error when importing function from module with the same name

Open
#5,479 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

mypy shows an unexpected error, when I import all attributes from a module using * and a function has the same name as the module.

Example:
I have a package named foo, that contains the files __init__.pyi, a.pyi, b.pyi and c.pyi.

# a.pyi
def a() -> None: ...

# b.pyi
def not_b() -> None: ...

# c.pyi
def c() -> None: ...

# __init__.pyi
from .a import *
from .b import *
from .c import c

mypy foo shows the following error:
foo/__init__.pyi:1: error: Name 'a' already defined (by an import).

I have used mypy 0.620.

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

Start with the reported foo package and its init.pyi, a.pyi, b.pyi, and c.pyi files, then run mypy foo to reproduce the duplicate-name error. Trace handling of star imports and the explicit from .c import c import, and add coverage showing that the valid imports produce no erroneous redefinition diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.