Skip to content

Commit

Permalink
stubgen: Use PEP 604 instead of Optional (#10624)
Browse files Browse the repository at this point in the history
  • Loading branch information
srittau authored Jun 10, 2021
1 parent 5080053 commit 1cad082
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
4 changes: 1 addition & 3 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -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): ...
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1cad082

Please sign in to comment.