python / python/mypy

Star imports can't be redefined

Open
#10,563 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Using mypy 0.820+dev.61c346230cf4960c9766f7c2b5bd3312b47f74da and Python 3.9.4:

# a.py

x: int
# and many more definitions
# b.py

from a import *

x: str  # Name "x" already defined (possibly by an import)
reveal_type(x)  # Revealed type is "builtins.int"

This caused us a lot of trouble in python/typeshed#5545 and leads to this horrible and hard to maintain import mess: https://github.com/python/typeshed/blob/6ee67483a30e178f243d3a9a5b5a9bb4b902901a/stdlib/socket.pyi

I understand the warning and I think it makes sense to have to suppress it with # type: ignore. But the the fact that the override behaves differently from runtime is quite problematic as evidenced by socket.py. This is an example from the standard library that relies on the fact that star imports can be overridden selectively.

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 a.py and b.py reproduction, then compare mypy's handling of the star import and subsequent x annotation with Python runtime behavior. Use the socket.pyi example as a real-world case and add a regression test covering the selective override and its revealed type.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.