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)
| 272 | |
| 273 | |
| 274 | def 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 | |
| 298 | def bytes_from_str(value: str) -> bytes: |
no test coverage detected
searching dependent graphs…