Skip to content

Commit

Permalink
Always infer union types for ternaries
Browse files Browse the repository at this point in the history
Undoes python#5095. Part of python#12056. This fails a few tests but I want to
see the mypy-primer output before I spend time fixing them.
  • Loading branch information
JelleZijlstra committed Mar 21, 2022
1 parent 8d02b47 commit 01ce503
Showing 1 changed file with 1 addition and 10 deletions.
11 changes: 1 addition & 10 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3923,16 +3923,7 @@ def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = F
else_type = self.analyze_cond_branch(else_map, e.else_expr, context=if_type,
allow_none_return=allow_none_return)

# Only create a union type if the type context is a union, to be mostly
# compatible with older mypy versions where we always did a join.
#
# TODO: Always create a union or at least in more cases?
if isinstance(get_proper_type(self.type_context[-1]), UnionType):
res = make_simplified_union([if_type, full_context_else_type])
else:
res = join.join_types(if_type, else_type)

return res
return make_simplified_union([if_type, full_context_else_type])

def analyze_cond_branch(self, map: Optional[Dict[Expression, Type]],
node: Expression, context: Optional[Type],
Expand Down

0 comments on commit 01ce503

Please sign in to comment.