Skip to content

Commit 10b31b6

Browse files
committed
Revert the creation of anonymous enumerated types
1 parent 753a08a commit 10b31b6

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Fixed
1515
- Terminate the MiniZinc process when stopping early (instead of killing it).
1616
This allows MiniZinc to correctly stop any solver processes.
1717

18+
Changed
19+
^^^^^^^
20+
21+
- Revert change from 0.4.1 where enumerated types unknown to Python would be
22+
made stored as anonymous enumerations. Interoperability between the MiniZinc
23+
driver and the MiniZinc Python has instead changed to allow JSON strings as
24+
valid input for enumerated types. (required MiniZinc 2.5.3)
25+
1826
0.4.1_ - 2020-11-11
1927
-------------------
2028

src/minizinc/json.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ def __init__(self, enum_map=None, *args, **kwargs):
2424
self.enum_map = enum_map
2525
JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
2626

27-
def _lookup_enum(self, name: str):
28-
if name in self.enum_map:
29-
return self.enum_map[name]
30-
else:
31-
# TODO: mypy seems to believe name the elements should be literals,
32-
# but I cannot find this anywhere in the documentation
33-
anon_enum = Enum("AnonymousEnum", name) # type: ignore
34-
self.enum_map[name] = anon_enum(1)
35-
return anon_enum(1)
36-
3727
def object_hook(self, obj):
3828
if len(obj) == 1 and "set" in obj:
3929
if len(obj["set"]) == 1 and isinstance(obj["set"][0], list):
@@ -46,11 +36,11 @@ def object_hook(self, obj):
4636
assert len(item) == 2
4737
li.extend([i for i in range(item[0], item[1] + 1)])
4838
elif len(obj) == 1 and "e" in obj:
49-
li.append(self._lookup_enum(obj["e"]))
39+
li.append(self.enum_map.get(obj["e"], obj["e"]))
5040
else:
5141
li.append(item)
5242
return set(li)
5343
elif len(obj) == 1 and "e" in obj:
54-
return self._lookup_enum(obj["e"])
44+
return self.enum_map.get(obj["e"], obj["e"])
5545
else:
5646
return obj

tests/test_types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class TestEnum(InstanceTestCase):
1717
def test_value(self):
1818
self.instance.add_string("constraint d == Mo;")
1919
result = self.instance.solve()
20-
assert isinstance(result["d"], enum.Enum)
21-
assert result["d"].name == "Mo"
20+
assert isinstance(result["d"], str)
21+
assert result["d"] == "Mo"
2222

2323
def test_cmp_in_instance(self):
2424
self.instance.add_string("var DAY: d2;")
2525
self.instance.add_string("constraint d < d2;")
2626
result = self.instance.solve()
27-
assert isinstance(result["d"], enum.Enum)
28-
assert isinstance(result["d2"], enum.Enum)
27+
assert isinstance(result["d"], str)
28+
assert isinstance(result["d2"], str)
2929
# TODO: assert result["d"] < result["d2"]
3030

3131
def test_cmp_between_instances(self):
@@ -36,9 +36,9 @@ def test_cmp_between_instances(self):
3636
inst = Instance(self.solver)
3737
inst.add_string(self.code + append)
3838
result2 = inst.solve()
39-
assert isinstance(result["d"], enum.Enum)
40-
assert isinstance(result2["d"], enum.Enum)
41-
assert result["d"].name == result2["d"].name
39+
assert isinstance(result["d"], str)
40+
assert isinstance(result2["d"], str)
41+
assert result["d"] == result2["d"]
4242

4343
inst = Instance(self.solver)
4444
inst.add_string(
@@ -49,7 +49,7 @@ def test_cmp_between_instances(self):
4949
+ append
5050
)
5151
result2 = inst.solve()
52-
assert result["d"].name == result2["d"].name
52+
assert result["d"] == result2["d"]
5353

5454
def test_assign(self):
5555
self.instance = Instance(self.solver)

0 commit comments

Comments
 (0)