Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ def visit_index_expr(self, node: IndexExpr) -> str:
return " | ".join([item.accept(self) for item in node.index.items])
return node.index.accept(self)
if base_fullname == "typing.Optional":
if isinstance(node.index, TupleExpr):
return f"{self.stubgen.add_name('_typeshed.Incomplete')} | None"
return f"{node.index.accept(self)} | None"
base = node.base.accept(self)
index = node.index.accept(self)
Expand Down
4 changes: 3 additions & 1 deletion mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ def visit_unbound_type(self, t: UnboundType) -> str:
if fullname == "typing.Union":
return " | ".join([item.accept(self) for item in t.args])
if fullname == "typing.Optional":
return f"{t.args[0].accept(self)} | None"
if len(t.args) == 1:
return f"{t.args[0].accept(self)} | None"
return f"{self.stubgen.add_name('_typeshed.Incomplete')} | None"
if fullname in TYPING_BUILTIN_REPLACEMENTS:
s = self.stubgen.add_name(TYPING_BUILTIN_REPLACEMENTS[fullname], require=True)
if self.known_modules is not None and "." in s:
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -4366,3 +4366,25 @@ class Foo(Enum):
class Bar(Enum):
A = ...
B = ...

[case testGracefullyHandleInvalidOptionalUsage]
from typing import Optional

x: Optional
y: Optional[int]
z: Optional[int, str]

X = Optional
Y = Optional[int]
Z = Optional[int, str]

[out]
from _typeshed import Incomplete
from typing import Optional

x: Incomplete | None
y: int | None
z: Incomplete | None
X = Optional
Y = int | None
Z = Incomplete | None