Skip to content

Commit a0f0920

Browse files
committed
[CP-SAT] remove useless python type checks
1 parent ab029a6 commit a0f0920

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

ortools/sat/python/cp_model.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1716,17 +1716,20 @@ def add_allowed_assignments(
17161716
"add_allowed_assignments expects a non-empty variables array"
17171717
)
17181718

1719-
ct = Constraint(self)
1719+
ct: Constraint = Constraint(self)
17201720
model_ct = self.__model.constraints[ct.index]
17211721
model_ct.table.vars.extend([self.get_or_make_index(x) for x in variables])
1722-
arity = len(variables)
1722+
arity: int = len(variables)
17231723
for t in tuples_list:
17241724
if len(t) != arity:
17251725
raise TypeError("Tuple " + str(t) + " has the wrong arity")
1726-
ar = []
1727-
for v in t:
1728-
ar.append(cmh.assert_is_int64(v))
1729-
model_ct.table.values.extend(ar)
1726+
1727+
# duck-typing (no explicit type checks here)
1728+
try:
1729+
model_ct.table.values.extend(a for b in tuples_list for a in b)
1730+
except ValueError as ex:
1731+
raise TypeError(f"add_xxx_assignment: Not an integer or does not fit in an int64_t: {ex.args}") from ex
1732+
17301733
return ct
17311734

17321735
def add_forbidden_assignments(
@@ -1760,7 +1763,7 @@ def add_forbidden_assignments(
17601763
)
17611764

17621765
index = len(self.__model.constraints)
1763-
ct = self.add_allowed_assignments(variables, tuples_list)
1766+
ct: Constraint = self.add_allowed_assignments(variables, tuples_list)
17641767
self.__model.constraints[index].table.negated = True
17651768
return ct
17661769

@@ -2751,7 +2754,7 @@ def get_interval_var_from_proto_index(self, index: int) -> IntervalVar:
27512754

27522755
# Helpers.
27532756

2754-
def __str__(self):
2757+
def __str__(self) -> str:
27552758
return str(self.__model)
27562759

27572760
@property

0 commit comments

Comments
 (0)