8000 Make join(None, T) produce Optional[T] in strict-optional by ddfisher · Pull Request #1999 · python/mypy · GitHub
[go: up one dir, main page]

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
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make join(None, T) produce Optional[T]
  • Loading branch information
ddfisher committed Aug 8, 2016
commit 17acef12bcf155cd55559174345801d919ac3e10
10 changes: 9 additions & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ 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

# Use a visitor to handle non-trivial cases.
return t.accept(TypeJoinVisitor(s))



Copy link
Member

Choose a reason for hiding this comment

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

You should never need three blank lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops! That was accidental. As they say, "0.002K blank lines ought to be enough for anybody".

class TypeJoinVisitor(TypeVisitor[Type]):
"""Implementation of the least upper bound algorithm.

Expand Down Expand Up @@ -114,8 +118,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
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
0