From 602f7a14e8afe7e65e26ba72c30c30a7da75d4ef Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 29 Jan 2023 18:40:04 -0800 Subject: [PATCH] stubgen: fix PEP 604 union in typevar bound Fixes #14533 --- mypy/stubgen.py | 4 ++++ test-data/unit/stubgen.test | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 8c7e24504270..bed552c3e214 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -95,6 +95,7 @@ MemberExpr, MypyFile, NameExpr, + OpExpr, OverloadedFuncDef, Statement, StrExpr, @@ -402,6 +403,9 @@ def visit_list_expr(self, node: ListExpr) -> str: def visit_ellipsis(self, node: EllipsisExpr) -> str: return "..." + def visit_op_expr(self, o: OpExpr) -> str: + return f"{o.left.accept(self)} {o.op} {o.right.accept(self)}" + class ImportTracker: """Record necessary imports during stub generation.""" diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index 8467271e5593..009db553237f 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -2734,3 +2734,12 @@ class Some: def __int__(self) -> int: ... def __float__(self) -> float: ... def __index__(self) -> int: ... + + +[case testTypeVarPEP604Bound] +from typing import TypeVar +T = TypeVar("T", bound=str | None) +[out] +from typing import TypeVar + +T = TypeVar('T', bound=str | None)