Skip to content

stubgen: preserve type alias definitions when TypeAlias is imported from another module #22294

Open

Description

@spaceone

stubgen does not recognize TypeAlias annotations when TypeAlias is re-exported from another module.

Instead of preserving the type alias definition, it replaces the right-hand side with ....

This is particularly problematic when using a compatibility module to support TypeAlias across different Python versions.

To Reproduce

types_compat.py

import sys
from typing import TYPE_CHECKING

if sys.version_info >= (3, 10):  # workaround for mypy, which cannot distinguish between real imports in except clause
    from typing import TypeAlias
elif TYPE_CHECKING:   # don't add a real dependency on typing_extensions!
    from typing_extensions import TypeAlias
else:
    TypeAlias = object

example.py

from typing import Union

from types_compat import TypeAlias

LDAPTokenDictValue: TypeAlias = Union[tuple[()], tuple[str, ...]]

Run stubgen on example.py.

Actual behavior

The generated stub contains:

LDAPTokenDictValue: TypeAlias = ...

The actual type definition is lost.

Expected behavior

The generated stub should preserve the type alias definition:

LDAPTokenDictValue: TypeAlias = Union[tuple[()], tuple[str, ...]]

Additional context

When TypeAlias is imported directly from typing or defined using the compatibility logic in the same module, stubgen correctly preserves the type alias definition.

The problem only occurs when the TypeAlias annotation is imported through another module.

It would be useful if stubgen could recognize re-exported TypeAlias annotations and preserve their definitions, without requiring the compatibility logic to be duplicated across modules or post-processing of the generated stubs.

Additionally, my module is actually named _types - and I don't want to use --include-private to be able to use them type aliases.

stubgen 2.3.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions