Skip to content

Commit 27f6876

Browse files
committed
fix: typeerror
1 parent 199fbb7 commit 27f6876

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

fgpyo/util/metric.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,17 @@ def asdict(metric: Metric) -> Dict[str, Any]:
391391
392392
Returns:
393393
A dictionary representation of the given metric.
394+
395+
Raises:
396+
TypeError: If the given metric is not an instance of a `dataclass` or `attr.s`-decorated
397+
Metric.
394398
"""
395399
if _is_dataclass_instance(metric):
396400
return dataclasses.asdict(metric)
397401
elif _is_attrs_instance(metric):
398402
return attr.asdict(metric)
399403
else:
400-
assert False, "Unreachable"
404+
raise TypeError(
405+
"The provided metric is not an instance of a `dataclass` or `attr.s`-decorated Metric "
406+
f"class: {metric.__class__}"
407+
)

fgpyo/util/tests/test_metric.py

+15
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,18 @@ def test_asdict(data_and_classes: DataBuilder) -> None:
553553
"""Test that asdict works as expected on both dataclass and attr.s decoreated metrics."""
554554

555555
assert asdict(data_and_classes.Person(name="name", age=42)) == {"name": "name", "age": 42}
556+
557+
558+
def test_asdict_raises() -> None:
559+
"""Test that we raise a TypeError when asdict is called on a non-metric class."""
560+
561+
class UndecoratedMetric(Metric["UndecoratedMetric"]):
562+
foo: int
563+
bar: str
564+
565+
def __init__(self, foo: int, bar: str):
566+
self.foo = foo
567+
self.bar = bar
568+
569+
with pytest.raises(TypeError, match="The provided metric is not an instance"):
570+
asdict(UndecoratedMetric(foo=1, bar="a"))

0 commit comments

Comments
 (0)