@@ -115,31 +115,44 @@ def infer_condition_value(expr: Expression, options: Options) -> int:
115
115
MYPY_TRUE if true under mypy and false at runtime, MYPY_FALSE if
116
116
false under mypy and true at runtime, else TRUTH_VALUE_UNKNOWN.
117
117
"""
118
+ if isinstance (expr , UnaryExpr ) and expr .op == "not" :
119
+ positive = infer_condition_value (expr .expr , options )
120
+ return inverted_truth_mapping [positive ]
121
+
118
122
pyversion = options .python_version
119
123
name = ""
120
- negated = False
121
- alias = expr
122
- if isinstance (alias , UnaryExpr ):
123
- if alias .op == "not" :
124
- expr = alias .expr
125
- negated = True
124
+
126
125
result = TRUTH_VALUE_UNKNOWN
127
126
if isinstance (expr , NameExpr ):
128
127
name = expr .name
129
128
elif isinstance (expr , MemberExpr ):
130
129
name = expr .name
131
- elif isinstance (expr , OpExpr ) and expr .op in ("and" , "or" ):
130
+ elif isinstance (expr , OpExpr ):
131
+ if expr .op not in ("or" , "and" ):
132
+ return TRUTH_VALUE_UNKNOWN
133
+
132
134
left = infer_condition_value (expr .left , options )
133
- if (left in (ALWAYS_TRUE , MYPY_TRUE ) and expr .op == "and" ) or (
134
- left in (ALWAYS_FALSE , MYPY_FALSE ) and expr .op == "or"
135
- ):
136
- # Either `True and <other>` or `False or <other>`: the result will
137
- # always be the right-hand-side.
138
- return infer_condition_value (expr .right , options )
139
- else :
140
- # The result will always be the left-hand-side (e.g. ALWAYS_* or
141
- # TRUTH_VALUE_UNKNOWN).
142
- return left
135
+ right = infer_condition_value (expr .right , options )
136
+ results = {left , right }
137
+ if expr .op == "or" :
138
+ if ALWAYS_TRUE in results :
139
+ return ALWAYS_TRUE
140
+ elif MYPY_TRUE in results :
141
+ return MYPY_TRUE
142
+ elif left == right == MYPY_FALSE :
143
+ return MYPY_FALSE
144
+ elif results <= {ALWAYS_FALSE , MYPY_FALSE }:
145
+ return ALWAYS_FALSE
146
+ elif expr .op == "and" :
147
+ if ALWAYS_FALSE in results :
148
+ return ALWAYS_FALSE
149
+ elif MYPY_FALSE in results :
150
+ return MYPY_FALSE
151
+ elif left == right == ALWAYS_TRUE :
152
+ return ALWAYS_TRUE
153
+ elif results <= {ALWAYS_TRUE , MYPY_TRUE }:
154
+ return MYPY_TRUE
155
+ return TRUTH_VALUE_UNKNOWN
143
156
else :
144
157
result = consider_sys_version_info (expr , pyversion )
145
158
if result == TRUTH_VALUE_UNKNOWN :
@@ -155,8 +168,6 @@ def infer_condition_value(expr: Expression, options: Options) -> int:
155
168
result = ALWAYS_TRUE
156
169
elif name in options .always_false :
157
170
result = ALWAYS_FALSE
158
- if negated :
159
- result = inverted_truth_mapping [result ]
160
171
return result
161
172
162
173
0 commit comments