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

Function consider_sys_version_info

mypy/reachability.py:182–223  ·  view source on GitHub ↗

Consider whether expr is a comparison involving sys.version_info. Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.

(expr: Expression, pyversion: tuple[int, ...])

Source from the content-addressed store, hash-verified

180
181
182def consider_sys_version_info(expr: Expression, pyversion: tuple[int, ...]) -> int:
183 """Consider whether expr is a comparison involving sys.version_info.
184
185 Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
186 """
187 # Cases supported:
188 # - sys.version_info[<int>] <compare_op> <int>
189 # - sys.version_info[:<int>] <compare_op> <tuple_of_n_ints>
190 # - sys.version_info <compare_op> <tuple_of_1_or_2_ints>
191 # (in this case <compare_op> must be >, >=, <, <=, but cannot be ==, !=)
192 if not isinstance(expr, ComparisonExpr):
193 return TRUTH_VALUE_UNKNOWN
194 # Let's not yet support chained comparisons.
195 if len(expr.operators) > 1:
196 return TRUTH_VALUE_UNKNOWN
197 op = expr.operators[0]
198 if op not in ("==", "!=", "<=", ">=", "<", ">"):
199 return TRUTH_VALUE_UNKNOWN
200
201 index = contains_sys_version_info(expr.operands[0])
202 thing = contains_int_or_tuple_of_ints(expr.operands[1])
203 if index is None or thing is None:
204 index = contains_sys_version_info(expr.operands[1])
205 thing = contains_int_or_tuple_of_ints(expr.operands[0])
206 op = reverse_op[op]
207 if isinstance(index, int) and isinstance(thing, int):
208 # sys.version_info[i] <compare_op> k
209 if 0 <= index <= 1:
210 return fixed_comparison(pyversion[index], op, thing)
211 else:
212 return TRUTH_VALUE_UNKNOWN
213 elif isinstance(index, tuple) and isinstance(thing, tuple):
214 lo, hi = index
215 if lo is None:
216 lo = 0
217 if hi is None:
218 hi = 2
219 if 0 <= lo < hi <= 2:
220 val = pyversion[lo:hi]
221 if len(val) == len(thing) or len(val) > len(thing) and op not in ("==", "!="):
222 return fixed_comparison(val, op, thing)
223 return TRUTH_VALUE_UNKNOWN
224
225
226def consider_sys_platform(expr: Expression, platform: str) -> int:

Callers 1

infer_condition_valueFunction · 0.85

Calls 5

isinstanceFunction · 0.85
lenFunction · 0.85
fixed_comparisonFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…