Global constraint name as class attribute#946
Open
IgnaceBleukx wants to merge 9 commits into
Open
Conversation
hbierlee
reviewed
Apr 24, 2026
hbierlee
left a comment
Contributor
There was a problem hiding this comment.
I think this will be an improvement, I'm really thinking that the proper way is to just have a get_name() function, since in most cases it can just return the class name, but will require too many changes plus adds some overhead I suppose
| """ | ||
|
|
||
| supported_global_constraints = frozenset({"alldifferent", "alldifferent_except0", "allequal", | ||
| supported_global_constraints = frozenset({"alldifferent", "alldifferent_except_0", "allequal", |
Contributor
There was a problem hiding this comment.
here's a source of duplication then, still, which leads to typos: I'd have cp.AllDifferent.name, which avoids this issue by giving an error if you write cp.AlDifferent
| if not is_any_list(arr): | ||
| raise ValueError(f"NValue(arr) takes an array as input, not: {arr}") | ||
| super().__init__("nvalue", tuple(arr)) | ||
| super().__init__(self.name, tuple(arr)) |
Contributor
There was a problem hiding this comment.
can't we change the constraint constructor to make the name optional?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, the name of a global constraint was hard-coded in the super() call during init.
This PR sets the name of global constraints and functions as a class attribute, as it is not supposed to change between instances.
Originally inspired by the issues @hbierlee discovered with coverage testing for OR-Tools (missing comma in supported list).
This change also allows to catch these mistakes automatically by checking whether all strings in the supported set of a solver actually exist.
I'm not entirely sold on the idea myself, but I thought this may be a nice change, so happy to discuss/merge/close : )