From 8bb0ce460498e595eb9202fa0e4ae763eeae7ffe Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Mon, 30 Dec 2019 13:54:08 +0100
Subject: [PATCH] Throw error when trying to sympify `pystencils.Field` (e.g.
 using it in an Assignment without indexing)

---
 pystencils/field.py                            |  8 +++++++-
 pystencils_tests/test_assignment_collection.py | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/pystencils/field.py b/pystencils/field.py
index 507be0c2..a8c82b47 100644
--- a/pystencils/field.py
+++ b/pystencils/field.py
@@ -15,7 +15,7 @@ import pystencils
 from pystencils.alignedarray import aligned_empty
 from pystencils.data_types import StructType, TypedSymbol, create_type
 from pystencils.kernelparameters import FieldShapeSymbol, FieldStrideSymbol
-from pystencils.stencil import direction_string_to_offset, offset_to_direction_string, inverse_direction
+from pystencils.stencil import direction_string_to_offset, inverse_direction, offset_to_direction_string
 from pystencils.sympyextensions import is_integer_sequence
 
 __all__ = ['Field', 'fields', 'FieldType', 'AbstractField']
@@ -413,6 +413,12 @@ class Field(AbstractField):
     def __str__(self):
         return self.name
 
+    def __getattribute__(self, name):
+        if name == '__sympy__':
+            raise RuntimeError("pystencils.Field cannot by sympified!\n"
+                               "Please use a proper indexing to access elements of the Field!")
+        return super().__getattribute__(name)
+
     def neighbor(self, coord_id, offset):
         offset_list = [0] * self.spatial_dimensions
         offset_list[coord_id] = offset
diff --git a/pystencils_tests/test_assignment_collection.py b/pystencils_tests/test_assignment_collection.py
index 42e3791a..cbe08763 100644
--- a/pystencils_tests/test_assignment_collection.py
+++ b/pystencils_tests/test_assignment_collection.py
@@ -1,4 +1,6 @@
+import pytest
 import sympy as sp
+import pystencils
 
 from pystencils import Assignment, AssignmentCollection
 from pystencils.astnodes import Conditional
@@ -40,3 +42,15 @@ def test_free_and_defined_symbols():
 
     print(ac)
     print(ac.__repr__)
+
+
+def test_sympifying_fields():
+    x, y = pystencils.fields('x, y: double[1d]')
+    # Apparently, we trigger our helpful error message only for Sympy>=1.5
+    with pytest.raises((RuntimeError, sp.SympifyError)):
+        AssignmentCollection({
+            x: y
+            })
+
+    with pytest.raises((RuntimeError, sp.SympifyError)):
+        sp.S(x)
-- 
GitLab