Infer whether the given condition is always true/false. Return ALWAYS_TRUE if always true, ALWAYS_FALSE if always false, MYPY_TRUE if true under mypy and false at runtime, MYPY_FALSE if false under mypy and true at runtime, else TRUTH_VALUE_UNKNOWN.
(expr: Expression, options: Options)
| 106 | |
| 107 | |
| 108 | def infer_condition_value(expr: Expression, options: Options) -> int: |
| 109 | """Infer whether the given condition is always true/false. |
| 110 | |
| 111 | Return ALWAYS_TRUE if always true, ALWAYS_FALSE if always false, |
| 112 | MYPY_TRUE if true under mypy and false at runtime, MYPY_FALSE if |
| 113 | false under mypy and true at runtime, else TRUTH_VALUE_UNKNOWN. |
| 114 | """ |
| 115 | if isinstance(expr, UnaryExpr) and expr.op == "not": |
| 116 | positive = infer_condition_value(expr.expr, options) |
| 117 | return inverted_truth_mapping[positive] |
| 118 | |
| 119 | pyversion = options.python_version |
| 120 | name = "" |
| 121 | |
| 122 | result = TRUTH_VALUE_UNKNOWN |
| 123 | if isinstance(expr, NameExpr): |
| 124 | name = expr.name |
| 125 | elif isinstance(expr, MemberExpr): |
| 126 | name = expr.name |
| 127 | elif isinstance(expr, OpExpr): |
| 128 | if expr.op not in ("or", "and"): |
| 129 | return TRUTH_VALUE_UNKNOWN |
| 130 | |
| 131 | left = infer_condition_value(expr.left, options) |
| 132 | right = infer_condition_value(expr.right, options) |
| 133 | results = {left, right} |
| 134 | if expr.op == "or": |
| 135 | if ALWAYS_TRUE in results: |
| 136 | return ALWAYS_TRUE |
| 137 | elif MYPY_TRUE in results: |
| 138 | return MYPY_TRUE |
| 139 | elif left == right == MYPY_FALSE: |
| 140 | return MYPY_FALSE |
| 141 | elif results <= {ALWAYS_FALSE, MYPY_FALSE}: |
| 142 | return ALWAYS_FALSE |
| 143 | elif expr.op == "and": |
| 144 | if ALWAYS_FALSE in results: |
| 145 | return ALWAYS_FALSE |
| 146 | elif MYPY_FALSE in results: |
| 147 | return MYPY_FALSE |
| 148 | elif left == right == ALWAYS_TRUE: |
| 149 | return ALWAYS_TRUE |
| 150 | elif results <= {ALWAYS_TRUE, MYPY_TRUE}: |
| 151 | return MYPY_TRUE |
| 152 | return TRUTH_VALUE_UNKNOWN |
| 153 | else: |
| 154 | result = consider_sys_version_info(expr, pyversion) |
| 155 | if result == TRUTH_VALUE_UNKNOWN: |
| 156 | result = consider_sys_platform(expr, options.platform) |
| 157 | if result == TRUTH_VALUE_UNKNOWN: |
| 158 | if name == "PY2": |
| 159 | result = ALWAYS_FALSE |
| 160 | elif name == "PY3": |
| 161 | result = ALWAYS_TRUE |
| 162 | elif name == "MYPY" or name == "TYPE_CHECKING": |
| 163 | result = MYPY_TRUE |
| 164 | elif name in options.always_true: |
| 165 | result = ALWAYS_TRUE |
no test coverage detected
searching dependent graphs…