simpleeval icon indicating copy to clipboard operation
simpleeval copied to clipboard

Feature request: provide support for DictComp

Open Reskov opened this issue 5 years ago • 5 comments

Such code is not support by EvalWithCompoundTypes

{i:i for i in range(3)}

Basically, it is should be easy to support such functionality with something like that https://github.com/Reskov/simpleeval/commit/695bb684fff24af7eab47505f8438261911f109b

I can do more tests and prepare PR if you agree in general

Reskov avatar Jun 11 '19 10:06 Reskov

I guess this is another example, trying to evaluate something like {**a, **b}:

>>> names = dict(a=dict(x=1, y=2), b=dict(z=3))
>>> EvalWithCompoundTypes(names=names).eval('{**a, **b}')
Traceback (most recent call last):
  File "/Users/psurry/Downloads/simpleeval.py", line 395, in _eval
    handler = self.nodes[type(node)]
KeyError: <class 'NoneType'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/psurry/Downloads/simpleeval.py", line 616, in eval
    return super(EvalWithCompoundTypes, self).eval(expr)
  File "/Users/psurry/Downloads/simpleeval.py", line 389, in eval
    return self._eval(ast.parse(expr.strip()).body[0])
  File "/Users/psurry/Downloads/simpleeval.py", line 401, in _eval
    return handler(node)
  File "/Users/psurry/Downloads/simpleeval.py", line 404, in _eval_expr
    return self._eval(node.value)
  File "/Users/psurry/Downloads/simpleeval.py", line 401, in _eval
    return handler(node)
  File "/Users/psurry/Downloads/simpleeval.py", line 619, in _eval_dict
    return {self._eval(k): self._eval(v) for (k, v) in zip(node.keys, node.values)}
  File "/Users/psurry/Downloads/simpleeval.py", line 619, in <dictcomp>
    return {self._eval(k): self._eval(v) for (k, v) in zip(node.keys, node.values)}
  File "/Users/psurry/Downloads/simpleeval.py", line 398, in _eval
    "Sorry, {0} is not available in this " "evaluator".format(type(node).__name__)
simpleeval.FeatureNotAvailable: Sorry, NoneType is not available in this evaluator
>>> 

patricksurry avatar Feb 16 '22 16:02 patricksurry

I used a workaround by evaluating something like this dict(list(a.items()) + list(b.items())). Looking at the AST parse:

>>> ast.dump(ast.parse('{**a}'))
"Module(body=[Expr(value=Dict(keys=[None], values=[Name(id='a', ctx=Load())]))])"

I guess it's not handling the keys=[None] piece? Adding an explicit key doesn't help:

>>> ast.dump(ast.parse('{"a": 1, **a}'))
"Module(body=[Expr(value=Dict(keys=[Str(s='a'), None], values=[Num(n=1), Name(id='a', ctx=Load())]))])"
>>> 

I guess this would be fixed by either tweaking _eval_dict to handle injection of a dictionary in v when k was None? See the ast docs where it explains: "When doing dictionary unpacking using dictionary literals the expression to be expanded goes in the values list, with a None at the corresponding position in keys."

Perhaps this is a different bug that the originally reported one?

patricksurry avatar Feb 16 '22 16:02 patricksurry

This would be really cool to add

danthedeckie avatar Feb 17 '23 09:02 danthedeckie

+1 for this feature request. DictComp is really a missing thing for this cool library. @Reskov could you prepare a PR? As I see in the previous comment @danthedeckie is ready to accept it (please correct me if I'm wrong)

amyasnikov avatar Apr 15 '23 22:04 amyasnikov

@amyasnikov Not sure. Personally, I am not interested in this anymore. 😞 And there was already PR #62.

Reskov avatar Apr 17 '23 07:04 Reskov