Browse Source

property Triangle.vertex_indices now read-only [breaking]

Fabian Peter Hammerle 5 years ago
parent
commit
abfdfd8a5c
3 changed files with 19 additions and 24 deletions
  1. 4 12
      freesurfer_surface/__init__.py
  2. 15 0
      tests/test_polygonal_circuit.py
  3. 0 12
      tests/test_triangle.py

+ 4 - 12
freesurfer_surface/__init__.py

@@ -104,17 +104,12 @@ class _PolygonalCircuit:
     _VERTEX_INDICES_TYPE = typing.Tuple[int]
 
     def __init__(self, vertex_indices: _VERTEX_INDICES_TYPE):
-        self.vertex_indices = vertex_indices
+        self._vertex_indices = tuple(vertex_indices)
 
     @property
     def vertex_indices(self):
         return self._vertex_indices
 
-    @vertex_indices.setter
-    def vertex_indices(self, indices: _VERTEX_INDICES_TYPE):
-        # pylint: disable=attribute-defined-outside-init
-        self._vertex_indices = tuple(indices)
-
     def _normalize(self) -> '_PolygonalCircuit':
         vertex_indices = collections.deque(self.vertex_indices)
         vertex_indices.rotate(-numpy.argmin(self.vertex_indices))
@@ -159,12 +154,9 @@ class _LineSegment(_PolygonalCircuit):
 
 class Triangle(_PolygonalCircuit):
 
-    # pylint: disable=no-member
-    @_PolygonalCircuit.vertex_indices.setter
-    def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
-        assert len(indices) == 3
-        # pylint: disable=attribute-defined-outside-init
-        self._vertex_indices = tuple(indices)
+    def __init__(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
+        super().__init__(indices)
+        assert len(self.vertex_indices) == 3
 
     def __repr__(self) -> str:
         return 'Triangle(vertex_indices={})'.format(self.vertex_indices)

+ 15 - 0
tests/test_polygonal_circuit.py

@@ -3,6 +3,21 @@ import pytest
 from freesurfer_surface import _PolygonalCircuit
 
 
+def test_init_tuple():
+    circuit = _PolygonalCircuit((1, 2, 3, 5))
+    assert circuit.vertex_indices == (1, 2, 3, 5)
+
+
+def test_init_list():
+    circuit = _PolygonalCircuit([1, 2, 3, 5])
+    assert circuit.vertex_indices == (1, 2, 3, 5)
+
+
+def test_init_iterator():
+    circuit = _PolygonalCircuit(iter([1, 2, 3, 5]))
+    assert circuit.vertex_indices == (1, 2, 3, 5)
+
+
 @pytest.mark.parametrize(('source_vertex_indices', 'expected_vertex_indices'), [
     ((1,), (1,)),
     ((1, 2), (1, 2)),

+ 0 - 12
tests/test_triangle.py

@@ -13,18 +13,6 @@ def test_init_invalid_indices_len():
         Triangle((0, 21, 42, 84))
 
 
-def test_reassign_vertex_indices():
-    triangle = Triangle((0, 21, 42))
-    triangle.vertex_indices = (1, 2, 3)
-    assert triangle.vertex_indices == (1, 2, 3)
-
-
-def test_reassign_vertex_indices_invalid_len():
-    triangle = Triangle((0, 21, 42))
-    with pytest.raises(Exception):
-        triangle.vertex_indices = (1, 2, 3, 4)
-
-
 def test_eq():
     assert Triangle((0, 1, 2)) == Triangle((0, 1, 2))
     assert Triangle((0, 1, 2)) == Triangle((1, 2, 0))