(self, e: ConditionalExpr, allow_none_return: bool = False)
| 6004 | self.msg.redundant_condition_in_comprehension(True, condition) |
| 6005 | |
| 6006 | def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = False) -> Type: |
| 6007 | self.accept(e.cond) |
| 6008 | ctx: Type | None = self.type_context[-1] |
| 6009 | |
| 6010 | # Gain type information from isinstance if it is there |
| 6011 | # but only for the current expression |
| 6012 | if_map, else_map = self.chk.find_isinstance_check(e.cond) |
| 6013 | if codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes: |
| 6014 | if mypy.checker.is_unreachable_map(if_map): |
| 6015 | self.msg.redundant_condition_in_if(False, e.cond) |
| 6016 | elif mypy.checker.is_unreachable_map(else_map): |
| 6017 | self.msg.redundant_condition_in_if(True, e.cond) |
| 6018 | |
| 6019 | if ctx is None: |
| 6020 | # When no context is provided, compute each branch individually, and |
| 6021 | # use the union of the results as artificial context. Important for: |
| 6022 | # - testUnificationDict |
| 6023 | # - testConditionalExpressionWithEmpty |
| 6024 | ctx_if_type = self.analyze_cond_branch( |
| 6025 | if_map, e.if_expr, context=ctx, allow_none_return=allow_none_return |
| 6026 | ) |
| 6027 | ctx_else_type = self.analyze_cond_branch( |
| 6028 | else_map, e.else_expr, context=ctx, allow_none_return=allow_none_return |
| 6029 | ) |
| 6030 | if has_ambiguous_uninhabited_component(ctx_if_type): |
| 6031 | ctx = ctx_else_type |
| 6032 | elif has_ambiguous_uninhabited_component(ctx_else_type): |
| 6033 | ctx = ctx_if_type |
| 6034 | else: |
| 6035 | ctx = make_simplified_union([ctx_if_type, ctx_else_type]) |
| 6036 | |
| 6037 | if_type = self.analyze_cond_branch( |
| 6038 | if_map, e.if_expr, context=ctx, allow_none_return=allow_none_return |
| 6039 | ) |
| 6040 | else_type = self.analyze_cond_branch( |
| 6041 | else_map, e.else_expr, context=ctx, allow_none_return=allow_none_return |
| 6042 | ) |
| 6043 | |
| 6044 | res: Type = make_simplified_union([if_type, else_type]) |
| 6045 | if has_uninhabited_component(res) and not isinstance( |
| 6046 | get_proper_type(self.type_context[-1]), UnionType |
| 6047 | ): |
| 6048 | # In rare cases with empty collections join may give a better result. |
| 6049 | alternative = join.join_types(if_type, else_type) |
| 6050 | p_alt = get_proper_type(alternative) |
| 6051 | if not isinstance(p_alt, Instance) or p_alt.type.fullname != "builtins.object": |
| 6052 | res = alternative |
| 6053 | return res |
| 6054 | |
| 6055 | def analyze_cond_branch( |
| 6056 | self, |
no test coverage detected