(s: IfStmt, options: Options)
| 51 | |
| 52 | |
| 53 | def infer_reachability_of_if_statement(s: IfStmt, options: Options) -> None: |
| 54 | for i in range(len(s.expr)): |
| 55 | result = infer_condition_value(s.expr[i], options) |
| 56 | if result in (ALWAYS_FALSE, MYPY_FALSE): |
| 57 | # The condition is considered always false, so we skip the if/elif body. |
| 58 | mark_block_unreachable(s.body[i]) |
| 59 | elif result in (ALWAYS_TRUE, MYPY_TRUE): |
| 60 | # This condition is considered always true, so all of the remaining |
| 61 | # elif/else bodies should not be checked. |
| 62 | if result == MYPY_TRUE: |
| 63 | # This condition is false at runtime; this will affect |
| 64 | # import priorities. |
| 65 | mark_block_mypy_only(s.body[i]) |
| 66 | for body in s.body[i + 1 :]: |
| 67 | mark_block_unreachable(body) |
| 68 | |
| 69 | # Make sure else body always exists and is marked as |
| 70 | # unreachable so the type checker always knows that |
| 71 | # all control flow paths will flow through the if |
| 72 | # statement body. |
| 73 | if not s.else_body: |
| 74 | s.else_body = Block([]) |
| 75 | mark_block_unreachable(s.else_body) |
| 76 | break |
| 77 | |
| 78 | |
| 79 | def infer_reachability_of_match_statement(s: MatchStmt, options: Options) -> None: |
no test coverage detected
searching dependent graphs…