Skip to content

Conversation

@hbierlee
Copy link
Contributor

@hbierlee hbierlee commented Sep 30, 2025

Fixes #654. It is over-explained in the linked issue, but the discrepancy is simply this:

>>> cp.sum(1,2)
3
>>> sum(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

Allowing cp.sum(1,2) was I think accidentally introduced by #111. I believe we should follow Python's design for all builtins, but this is a breaking change, so perhaps needs to be moved to cpmpy 0.10 / stable.

This is in line with the Python's `builtins.sum`
@hbierlee hbierlee requested a review from tias September 30, 2025 11:28
@tias tias added this to the v.0.9.28 milestone Oct 6, 2025
@tias tias added the for stable label Nov 3, 2025
@hbierlee hbierlee self-assigned this Dec 8, 2025
@hbierlee
Copy link
Contributor Author

I checked the behaviour again for all operators, they seem consistent with Python 3.12. No examples needed updating. So I think this is good to merge.

One more change I propose:

The straightforward way to check that our cpmpy override behaviour is the same as the python built-in is by comparing the signatures. Currently, they are the same, except for max and min, which for Python is:

max(arg1: SupportsLessThanT, arg2: SupportsLessThanT, /, *_args: SupportsLessThanT, key: None=...) -> SupportsLessThanT

but we have:

max(*iterable, **kwargs)

The behaviour is almost the same, except that we'll get a slightly different error. I will align that as well, then it's ready for review/merge.

@hbierlee hbierlee marked this pull request as draft December 16, 2025 12:16
@hbierlee
Copy link
Contributor Author

Ok, I looked into getting the same type signature for max/min, but it seems overkill for our case. It uses some pretty extensive typing to accomplish the argument overloading with @overload (https://typing.python.org/en/latest/spec/overload.html), plus there are not 2 but 6 max overloaded functions. I think our current solutions suffices, you almost get the same error as in Python, just from within the function rather than from the top.

To maintain this kind of complexity, we'd need some way to dynamically get the same signatures as the Python built-ins, but this is also out-of-scope.

@hbierlee hbierlee marked this pull request as ready for review December 16, 2025 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sum over single boolvar

3 participants