-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
on_compare not properly handling non-boolean values #131
Comments
@OliverCWY Um, your example never gets to the comparison. It raises a NameError:
Yup: If you still think there is a problem, post actual working code that actually shows the problem, and the full traceback. |
Sorry, I forgot to pass the symbol table when modifying my code. import polars as pl
from asteval import Interpreter
aeval = Interpreter({"pl": pl})
aeval("pl.col('a') > 1") and the traceback:
|
@OliverCWY Indeed, from Python:
Asteval just raises this exception more aggressively (at the first "Compare" instead of at "If"). But if you do (in Python):
That will raise the same kind of TypeError exception. It sort of seems like you would want to follow I do not have much experience with What would you expect to happen? |
Apologies for not explaining the use case. If you simply run Following the previous snippet: expr = pl.col('a') > 1 # works fine
expr = aeval("pl.col('a') > 1") # fails |
@OliverCWY Thanks -- that helps. Yeah, we do use a special case there that maybe should be relaxed. As with this example (but others, notably numpy), The challenge is that Comparisons may have multiple operators: And indeed,
raises the same TypeError exception. The result of A similar case is
anyway, I think we can fix this so it better matches Python behavior. |
@newville Yes, I have read the source codes and understand the reasoning. I think in the try-except block, any error would come from converting |
@OliverCWY Yeah, I agree with that. And maybe for the case of a single comparison, we should just return the result. without testing "true-ness" That would still fail on the "If" and behave more like Python. Looking into it... |
@OliverCWY OK, I think this should be fixed (that is, "match Python") in the master branch with 7e2050d |
Thanks a lot for this great project. I will close this issue. |
In some libraries (such as polars), the
__bool__
methods do not raiseValueError
(e.g. polars raisesTypeError
). This causes the try-except blockto raise the uncaught
TypeError
.Example code snippet that demonstrates the above:
I assume that any exceptions in the
try
block would come from the__bool__
method and thus it would be safe to catch all types of error?The text was updated successfully, but these errors were encountered: