From 63e876c1212c0dfa4ae3a80e28d45d9fca3ea8dc Mon Sep 17 00:00:00 2001 From: vrey Date: Tue, 21 Feb 2023 19:39:05 +0100 Subject: [PATCH 1/2] Fix Metric.__iter__ by setting it to None (fixes #1536) --- src/torchmetrics/metric.py | 4 +--- tests/unittests/bases/test_metric.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/torchmetrics/metric.py b/src/torchmetrics/metric.py index 09fbfea3c35..bae38344bde 100644 --- a/src/torchmetrics/metric.py +++ b/src/torchmetrics/metric.py @@ -920,9 +920,7 @@ def __getnewargs__(self) -> Tuple: """Needede method for construction of new metrics __new__ method.""" return (Metric.__str__(self),) - def __iter__(self): - """Iteration over metrics are not allowed. Use metric collections for nesting metrics.""" - raise NotImplementedError("Metrics does not support iteration.") + __iter__ = None def _neg(x: Tensor) -> Tensor: diff --git a/tests/unittests/bases/test_metric.py b/tests/unittests/bases/test_metric.py index b7f0c20ea9b..d111661df59 100644 --- a/tests/unittests/bases/test_metric.py +++ b/tests/unittests/bases/test_metric.py @@ -468,7 +468,7 @@ def test_custom_availability_check_and_sync_fn(): def test_no_iteration_allowed(): metric = DummyMetric() - with pytest.raises(NotImplementedError, match="Metrics does not support iteration."): # noqa: PT012 + with pytest.raises(TypeError, match="'DummyMetric' object is not iterable"): # noqa: PT012 for m in metric: continue From 07da8a36af2cf26977b105b8bf81d6cf02209731 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Detlefsen Date: Wed, 22 Feb 2023 10:38:00 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02f837709e4..af15c429c3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Extend `EnumStr` raising `ValueError` for invalid value ([#1479](https://github.com/Lightning-AI/metrics/pull/1479)) +- Changed `__iter__` method from raising `NotImplementedError` to `TypeError` by setting to `None` ([#1538](https://github.com/Lightning-AI/metrics/pull/1538)) + ### Deprecated - @@ -64,7 +66,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed `multilabel` in `ExactMatch` ([#1474](https://github.com/Lightning-AI/metrics/pull/1474)) - ## [0.11.1] - 2023-01-30 ### Fixed