Skip to content

Commit d8681d5

Browse files
Better unit test around at usage
Better error message.
1 parent 68e905b commit d8681d5

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/gt4py/cartesian/frontend/gtscript_frontend.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1163,9 +1163,13 @@ def visit_Subscript(self, node: ast.Subscript):
11631163
field_axes = self.fields[result.name].axes
11641164
if index is not None:
11651165
if len(field_axes) != len(index):
1166+
ro_field_message = ""
1167+
if len(field_axes) == 0:
1168+
ro_field_message = f"Did you mean .at{index}?"
11661169
raise GTScriptSyntaxError(
11671170
f"Incorrect offset specification detected. Found {index}, "
1168-
f"but the field has dimensions ({', '.join(field_axes)})"
1171+
f"but the field has dimensions ({', '.join(field_axes)}). "
1172+
f"{ro_field_message}"
11691173
)
11701174
result.offset = {axis: value for axis, value in zip(field_axes, index)}
11711175
elif isinstance(node.value, ast.Subscript) or _is_absolute_indexing_node(node):

tests/cartesian_tests/unit_tests/frontend_tests/test_gtscript_frontend.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1477,16 +1477,47 @@ def at_write(
14771477
with computation(PARALLEL), interval(...):
14781478
global_field.at[1, 0, 2] = in_field
14791479

1480-
parse_definition(data_dims, name=inspect.stack()[0][3], module=self.__class__.__name__)
1480+
def ROField_access_as_IJK(
1481+
out_field: gtscript.Field[gtscript.IJK, np.int32],
1482+
global_field: gtscript.ROField[(np.int32, (3, 3, 3))],
1483+
):
1484+
with computation(PARALLEL), interval(...):
1485+
out_field = global_field[1, 0, 2]
1486+
1487+
def data_dims_with_at(
1488+
out_field: gtscript.Field[gtscript.IJK, np.int32],
1489+
global_field: gtscript.Field[(np.int32, (3, 3, 3))],
1490+
):
1491+
with computation(PARALLEL), interval(...):
1492+
out_field = global_field.at[1, 0, 2]
14811493

1494+
# Check .at on read with a Field with data dimensions
1495+
parse_definition(
1496+
data_dims_with_at, name=inspect.stack()[0][3], module=self.__class__.__name__
1497+
)
1498+
1499+
# Check .at on read
14821500
parse_definition(at_read, name=inspect.stack()[0][3], module=self.__class__.__name__)
14831501

1502+
# Check classic data dimensions are working
1503+
parse_definition(data_dims, name=inspect.stack()[0][3], module=self.__class__.__name__)
1504+
1505+
# Can't write to the field
14841506
with pytest.raises(
14851507
gt_frontend.GTScriptSyntaxError,
14861508
match=r".*writing to a ROField \('at' global indexation\) is forbidden.*",
14871509
):
14881510
parse_definition(at_write, name=inspect.stack()[0][3], module=self.__class__.__name__)
14891511

1512+
# Can't index cartesian style
1513+
with pytest.raises(
1514+
gt_frontend.GTScriptSyntaxError,
1515+
match="Incorrect offset specification detected. Found .* but the field has dimensions .* Did you mean .at",
1516+
):
1517+
parse_definition(
1518+
ROField_access_as_IJK, name=inspect.stack()[0][3], module=self.__class__.__name__
1519+
)
1520+
14901521

14911522
class TestNestedWithSyntax:
14921523
def test_nested_with(self):

0 commit comments

Comments
 (0)