Instagram / Instagram/LibCST

[Feature Request] evaluated_value() method on [static] containers

Open
#878 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

[Integer.evaluated_value](https://libcst.readthedocs.io/en/latest/nodes.html#libcst.Integer.evaluated_value) exists, so why not also a Tuple.evaluated_value, Dict.evaluated_value, etc?

Background:
I just cobbled together this mess of a function:
```python
def cst_literal_eval(node: cst.BaseExpression) -> Any:
ast_node = cst.Module([cst.SimpleStatementLine([cst.Expr(node)])]).code
try:
return ast.literal_eval(ast_node)
except Exception:
return None
```
In flake8-trio, for seeing if `for i in range(...)` can be statically determined to run at least once, and would be great to just be able to call `.evaluated_value` on the node directly.
(`.code` on something other than `Module` would also be neat, but that might be tricky).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.