Skip to content
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
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]