Skip to content

Commit

Permalink
Exclude special attributes from causing MetricsLambda creation (#3263)
Browse files Browse the repository at this point in the history
* Fix the problem

* Add a test
  • Loading branch information
sadra-barikbin authored Jul 17, 2024
1 parent 6b6b169 commit edd5025
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ignite/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ def __floordiv__(self, other: Any) -> "MetricsLambda":
def __getattr__(self, attr: str) -> Callable:
from ignite.metrics.metrics_lambda import MetricsLambda

if attr.startswith("__") and attr.endswith("__"):
return object.__getattribute__(self, attr)

def fn(x: Metric, *args: Any, **kwargs: Any) -> Any:
return getattr(x, attr)(*args, **kwargs)

Expand Down
22 changes: 22 additions & 0 deletions tests/ignite/metrics/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,3 +1446,25 @@ def test_skip_unrolling():
state = State(output=(y_pred, y_true))
engine = MagicMock(state=state)
metric.iteration_completed(engine)


class DummyMetric6(Metric):
def reset(self):
pass

def compute(self):
pass

def update(self, output):
pass

def __call__(self, value):
pass


def test_access_to_metric_dunder_attributes():
metric = DummyMetric6()
import inspect

# `inspect.signature` accesses `__signature__` attribute of the metric.
assert "value" in inspect.signature(metric).parameters.keys()

0 comments on commit edd5025

Please sign in to comment.