code: def test_error_on_too_few_samples(metric_class=R2Score):
      """Test that error is raised if too few samples are provided."""
      metric = metric_class()
      with pytest.raises(
          ValueError,
          match="Needs at least two samples to calculate r2 score.",
      ):
          metric(torch.randn(1), torch.randn(1))
      metric.reset()
  
      # calling update twice should still work
      metric.update(torch.randn(1), torch.randn(1))
      metric.update(torch.randn(1), torch.randn(1))
      assert metric.compute()
  
imports: from functools import partial
  import pytest
  import torch
  from sklearn.metrics import r2_score
  from torchmetrics.functional import r2_score
  from torchmetrics.regression import R2Score
  from unittests import BATCH_SIZE, NUM_BATCHES, _Input
  from unittests.helpers import seed_all
  from unittests.helpers.testers import MetricTester
label_desc: Use image node_id 5 for calling a global function with example usage: test_error_on_too_few_samples(metric_class) without return types