python / python/mypy

Stub generation adds too many explicit re-exports

Open
#18,113 2 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

mypy stubgen re-exports more names than needed (despite --export-less). It seems that imports that are only used in function parameter type annotations (see FirstErrorType below) are always re-exported and imports from 'if TYPE_CHECKING' clauses (see SecondErrorType below) as well.

To Reproduce

# example.py
from typing import TYPE_CHECKING
from b import FirstErrorType, WorkingType
if TYPE_CHECKING:
    from c import SecondErrorType

def foobar(one: FirstErrorType, two: "SecondErrorType") -> None:
    pass

def spamandeggs() -> WorkingType:
    return WorkingType()

Expected Behavior

Nether FirstErrorType nor SecondErrorType should be re-exported according to the documentation of the --export-less switch, which states

only export imported names that are not referenced in the module that contains the import.

Actual Behavior

the output of stubgen --export-less example.py

# example.pyi
from b import FirstErrorType as FirstErrorType, WorkingType
from c import SecondErrorType as SecondErrorType

def foobar(one: FirstErrorType, two: SecondErrorType) -> None: ...
def spamandeggs() -> WorkingType: ...

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: stubgen --export-less
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10

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 issue with the shown example.py and run stubgen --export-less, then inspect the generated example.pyi. Trace stubgen's export-less handling for annotation-only imports and imports under TYPE_CHECKING; done means FirstErrorType and SecondErrorType are not explicit re-exports while WorkingType remains exported.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.