From b6e91d46b299bfd0af36b37586d3337a20e14b0e Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sun, 11 Feb 2024 23:09:22 +0100 Subject: [PATCH] stubgen: Preserve empty tuple annotation (#16907) --- mypy/stubutil.py | 2 ++ test-data/unit/stubgen.test | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index 1a9c2357c58e..69af643efab2 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -250,6 +250,8 @@ def visit_unbound_type(self, t: UnboundType) -> str: self.stubgen.import_tracker.require_name(s) if t.args: s += f"[{self.args_str(t.args)}]" + elif t.empty_tuple_index: + s += "[()]" return s def visit_none_type(self, t: NoneType) -> str: diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index b5bccaa4cdbd..c56f6b40b74d 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -4253,3 +4253,17 @@ def f5(x: int, /, *, y: float): ... def f6(x: int = 0, /, *, y: float): ... def f7(x: int, /, *, y: float = 1): ... def f8(x: int = 0, /, *, y: float = 1): ... + +[case testPreserveEmptyTuple] +ann: tuple[()] +alias = tuple[()] +def f(x: tuple[()]): ... +class C(tuple[()]): ... + +[out] +ann: tuple[()] +alias = tuple[()] + +def f(x: tuple[()]): ... + +class C(tuple[()]): ...