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

Function consider_sys_platform

mypy/reachability.py:226–262  ·  view source on GitHub ↗

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

(expr: Expression, platform: str)

Source from the content-addressed store, hash-verified

224
225
226def consider_sys_platform(expr: Expression, platform: str) -> int:
227 """Consider whether expr is a comparison involving sys.platform.
228
229 Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
230 """
231 # Cases supported:
232 # - sys.platform == 'linux'
233 # - sys.platform != 'win32'
234 # - sys.platform.startswith('win')
235 if isinstance(expr, ComparisonExpr):
236 # Let's not yet support chained comparisons.
237 if len(expr.operators) > 1:
238 return TRUTH_VALUE_UNKNOWN
239 op = expr.operators[0]
240 if op not in ("==", "!="):
241 return TRUTH_VALUE_UNKNOWN
242 if not is_sys_attr(expr.operands[0], "platform"):
243 return TRUTH_VALUE_UNKNOWN
244 right = expr.operands[1]
245 if not isinstance(right, StrExpr):
246 return TRUTH_VALUE_UNKNOWN
247 return fixed_comparison(platform, op, right.value)
248 elif isinstance(expr, CallExpr):
249 if not isinstance(expr.callee, MemberExpr):
250 return TRUTH_VALUE_UNKNOWN
251 if len(expr.args) != 1 or not isinstance(expr.args[0], StrExpr):
252 return TRUTH_VALUE_UNKNOWN
253 if not is_sys_attr(expr.callee.expr, "platform"):
254 return TRUTH_VALUE_UNKNOWN
255 if expr.callee.name != "startswith":
256 return TRUTH_VALUE_UNKNOWN
257 if platform.startswith(expr.args[0].value):
258 return ALWAYS_TRUE
259 else:
260 return ALWAYS_FALSE
261 else:
262 return TRUTH_VALUE_UNKNOWN
263
264
265Targ = TypeVar("Targ", int, str, tuple[int, ...])

Callers 1

infer_condition_valueFunction · 0.85

Calls 5

isinstanceFunction · 0.85
lenFunction · 0.85
is_sys_attrFunction · 0.85
fixed_comparisonFunction · 0.85
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…