fix(test_tools): Asserting unlabeled metrics in assert_metric results in AttributeError#224
Merged
Merged
Conversation
`MetricWrapperBase.clear()` only works on parent metrics — the ones declared with labels. Calling it on an unlabeled metric raises AttributeError because `_lock` is only set on parents. `assert_metric_impl` iterates every collector in the registry and calls `clear()`, so any unlabeled metric registered in the test session would crash the fixture. Reset unlabeled metrics' observable state via `_metric_init` instead, which recreates `_value` (Counter) / `_buckets`+`_sum` (Histogram) / etc. fresh. beep boop
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #224 +/- ##
=======================================
Coverage 97.38% 97.38%
=======================================
Files 105 105
Lines 4586 4595 +9
=======================================
+ Hits 4466 4475 +9
Misses 120 120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
assert_metricassert_metric results in AttributeError
Zaimwa9
approved these changes
May 11, 2026
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.
MetricWrapperBase.clear()only works on parent metrics (those declared with labels). Calling it on an unlabeled metric raisesAttributeErrorbecause_lockis only initialised on parents.assert_metric_impliterates every collector in the registry and callsclear()to reset state between tests, so any unlabeled metric registered in the test session would crash the fixture for downstream consumers.This PR resets unlabeled metrics' observable state via
_metric_initinstead, which recreates_value(Counter) /_buckets+_sum(Histogram) etc. fresh.Test plan
test_assert_metrics__unlabeled_metric_incremented__asserts_expectedexercises an unlabeled counter throughassert_metric. Verified failing onmainand passing on this branch.test_assert_metrics__metric_incremented__asserts_expectedandtest_assert_metrics__after_registry_reset__raises_assertionstill pass.