diff --git a/.github/workflows/pullrequest-push.yml b/.github/workflows/pullrequest-push.yml index 316c38e..827b92a 100644 --- a/.github/workflows/pullrequest-push.yml +++ b/.github/workflows/pullrequest-push.yml @@ -31,6 +31,7 @@ jobs: install: true install_args: python poetry cache: true + version: 2026.9.2 - name: Install Python dependencies uses: ZeroGachis/.github/.github/actions/poetry-install@v6 with: diff --git a/magicparse/fields.py b/magicparse/fields.py index 44613f2..8687666 100644 --- a/magicparse/fields.py +++ b/magicparse/fields.py @@ -18,6 +18,7 @@ def __init__(self, key: str, options: dict[str, Any]) -> None: post_processors = [PostProcessor.build(item) for item in options.get("post-processors", [])] self.optional = options.get("optional", False) + self.on_type_error = type_converter.on_error self.transforms = pre_processors + [type_converter] + validators + post_processors @@ -25,6 +26,8 @@ def _process_raw_value(self, raw_value: str) -> Result: if not raw_value: if self.optional: return Ok(value=None) + elif self.on_type_error == OnError.SKIP_ROW: + return SkipRow(ValueError(f"{self.key} field is required but the value was empty")) else: raise ValueError(f"{self.key} field is required but the value was empty") for transform in self.transforms: diff --git a/tests/test_fields.py b/tests/test_fields.py index d10b760..73cda10 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -2,11 +2,12 @@ from typing import Any import pytest -from magicparse.transform import Ok -from magicparse.type_converters import DecimalConverter, StrConverter + from magicparse.fields import ColumnarField, CsvField, Field from magicparse.post_processors import Divide from magicparse.pre_processors import Replace, StripWhitespaces +from magicparse.transform import Ok, SkipRow +from magicparse.type_converters import DecimalConverter, StrConverter from magicparse.validators import RegexMatches @@ -127,6 +128,19 @@ def test_require_field_with_empty_value(): field.parse("") +def test_ignore_require_field_with_empty_value(): + field = DummyField( + "pepito", + { + "type": {"key": "decimal", "on-error": "skip-row"}, + }, + ) + result = field.parse("") + assert isinstance(result, SkipRow) + assert type(result.exception) is ValueError + assert str(result.exception) == "pepito field is required but the value was empty" + + def test_field_without_key(): with pytest.raises(ValueError, match="key is required in field definition"): Field.build({"type": "decimal"})