MCPcopy Index your code
hub / github.com/python/mypy / infer_reachability_of_if_statement

Function infer_reachability_of_if_statement

mypy/reachability.py:53–76  ·  view source on GitHub ↗
(s: IfStmt, options: Options)

Source from the content-addressed store, hash-verified

51
52
53def 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
79def infer_reachability_of_match_statement(s: MatchStmt, options: Options) -> None:

Calls 6

BlockClass · 0.90
rangeClass · 0.85
lenFunction · 0.85
infer_condition_valueFunction · 0.85
mark_block_unreachableFunction · 0.85
mark_block_mypy_onlyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…