Skip to content

Commit

Permalink
Prevent iteration over metrics (#1320)
Browse files Browse the repository at this point in the history
* fix

* changelog

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

(cherry picked from commit 00bb1ed)
  • Loading branch information
SkafteNicki authored and Borda committed Nov 16, 2022
1 parent eb5f701 commit d992875
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

**Note: we move fast, but still we preserve 0.1 version (one feature release) back compatibility.**

## [unReleased] - 2022-MM-DD

### Fixed

- Fixed bug in `Metrictracker.best_metric` when `return_step=False` ([#1306](https://github.com/Lightning-AI/metrics/pull/1306))


- Fixed bug to prevent users from going into a infinite loop if trying to iterate of a single metric ([#1320](https://github.com/Lightning-AI/metrics/pull/1320))


- Fixed bug in `Metrictracker.best_metric` when `return_step=False` ([#1306](https://github.com/Lightning-AI/metrics/pull/1306))


## [0.10.2] - 2022-10-31

Expand All @@ -21,9 +33,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed restrictive dtype checking in `spearman_corrcoef` when used with autocast ([#1303](https://github.com/Lightning-AI/metrics/pull/1303))


- Fixed bug in `Metrictracker.best_metric` when `return_step=False` ([#1306](https://github.com/Lightning-AI/metrics/pull/1306))


## [0.10.1] - 2022-10-21

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions src/torchmetrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,9 @@ def __getitem__(self, idx: int) -> "Metric":
def __getnewargs__(self) -> Tuple:
return (Metric.__str__(self),)

def __iter__(self):
raise NotImplementedError("Metrics does not support iteration.")


def _neg(x: Tensor) -> Tensor:
return -torch.abs(x)
Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/bases/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,10 @@ def forward(self, *args, **kwargs):
match="Torchmetrics v0.9 introduced a new argument class property called.*",
):
UnsetProperty()


def test_no_iteration_allowed():
metric = DummyMetric()
with pytest.raises(NotImplementedError, match="Metrics does not support iteration."):
for m in metric:
continue

0 comments on commit d992875

Please sign in to comment.