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

Function is_constant

mypyc/irbuild/util.py:274–295  ·  view source on GitHub ↗

Check whether we allow an expression to appear as a default value. We don't currently properly support storing the evaluated values for default arguments and default attribute values, so we restrict what expressions we allow. We allow literals of primitives types, None, and referen

(e: Expression)

Source from the content-addressed store, hash-verified

272
273
274def is_constant(e: Expression) -> bool:
275 """Check whether we allow an expression to appear as a default value.
276
277 We don't currently properly support storing the evaluated
278 values for default arguments and default attribute values, so
279 we restrict what expressions we allow. We allow literals of
280 primitives types, None, and references to Final global
281 variables.
282 """
283 return (
284 isinstance(e, (StrExpr, BytesExpr, IntExpr, FloatExpr))
285 or (isinstance(e, UnaryExpr) and e.op == "-" and isinstance(e.expr, (IntExpr, FloatExpr)))
286 or (isinstance(e, TupleExpr) and all(is_constant(e) for e in e.items))
287 or (
288 isinstance(e, RefExpr)
289 and e.kind == GDEF
290 and (
291 e.fullname in ("builtins.True", "builtins.False", "builtins.None")
292 or (isinstance(e.node, Var) and e.node.is_final)
293 )
294 )
295 )
296
297
298def bytes_from_str(value: str) -> bytes:

Callers 3

get_defaultFunction · 0.90
calculate_arg_defaultsFunction · 0.90

Calls 2

isinstanceFunction · 0.85
allFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…