Skip to content

stubgen: Use PEP 604 instead of Optional #10624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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