Skip to content

Commit 1e02528

Browse files
committed
Add support for record and tuple types
1 parent 6d9cd4c commit 1e02528

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ this project adheres to `Semantic Versioning <https://semver.org/>`_.
99
Unreleased_
1010
------------
1111

12+
Added
13+
^^^^^
14+
15+
- Add support for MiniZinc tuple and record types.
16+
1217
Changed
1318
^^^^^^^
1419

src/minizinc/instance.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,10 @@ def _to_python_type(mzn_type: dict) -> Type:
797797
pytype = str
798798
elif basetype == "ann":
799799
pytype = str
800+
elif basetype == "tuple":
801+
pytype = list
802+
elif basetype == "record":
803+
pytype = dict
800804
else:
801805
warnings.warn(
802806
f"Unable to determine minizinc type `{basetype}` assuming integer type",

tests/test_types.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,82 @@ def test_string(self):
182182
assert result.solution.name in names
183183

184184

185+
class TestTuple(InstanceTestCase):
186+
def test_simple_tuple(self):
187+
self.instance.add_string(
188+
"""
189+
var tuple(1..3, bool, 1.0..3.0): x;
190+
"""
191+
)
192+
result = self.instance.solve()
193+
tup = result["x"]
194+
assert isinstance(tup, list)
195+
assert len(tup) == 3
196+
assert isinstance(tup[0], int)
197+
assert tup[0] in range(1, 4)
198+
assert isinstance(tup[1], bool)
199+
assert isinstance(tup[2], float)
200+
assert 1.0 <= tup[2] and tup[2] <= 3.0
201+
202+
def test_rec_tuple(self):
203+
self.instance.add_string(
204+
"""
205+
var tuple(1..3, bool, tuple(2..3, 4..6)): x;
206+
"""
207+
)
208+
result = self.instance.solve()
209+
tup = result["x"]
210+
assert isinstance(tup, list)
211+
assert len(tup) == 3
212+
assert isinstance(tup[0], int)
213+
assert tup[0] in range(1, 4)
214+
assert isinstance(tup[1], bool)
215+
assert isinstance(tup[2], list)
216+
assert len(tup[2]) == 2
217+
assert isinstance(tup[2][0], int)
218+
assert tup[2][0] in range(2, 4)
219+
assert isinstance(tup[2][1], int)
220+
assert tup[2][1] in range(4, 7)
221+
222+
223+
class TestRecord(InstanceTestCase):
224+
def test_simple_record(self):
225+
self.instance.add_string(
226+
"""
227+
var record(1..3: a, bool: b, 1.0..3.0: c): x;
228+
"""
229+
)
230+
result = self.instance.solve()
231+
rec = result["x"]
232+
assert isinstance(rec, dict)
233+
assert len(rec) == 3
234+
assert isinstance(rec["a"], int)
235+
assert rec["a"] in range(1, 4)
236+
assert isinstance(rec["b"], bool)
237+
assert isinstance(rec["c"], float)
238+
assert 1.0 <= rec["c"] and rec["c"] <= 3.0
239+
240+
def test_rec_record(self):
241+
self.instance.add_string(
242+
"""
243+
var record(1..3: a, bool: b, record(2..3: d, 4..6: e): c): x;
244+
"""
245+
)
246+
result = self.instance.solve()
247+
rec = result["x"]
248+
assert isinstance(rec, dict)
249+
assert len(rec) == 3
250+
assert isinstance(rec["a"], int)
251+
assert rec["a"] in range(1, 4)
252+
assert isinstance(rec["b"], bool)
253+
assert isinstance(rec["c"], dict)
254+
assert len(rec["c"]) == 2
255+
assert isinstance(rec["c"]["d"], int)
256+
assert rec["c"]["d"] in range(2, 4)
257+
assert isinstance(rec["c"]["e"], int)
258+
assert rec["c"]["e"] in range(4, 7)
259+
260+
185261
class TestNumPy(InstanceTestCase):
186262
def test_nparray_bool(self):
187263
numpy = pytest.importorskip("numpy")

0 commit comments

Comments
 (0)