Skip to content

Commit

Permalink
options: Add a function to compare different option choices
Browse files Browse the repository at this point in the history
This allows us to hide type differences inside the options module, but
still get accurate change information.
  • Loading branch information
dcbaker committed Sep 6, 2024
1 parent 8314215 commit c7ed1ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def update_project_options(self, project_options: 'MutableKeyedOptionDictType',
oldval = self.optstore.get_value_object(key)
if type(oldval) is not type(value):
self.optstore.set_value(key, value.value)
elif oldval.printable_choices() != value.printable_choices():
elif options.choices_are_different(oldval, value):
# If the choices have changed, use the new value, but attempt
# to keep the old options. If they are not valid keep the new
# defaults but warn.
Expand Down
26 changes: 25 additions & 1 deletion mesonbuild/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,31 @@ def is_auto(self) -> bool:
return self.value == 'auto'


@dataclasses.dataclass(init=False)
_U = T.TypeVar('_U', bound=UserOption)


def choices_are_different(a: _U, b: _U) -> bool:
"""Are the choices between two options the same?
:param a: A UserOption[T]
:param b: A second UserOption[T]
:return: True if the choices have changed, otherwise False
"""
if isinstance(a, EnumeratedUserOption):
# We expect `a` and `b` to be of the same type, but can't really annotate it that way.
assert isinstance(b, EnumeratedUserOption), 'for mypy'
return a.choices != b.choices
elif isinstance(a, UserArrayOption):
# We expect `a` and `b` to be of the same type, but can't really annotate it that way.
assert isinstance(b, UserArrayOption), 'for mypy'
return a.choices != b.choices
elif isinstance(a, _UserIntegerBase):
assert isinstance(b, _UserIntegerBase), 'for mypy'
return a.max_value != b.max_value or a.min_value != b.min_value

return False


class UserStdOption(UserComboOption):
'''
UserOption specific to c_std and cpp_std options. User can set a list of
Expand Down

0 comments on commit c7ed1ed

Please sign in to comment.