From 3400539ebd25f5a35a2de7595cf990f8e857650d Mon Sep 17 00:00:00 2001 From: Shanmukh Dheekonda Date: Fri, 6 Nov 2026 09:51:58 +0000 Subject: [PATCH] stubgen: preserve explicit type aliases with a re-exported TypeAlias stubgen runs mypy with follow_imports=skip, so a TypeAlias name that is imported from another module (e.g. a version compatibility shim) is never resolved back to typing.TypeAlias. The analyzed annotation then degrades to Any, visit_assignment_stmt() no longer treats the assignment as an explicit (PEP 613) type alias, and the right-hand side is dropped from the generated stub. Fall back to the annotation name as written in the source when deciding whether the assignment is an explicit type alias. Fixes #22294 --- mypy/stubgen.py | 10 ++++++-- mypy/test/teststubgen.py | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index fe90c3d256b7a..237537afaadd0 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -979,8 +979,14 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None: and self.is_alias_expression(o.rvalue) and not self.is_private_name(lvalue.name) ): - is_explicit_type_alias = ( - o.unanalyzed_type and getattr(o.type, "name", None) == "TypeAlias" + is_explicit_type_alias = bool(o.unanalyzed_type) and ( + getattr(o.type, "name", None) == "TypeAlias" + # The annotation isn't always analyzed to `typing.TypeAlias`: + # stubgen runs mypy with follow_imports=skip, so a `TypeAlias` + # name that is re-exported from another module (e.g. a version + # compatibility shim) can't be resolved. Fall back to the name + # as written in the source in that case. + or getattr(o.unanalyzed_type, "name", None) == "TypeAlias" ) if is_explicit_type_alias: self.process_typealias(lvalue, o.rvalue, is_explicit_type_alias=True) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index b5cdbf479f1c2..f421798d04f20 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -942,6 +942,59 @@ def add_file(self, path: str, result: list[str], header: bool) -> None: result.extend(file.read().splitlines()) +class StubgenTypeAliasSuite(unittest.TestCase): + """Tests for preserving explicit (PEP 613) type aliases in generated stubs.""" + + # A version compatibility shim that re-exports TypeAlias, as reported in + # https://github.com/python/mypy/issues/22294. + COMPAT_MODULE = """\ +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_MODULE = """\ +from typing import Union + +from types_compat import TypeAlias + +LDAPTokenDictValue: TypeAlias = Union[tuple[()], tuple[str, ...]] +""" + + @unittest.skipIf(sys.platform == "win32", "clean up fails on Windows") + def test_explicit_type_alias_imported_from_other_module(self) -> None: + # stubgen runs mypy with follow_imports=skip, so the re-exported + # TypeAlias name can't be resolved back to typing.TypeAlias. The + # alias definition must still be preserved in the generated stub. + current = os.getcwd() + with local_sys_path_set(), tempfile.TemporaryDirectory() as tmp: + try: + os.chdir(tmp) + with open("types_compat.py", "w") as f: + f.write(self.COMPAT_MODULE) + with open("example.py", "w") as f: + f.write(self.EXAMPLE_MODULE) + options = parse_options(["-m", "example", "-o", "out"]) + options.no_import = True + generate_stubs(options) + with open(os.path.join("out", "example.pyi")) as f: + actual = f.read().splitlines() + finally: + os.chdir(current) + expected = [ + "from types_compat import TypeAlias as TypeAlias", + "", + "LDAPTokenDictValue: TypeAlias = tuple[()] | tuple[str, ...]", + ] + assert_string_arrays_equal(expected, actual, "Invalid stub generated for example.py") + + self_arg = ArgSig(name="self")