File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -391,10 +391,17 @@ def asdict(metric: Metric) -> Dict[str, Any]:
391
391
392
392
Returns:
393
393
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.
394
398
"""
395
399
if _is_dataclass_instance (metric ):
396
400
return dataclasses .asdict (metric )
397
401
elif _is_attrs_instance (metric ):
398
402
return attr .asdict (metric )
399
403
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
+ )
Original file line number Diff line number Diff line change @@ -553,3 +553,18 @@ def test_asdict(data_and_classes: DataBuilder) -> None:
553
553
"""Test that asdict works as expected on both dataclass and attr.s decoreated metrics."""
554
554
555
555
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" ))
You can’t perform that action at this time.
0 commit comments