Repository navigation
fix(report): give each legacy metric dependency its own result - #2002
Open
sarankumar1325 wants to merge 1 commit into
Open
sarankumar1325 wants to merge 1 commit into
sarankumar1325 wants to merge 1 commit into
Conversation
Context.get_legacy_metric set get_result on every dependency with `lambda: result`, which closes over the loop variable. All dependencies (and later the metric itself) therefore returned whatever `result` held when the getter was called, i.e. the last dependency's result. A legacy metric with two or more dependencies read the same value from each. Bind the result when creating the getter, and add a regression test. Signed-off-by: sarankumar1325 <[email protected]>
atishay-kasliwal
left a comment
There was a problem hiding this comment.
The new test fails on master with assert 22 == 12: both dependencies' get_result returned the last result, because the lambdas shared the result variable. It passes here.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Context.get_legacy_metric(src/evidently/core/report.py) hands each dependency its result withobject.__setattr__(obj, "get_result", lambda: result)inside aforloop. The lambda closes over the variableresult, not its value, so every dependency'sget_result()returns whateverresultholds when it is called. That is the last dependency's result. A legacy metric with two or more dependencies therefore reads the same value from all of them.Found with ruff
B023(function definition does not bind loop variableresult).Repro
A legacy metric
Pair(first=Leaf(1), second=Leaf(2))that computesfirst.get_result().value * 10 + second.get_result().value:2212Fix
A small
_result_getter(result)helper creates the getter so each one keeps its own result. I used it for the metric's own getter too, for consistency.Tests
tests/future/report/test_legacy_metric_dependencies.pyhas the two-dependency case above. It fails onmainand passes with the fix.pytest tests/future/report tests/future/metrics tests/report: 1427 passed (Python 3.12, pandas<3).ruff==0.3.7check and format pass on the changed files.I didn't open an issue first because this is a small bug fix with a regression test. I found no existing issue or PR about it.
This PR was prepared with AI assistance. I reviewed the change and ran the tests above.