Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Add parsed value in error message #14

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions magicparse/type_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def apply(self, value: str) -> int:
try:
return int(value)
except:
raise ValueError("value is not a valid integer")
raise ValueError(f"value '{value}' is not a valid integer")

@staticmethod
def key() -> str:
Expand All @@ -44,7 +44,7 @@ def apply(self, value: str) -> Decimal:
try:
return Decimal(value)
except:
raise ValueError("value is not a valid decimal")
raise ValueError(f"value '{value}' is not a valid decimal")

@staticmethod
def key() -> str:
Expand All @@ -56,10 +56,10 @@ def apply(self, value: str) -> time:
try:
parsed = time.fromisoformat(value)
if parsed.tzinfo is None:
raise ValueError("value is a naïve time reprensentation")
raise ValueError(f"value '{value}' is a naïve time reprensentation")
return parsed
except:
raise ValueError("value is not a valid time representation")
raise ValueError(f"value '{value}' is not a valid time representation")

@staticmethod
def key() -> str:
Expand All @@ -71,10 +71,10 @@ def apply(self, value: str) -> datetime:
try:
parsed = datetime.fromisoformat(value)
if parsed.tzinfo is None:
raise ValueError("value is a naïve datetime reprensentation")
raise ValueError(f"value '{value}' is a naïve datetime reprensentation")
return parsed
except:
raise ValueError("value is not a valid datetime representation")
raise ValueError(f"value '{value}' is not a valid datetime representation")

@staticmethod
def key() -> str:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def test_csv_error_format():
field = CsvField({"key": "ratio", "type": "decimal", "column-number": 1})

with pytest.raises(ValueError) as error:
field.read_value(["hello"])
field.read_value("hello")

assert field.error(error.value) == {
"column-number": 1,
"error": "value is not a valid decimal",
"error": "value 'h' is not a valid decimal",
"field-key": "ratio",
}

Expand All @@ -78,11 +78,11 @@ def test_columnar_error_format():
)

with pytest.raises(ValueError) as error:
field.read_value(["hello"])
field.read_value("hello")

assert field.error(error.value) == {
"column-start": 0,
"column-length": 5,
"error": "value is not a valid decimal",
"error": "value 'hello' is not a valid decimal",
"field-key": "ratio",
}
8 changes: 4 additions & 4 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_error_display_row_number(self):
"row-number": 1,
"column-number": 1,
"field-key": "age",
"error": "value is not a valid integer",
"error": "value 'a' is not a valid integer",
}
]

Expand All @@ -125,7 +125,7 @@ def test_errors_do_not_halt_parsing(self):
"row-number": 2,
"column-number": 1,
"field-key": "age",
"error": "value is not a valid integer",
"error": "value 'a' is not a valid integer",
}
]

Expand Down Expand Up @@ -190,7 +190,7 @@ def test_error_display_row_number(self):
"column-start": 0,
"column-length": 1,
"field-key": "age",
"error": "value is not a valid integer",
"error": "value 'a' is not a valid integer",
}
]

Expand All @@ -211,7 +211,7 @@ def test_errors_do_not_halt_parsing(self):
"column-start": 0,
"column-length": 1,
"field-key": "age",
"error": "value is not a valid integer",
"error": "value 'a' is not a valid integer",
}
]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_type_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_apply(self):
def test_apply_failed(self):
type_converter = TypeConverter.build({"type": "int"})

with pytest.raises(ValueError, match="value is not a valid integer"):
with pytest.raises(ValueError, match="value 'abc' is not a valid integer"):
type_converter.apply("abc")


Expand All @@ -71,7 +71,7 @@ def test_apply(self):
def test_apply_failed(self):
type_converter = TypeConverter.build({"type": "decimal"})

with pytest.raises(ValueError, match="value is not a valid decimal"):
with pytest.raises(ValueError, match="value 'abc' is not a valid decimal"):
type_converter.apply("abc")


Expand Down
Loading