Skip to content

For Optional[T] or T, infer the type T instead of Optional[T]. #1818

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

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 17 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,17 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
elif e.op == 'or':
# Special case for `Optional[T] or T` -- this should infer T.
# If the type is a Union containing None, remove the None.
if isinstance(left_type, UnionType):
items = [i for i in left_type.items if not isinstance(i, NoneTyp)]
if len(items) < len(left_type.items):
left_type = UnionType.make_simplified_union(items)

# Special case for `None or T` -- this should infer T.
if isinstance(left_type, NoneTyp):
left_type = None

_, right_map = \
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
Expand All @@ -1103,9 +1114,13 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:

right_type = self.accept(e.right, left_type)

self.check_not_void(left_type, context)
if left_type is not None:
self.check_not_void(left_type, context)
self.check_not_void(right_type, context)
return UnionType.make_simplified_union([left_type, right_type])
if left_type is None:
return right_type
else:
return UnionType.make_simplified_union([left_type, right_type])

def check_list_multiply(self, e: OpExpr) -> Type:
"""Type check an expression of form '[...] * e'.
Expand Down
65 changes: 65 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,68 @@ def f(x: None) -> str: pass
def f(x: int) -> int: pass
reveal_type(f(None)) # E: Revealed type is 'builtins.str'
reveal_type(f(0)) # E: Revealed type is 'builtins.int'

[case testOptionalTypeOrTypePlain]
from typing import Optional
def f(a: Optional[int]) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test or with both operands optional?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could test left operand with a complex union type such as Union[int, str, None].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a or 0
[out]

[case testOptionalTypeOrTypeTypeVar]
from typing import Optional, TypeVar
T = TypeVar('T')
def f(a: Optional[T], b: T) -> T:
return a or b
[out]

[case testOptionalTypeOrTypeBothOptional]
from typing import Optional
def f(a: Optional[int], b: Optional[int]) -> None:
reveal_type(a or b)
def g(a: int, b: Optional[int]) -> None:
reveal_type(a or b)
[out]
main: note: In function "f":
main:3: error: Revealed type is 'Union[builtins.int, builtins.None]'
main: note: In function "g":
main:5: error: Revealed type is 'Union[builtins.int, builtins.None]'

[case testOptionalTypeOrTypeComplexUnion]
from typing import Union
def f(a: Union[int, str, None]) -> None:
reveal_type(a or 'default')
[out]
main: note: In function "f":
main:3: error: Revealed type is 'Union[builtins.int, builtins.str]'

[case testOptionalTypeOrTypeNoTriggerPlain]
from typing import Optional
def f(a: Optional[int], b: int) -> int:
return b or a
[out]
main: note: In function "f":
main:3: error: Incompatible return value type (got "Optional[int]", expected "int")

[case testOptionalTypeOrTypeNoTriggerTypeVar]
from typing import Optional, TypeVar
T = TypeVar('T')
def f(a: Optional[T], b: T) -> T:
return b or a
[out]
main: note: In function "f":
main:4: error: Incompatible return value type (got "Optional[T]", expected "T")

[case testNoneOrStringIsString]
def f() -> str:
a = None
b = ''
return a or b
[out]

[case testNoneOrTypeVarIsTypeVar]
from typing import TypeVar
T = TypeVar('T')
def f(b: T) -> T:
a = None
return a or b
[out]