Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub generation adds too many explicit re-exports #18113

Open
henzef opened this issue Nov 6, 2024 · 2 comments
Open

Stub generation adds too many explicit re-exports #18113

henzef opened this issue Nov 6, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@henzef
Copy link

henzef commented Nov 6, 2024

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
@henzef henzef added the bug mypy got something wrong label Nov 6, 2024
@henzef
Copy link
Author

henzef commented Nov 6, 2024

Only semi-related remark: It would be helpful if stubgen could be configured to only re-export names that are already explicitly re-exported in the source file. This would allow me to workaround this issue and would also fix #2190

@jorenham
Copy link

jorenham commented Nov 7, 2024

it also does this if there is an explicit __all__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants