Skip to content

Commit 6c7a2a0

Browse files
Explicity catch 2D fields error
1 parent b55f827 commit 6c7a2a0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tests/test_filter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ def test_numpy_arithmetic_expressions_data(self, expr, data):
260260
evaled = eval(expr, npdata)
261261
nt.assert_array_equal(result, evaled)
262262

263+
@pytest.mark.parametrize(
264+
("expr", "data"),
265+
[
266+
("a", {"a": [[1], [2], [3]]}),
267+
],
268+
)
269+
def test_numpy_2d_arithmetic_expressions_data(self, expr, data):
270+
parser = filter_mod.make_bcftools_filter_parser(map_vcf_identifiers=False)
271+
parsed = parser.parse_string(expr, parse_all=True)
272+
npdata = numpify_values(data)
273+
with pytest.raises(filter_mod.Unsupported2DFieldsError):
274+
parsed[0].eval(npdata)
275+
263276
@pytest.mark.parametrize(
264277
("expr", "expected"),
265278
[

vcztools/filter.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class UnsupportedFunctionsError(UnsupportedFilteringFeatureError):
5757
feature = "Function evaluation"
5858

5959

60+
class Unsupported2DFieldsError(UnsupportedFilteringFeatureError):
61+
issue = "193"
62+
feature = "2D INFO fields"
63+
64+
6065
# The parser and evaluation model here are based on the eval_arith example
6166
# in the pyparsing docs:
6267
# https://github.com/pyparsing/pyparsing/blob/master/examples/eval_arith.py
@@ -110,7 +115,10 @@ def __init__(self, mapper, tokens):
110115
logger.debug(f"Mapped {tokens[0]} to {self.field_name}")
111116

112117
def eval(self, data):
113-
return data[self.field_name]
118+
value = np.asarray(data[self.field_name])
119+
if len(value.shape) > 1:
120+
raise Unsupported2DFieldsError()
121+
return value
114122

115123
def __repr__(self):
116124
return self.field_name

0 commit comments

Comments
 (0)