Skip to content

fix(report): give each legacy metric dependency its own result - #2002

Open
sarankumar1325 wants to merge 1 commit into
evidentlyai:mainfrom
sarankumar1325:fix/legacy-metric-dependency-results
Open

sarankumar1325 wants to merge 1 commit into
evidentlyai:mainfrom
sarankumar1325:fix/legacy-metric-dependency-results

Conversation

@sarankumar1325

Copy link
Copy Markdown

What

Context.get_legacy_metric (src/evidently/core/report.py) hands each dependency its result with object.__setattr__(obj, "get_result", lambda: result) inside a for loop. The lambda closes over the variable result, not its value, so every dependency's get_result() returns whatever result holds 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 variable result).

Repro

A legacy metric Pair(first=Leaf(1), second=Leaf(2)) that computes first.get_result().value * 10 + second.get_result().value:

  • before: 22
  • after: 12

Fix

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

  • New tests/future/report/test_legacy_metric_dependencies.py has the two-dependency case above. It fails on main and passes with the fix.
  • pytest tests/future/report tests/future/metrics tests/report: 1427 passed (Python 3.12, pandas<3).
  • ruff==0.3.7 check 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.

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 atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants