amplify-education / python-hcl2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong Arithmetic Parse

soasme opened this issue · comments

commented

Issue

>>> from hcl2 import loads
>>> loads('a=1*(2+3)')
{'a': ['${1 * 2 + 3}']}

Expect

{'a': ['${1*(2+3)}']}
commented

In case it helps somebody, I fixed this issue by adding the parenthesis to the transformer, whenever an expression is found:

from hcl2.parser import hcl2 as parser
from hcl2.transformer import DictTransformer

class DictTransformerMod(DictTransformer):

    def expr_term(self, args: list) -> object:
        term = super().expr_term(args)
        if isinstance(term, str) and " " in term:
            return f"({term})"
        return term

transformer = DictTransformerMod()

tree = parser.parse('a=1*(2+3)')
transformer.transform(tree)

, which returns the expected:

{'a': '${1 * (2 + 3)}'}

I won't do a PR because I know too little about how this package is used to understand possible side effects