Stub generation adds too many explicit re-exports
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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