asteval icon indicating copy to clipboard operation
asteval copied to clipboard

Variable unpacking does not work as expected

Open elmer-le opened this issue 1 year ago • 1 comments
trafficstars

I have the following output with 0.9.32:

>>> import asteval
>>> ev = asteval.Interpreter()
>>> ev('[(x,y) for (x,y) in [(1,2), (3,4)]]') 
[(1, 2), (1, 4), (3, 2), (3, 4)]
>>> [(x,y) for (x,y) in [(1,2), (3,4)]] # actual python output
[(1, 2), (3, 4)]

It seems like unpacking in asteval is taking the cartesian product of x and y.

elmer-le avatar Apr 01 '24 18:04 elmer-le

@elmer-le Thanks -- that's bad! I think it will be fixable, but I am not certain when I'll get to this.

newville avatar Apr 01 '24 20:04 newville

I just ran into the same issue and tracked it down to being introduced in 0.9.28. Here's another reproduction:

import asteval
a=asteval.Interpreter()
data={'1': 123, '2': 456, '3': 789}
a.symtable['data']=data
print(a("multiplier = {'1': 3, '2': 2, '3': 1};sum([v*multiplier[k] for k, v in data.items()])")) # Returns 8208
multiplier = {'1': 3, '2': 2, '3': 1}
print(sum([v*multiplier[k] for k, v in data.items()])) # Returns 2070

chrisjbremner avatar May 15 '24 21:05 chrisjbremner

@elmer-le @chrisjbremner These are now fixed in the master branch.

newville avatar May 19 '24 04:05 newville

Thanks! Confirmed it worked for my use case. Looking forward to the next release so I can incorporate it!

chrisjbremner avatar May 20 '24 02:05 chrisjbremner

@chrisjbremner Thanks - I'll close this.

newville avatar May 20 '24 19:05 newville

Thanks, do you anticipate a release soon that incorporates this change?

chrisjbremner avatar May 21 '24 20:05 chrisjbremner

@chrisjbremner Yes, that is on my list, hopefully by the end of the week.

newville avatar May 21 '24 21:05 newville

@chrisjbremner 0.9.33 is now released and pushed to PyPI.

newville avatar May 23 '24 15:05 newville