Skip to content

Commit 2b4f40d

Browse files
authored
Merge pull request #6 from cmungall/linkml-issue-1373
throw meaningful exception when values > columns.
2 parents 88cd5a6 + ff712b6 commit 2b4f40d

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/json_flattener/flattener.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
OBJECT = Dict[KEYNAME, Any]
2222

2323

24+
class MissingColumnError(ValueError):
25+
"""Exception raised when a column is missing from a row."""
26+
27+
def __init__(self, row: ROW, value: Any):
28+
"""Initialize exception."""
29+
super().__init__(f"Value {value} has no column row {row}")
30+
31+
2432
@unique
2533
class Serializer(Enum):
2634
"""Vocabulary of methods to use for serialization objects."""
@@ -441,6 +449,8 @@ def _getval(x: str) -> Optional[Any]:
441449
for row in r:
442450
nu_obj = {}
443451
for k, v in row.items():
452+
if k is None:
453+
raise MissingColumnError(row, v)
444454
key_config = config.key_configs.get(k, None)
445455
v = v.replace("\\n", "\n").replace("\\t", "\t")
446456
# lists are demarcated by list markers

tests/inputs/badly-formatted.tsv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source_language translation_language subject_id predicate_id source_value translation_value translation_status
2+
en tr HP:0004432 IAO:0000115 ab "a b" CANDIDATE

tests/test_flattener.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import unittest
7+
from pathlib import Path
78
from typing import Any, List
89

910
import yaml
@@ -17,7 +18,8 @@
1718
unflatten,
1819
unflatten_from_csv,
1920
)
20-
from tests import INPUT
21+
from json_flattener.flattener import MissingColumnError
22+
from tests import INPUT, INPUT_DIR
2123

2224

2325
def _json(obj) -> str:
@@ -392,6 +394,14 @@ def test_roundtrip_from_file(self):
392394
logging.info(roundtrip_json)
393395
self._roundtrip_to_tsv(objs, config=config)
394396

397+
def test_badly_formatted(self):
398+
"""
399+
Tests graceful failure on badly formatted TSV input.
400+
"""
401+
with open(str(Path(INPUT_DIR) / "badly-formatted.tsv")) as stream:
402+
with self.assertRaises(MissingColumnError) as _e:
403+
_objs = unflatten_from_csv(stream)
404+
395405

396406
if __name__ == "__main__":
397407
unittest.main()

0 commit comments

Comments
 (0)