From 1cad082fa0df9110c8860d989003fb36a3b49da5 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 10 Jun 2021 23:31:19 +0200 Subject: [PATCH] stubgen: Use PEP 604 instead of Optional (#10624) --- mypy/stubgen.py | 4 +--- test-data/unit/stubgen.test | 14 +++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index a12cd969e9ca..f731b95c8790 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -1159,10 +1159,8 @@ def get_str_type_of_node(self, rvalue: Expression, return 'bool' if can_infer_optional and \ isinstance(rvalue, NameExpr) and rvalue.name == 'None': - self.add_typing_import('Optional') self.add_typing_import('Any') - return '{}[{}]'.format(self.typing_name('Optional'), - self.typing_name('Any')) + return '{} | None'.format(self.typing_name('Any')) self.add_typing_import('Any') return self.typing_name('Any') diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index cfcd13eb45ad..46451391ec31 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -32,9 +32,9 @@ def g(b: int = ..., c: int = ...) -> None: ... [case testDefaultArgNone] def f(x=None): ... [out] -from typing import Any, Optional +from typing import Any -def f(x: Optional[Any] = ...) -> None: ... +def f(x: Any | None = ...) -> None: ... [case testDefaultArgBool] def f(x=True, y=False): ... @@ -772,12 +772,12 @@ class A: def method(self, a=None): self.x = [] [out] -from typing import Any, Optional +from typing import Any class A: x: Any = ... - def __init__(self, a: Optional[Any] = ...) -> None: ... - def method(self, a: Optional[Any] = ...) -> None: ... + def __init__(self, a: Any | None = ...) -> None: ... + def method(self, a: Any | None = ...) -> None: ... [case testAnnotationImportsFrom] import foo @@ -1700,10 +1700,10 @@ def Optional(): return 0 [out] -from typing import Any as _Any, Optional as _Optional +from typing import Any as _Any def f(x: _Any = ...): ... -def g(x: _Optional[_Any] = ...) -> None: ... +def g(x: _Any | None = ...) -> None: ... x: _Any