fix: Python custom justifications with Decimal scores #1344
+56
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The translator automatically converts the result of Java method calls on Java objects into the corresponding Python types. This is normally wanted, since it allow users to use the result of ConstraintCollectors (which are Java objects).
However,
to_python_score
does not want this; it expects the normal Java result. It is typically called in an untranslated context and thus usually does not run into this issue. However, when a custom justification function is used, it is called in a translation context. For normal scores, it works, since we don't access any properties of the long when creating the corresponding Python score. For decimal scores, we call toPlainString() so we can create a Python Decimal instance from a Java BigDecimal. Python Decimal does not have a toPlainString method normally, so it causes an attribute lookup failure at runtime.The fix is to add a toPlainString method to PythonDecimal, that just delegate to the toPlainString method of its value.