Skip to content

Make join(None, T) produce Optional[T] in strict-optional #1999

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 2 commits into from
Aug 8, 2016
Merged
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
9 changes: 8 additions & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
@@ -66,6 +66,9 @@ def join_types(s: Type, t: Type) -> Type:
if isinstance(s, ErasedType):
return t

if isinstance(s, UnionType) and not isinstance(t, UnionType):
s, t = t, s

if isinstance(s, NoneTyp) and not isinstance(t, NoneTyp):
s, t = t, s

@@ -114,8 +117,12 @@ def visit_none_type(self, t: NoneTyp) -> Type:
if experiments.STRICT_OPTIONAL:
if isinstance(self.s, (NoneTyp, UninhabitedType)):
return t
elif isinstance(self.s, UnboundType):
return AnyType()
elif isinstance(self.s, Void) or isinstance(self.s, ErrorType):
return ErrorType()
else:
return self.default(self.s)
return UnionType.make_simplified_union([self.s, t])
else:
if not isinstance(self.s, Void):
return self.s
8 changes: 8 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
@@ -330,3 +330,11 @@ def lookup_field(name, obj):
[out]
main: note: In function "lookup_field":
main:10: error: Need type annotation for variable

[case testTernaryWithNone]
reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.int, builtins.None]'
[builtins fixtures/bool.py]

[case testListWithNone]
reveal_type([0, None, 0]) # E: Revealed type is 'builtins.list[Union[builtins.int, builtins.None]]'
[builtins fixtures/list.py]