Skip to content

Commit

Permalink
feat: enhance exception message on conversion error (#2)
Browse files Browse the repository at this point in the history
* feat: enhance exception message on conversion error

* chore: bump to 0.2.0
  • Loading branch information
clintval authored Nov 8, 2024
1 parent 666cc68 commit ff9e447
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "typeline"
version = "0.1.0"
version = "0.2.0"
description = "Write dataclasses to delimited text formats and read them back again."
authors = ["Clint Valentine <[email protected]>"]
license = "MIT"
Expand Down
24 changes: 24 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import get_origin

import pytest
from msgspec import ValidationError
from typing_extensions import override

from typeline import CsvStructReader
Expand Down Expand Up @@ -259,3 +260,26 @@ def _decode(record_type: type[Any] | str | Any, item: Any) -> Any:

with SimpleListReader.from_path(tmp_path / "test.txt", MyMetric) as reader:
assert list(reader) == [MyMetric(0.1, [1, 2, 3])]


def test_reader_msgspec_validation_exception(tmp_path: Path) -> None:
"""Test that we clarify when msgspec cannot decode a structure of builtins."""

@dataclass
class MyData:
field1: str
field2: list[int]

(tmp_path / "test.txt").write_text("field1,field2\nmy-name,null\n")

with CsvStructReader.from_path(tmp_path / "test.txt", MyData) as reader:
with pytest.raises(
ValidationError,
match=(
r"Could not parse JSON-like object into requested structure:"
+ r" \{'field1'\: 'my-name', 'field2': None\}."
+ r" Requested structure: MyData. Original exception:"
+ r" Expected \`array\`, got \`null\` - at \`\$.field2\`"
),
):
list(reader)
11 changes: 6 additions & 5 deletions typeline/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import get_args
from typing import get_origin

from msgspec import ValidationError
from msgspec import convert
from typing_extensions import Self
from typing_extensions import override
Expand Down Expand Up @@ -164,11 +165,11 @@ def __iter__(self) -> Iterator[RecordType]:
as_builtins = self._csv_dict_to_json(record)
try:
yield convert(as_builtins, self._record_type, strict=False)
except ValueError as exception:
raise ValueError(
f"Could not parse {record} as {self._record_type.__name__}!"
+ f" Intermediate structure formed is: {as_builtins}."
+ f" Original error: {exception}"
except ValidationError as exception:
raise ValidationError(
f"Could not parse JSON-like object into requested structure: {as_builtins}."
+ f" Requested structure: {self._record_type.__name__}."
+ f" Original exception: {exception}"
) from exception

@staticmethod
Expand Down

0 comments on commit ff9e447

Please sign in to comment.