From aa57b2d5f5b8995d16e3166592c33fb8028520df Mon Sep 17 00:00:00 2001 From: Timothy Kassis Date: Wed, 30 Sep 2026 16:38:25 -0700 Subject: [PATCH] fix(analytical-method-validation): reject surplus CSV fields Fixes #281. Report malformed CSV and TSV as input errors with the data row number, preserving missing-value errors for short rows. --- skills/analytical-method-validation/SKILL.md | 2 +- .../scripts/_common.py | 5 ++++ .../test_scripts.py | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/skills/analytical-method-validation/SKILL.md b/skills/analytical-method-validation/SKILL.md index 39d8b38f..6d193f01 100644 --- a/skills/analytical-method-validation/SKILL.md +++ b/skills/analytical-method-validation/SKILL.md @@ -5,7 +5,7 @@ license: MIT compatibility: Requires Python 3.11+. Scripts use only the standard library - no numpy, scipy, or network access. Statistical distributions are computed from first principles so results are reproducible in any conforming interpreter. allowed-tools: Read Write Edit Bash metadata: - version: "1.2" + version: "1.3" skill-author: K-Dense Inc. last-reviewed: "2026-07-27" --- diff --git a/skills/analytical-method-validation/scripts/_common.py b/skills/analytical-method-validation/scripts/_common.py index 50332f25..eb2feaeb 100644 --- a/skills/analytical-method-validation/scripts/_common.py +++ b/skills/analytical-method-validation/scripts/_common.py @@ -843,6 +843,11 @@ def parse_rows(text: str, path_hint: str | None = None) -> list[dict[str, str]]: for i, row in enumerate(reader): if i >= MAX_ROWS: raise InputError(f"more than {MAX_ROWS} rows") + # DictReader stores surplus fields as a list under its None restkey. + if None in row: + raise InputError( + f"row {i + 1}: more fields than the header ({len(row[None])} extra)" + ) rows.append({(k or "").strip(): (v or "").strip() for k, v in row.items()}) if not rows: raise InputError("no data rows found") diff --git a/tests/analytical-method-validation/test_scripts.py b/tests/analytical-method-validation/test_scripts.py index 262f8ae9..daa0ba9e 100644 --- a/tests/analytical-method-validation/test_scripts.py +++ b/tests/analytical-method-validation/test_scripts.py @@ -908,6 +908,31 @@ class TestInputHandling(unittest.TestCase): self.assertEqual(res.returncode, 2) self.assertIn("not numeric", res.stderr) + def test_ragged_csv_and_tsv_name_the_data_row(self): + for delimiter, suffix in ((",", ".csv"), ("\t", ".tsv")): + with self.subTest(suffix=suffix): + text = delimiter.join(("level", "response")) + "\n" + text += delimiter.join(("50", "10100")) + "\n" + text += delimiter.join(("100", "20100", "extra")) + "\n" + with self.assertRaisesRegex( + common.InputError, "row 2: more fields than the header" + ): + common.parse_rows(text, path_hint="calibration" + suffix) + + def test_ragged_and_short_rows_are_cli_input_errors(self): + for row, message in (("100,20100,extra", "more fields than the header"), + ("100", "not numeric")): + with self.subTest(row=row): + res = subprocess.run( + [sys.executable, str(SCRIPTS_DIR / "check_response.py"), "-i", "-"], + input="level,response\n50,10100\n" + row + "\n", + capture_output=True, text=True, check=False, + ) + self.assertEqual(res.returncode, 2, res.stderr) + self.assertIn(message, res.stderr) + self.assertNotIn("Traceback", res.stderr) + self.assertEqual(res.stdout, "") + def test_missing_file_exits_2(self): res = run_script("check_response", "-i", str(FIXTURES / "does_not_exist.csv")) self.assertEqual(res.returncode, 2)