Skip to content

Commit d9a1181

Browse files
mstorahulkaushal04
andauthored
refactor: Improve error messages in CounterPivotTable and DelimitedList (#27)
Include annotation types and quote field names in error messages for easier debugging. Co-authored-by: Rahul Kaushal <kaushalrahul15@gmail.com>
1 parent 64837d2 commit d9a1181

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

fgmetric/collections/_counter_pivot_table.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def _get_counter_fieldname(cls) -> str | None:
7070
if len(counter_fieldnames) > 1:
7171
raise TypeError(
7272
"Only one Counter per model is currently supported. "
73-
f"Found multiple fields with Counter types: {', '.join(counter_fieldnames)}"
73+
f"Found multiple Counter fields: {', '.join(counter_fieldnames)}"
7474
)
7575

7676
counter_fieldname: str | None = counter_fieldnames[0] if counter_fieldnames else None
7777

7878
if counter_fieldname:
7979
counter_field_info: FieldInfo = cls.model_fields[counter_fieldname]
8080
if is_optional(counter_field_info.annotation):
81-
raise TypeError(f"Optional Counters are not supported: {counter_fieldname}")
81+
raise TypeError(f"Optional Counter fields are not supported: '{counter_fieldname}'")
8282

8383
return counter_fieldname
8484

@@ -109,7 +109,8 @@ def _get_counter_enum(cls) -> type[StrEnum] | None: # noqa: C901
109109
enum_cls: type[StrEnum] = args[0]
110110
else:
111111
raise TypeError(
112-
f"Counter fields must have a StrEnum type parameter: {cls._counter_fieldname}"
112+
f"Counter fields must have a StrEnum type parameter,"
113+
f" got {info.annotation} for field '{cls._counter_fieldname}'"
113114
)
114115

115116
return enum_cls

fgmetric/collections/_delimited_list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def _require_single_character_collection_delimiter(cls) -> None:
101101
"""Require collection delimiters to be single characters."""
102102
if len(cls.collection_delimiter) != 1:
103103
raise ValueError(
104-
f"Class collection delimiter must be a single character: {cls.collection_delimiter}"
104+
"collection_delimiter must be a single character,"
105+
f" got: {cls.collection_delimiter!r}"
105106
)
106107

107108
@final

tests/test_collections.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ class FakeMetric(Metric):
258258
name: str
259259
counts: Counter[str]
260260

261-
assert str(excinfo.value) == "Counter fields must have a StrEnum type parameter: counts"
261+
assert str(excinfo.value) == (
262+
"Counter fields must have a StrEnum type parameter,"
263+
" got collections.Counter[str] for field 'counts'"
264+
)
262265

263266

264267
def test_counter_pivot_table_raises_if_multiple_counters() -> None:
@@ -280,7 +283,7 @@ class FakeMetric(Metric):
280283

281284
assert str(excinfo.value) == (
282285
"Only one Counter per model is currently supported. "
283-
"Found multiple fields with Counter types: foo_counts, bar_counts"
286+
"Found multiple Counter fields: foo_counts, bar_counts"
284287
)
285288

286289

@@ -296,4 +299,4 @@ class FakeMetric(Metric):
296299
name: str
297300
counts: Counter[FakeEnum] | None
298301

299-
assert str(excinfo.value) == "Optional Counters are not supported: counts"
302+
assert str(excinfo.value) == "Optional Counter fields are not supported: 'counts'"

0 commit comments

Comments
 (0)