Skip to content

Global constraint name as class attribute#946

Open
IgnaceBleukx wants to merge 9 commits into
masterfrom
global_name_as_cls_attr
Open

Global constraint name as class attribute#946
IgnaceBleukx wants to merge 9 commits into
masterfrom
global_name_as_cls_attr

Conversation

@IgnaceBleukx

Copy link
Copy Markdown
Collaborator

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 : )

@IgnaceBleukx
IgnaceBleukx requested review from hbierlee and tias April 23, 2026 16:25

@hbierlee hbierlee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread cpmpy/solvers/minizinc.py Outdated
"""

supported_global_constraints = frozenset({"alldifferent", "alldifferent_except0", "allequal",
supported_global_constraints = frozenset({"alldifferent", "alldifferent_except_0", "allequal",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we change the constraint constructor to make the name optional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants