Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the new options module fully type safe #13620

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,16 @@ class BaseOption(T.Generic[_T]):
choices: T.Any = None

def init_option(self, name: OptionKey) -> options.UserOption[_T]:
keywords = {'value': self.default}
keywords = {}
if self.choices:
keywords['choices'] = self.choices
return self.opt_type(name.name, self.description, **keywords)
return self.opt_type(name.name, self.description, self.default, **keywords)


BASE_OPTIONS: T.Mapping[OptionKey, BaseOption] = {
OptionKey('b_pch'): BaseOption(options.UserBooleanOption, 'Use precompiled headers', True),
OptionKey('b_lto'): BaseOption(options.UserBooleanOption, 'Use link time optimization', False),
OptionKey('b_lto_threads'): BaseOption(options.UserIntegerOption, 'Use multiple threads for Link Time Optimization', (None, None, 0)),
OptionKey('b_lto_threads'): BaseOption(options.UserIntegerOption, 'Use multiple threads for Link Time Optimization', 0),
OptionKey('b_lto_mode'): BaseOption(options.UserComboOption, 'Select between different LTO modes.', 'default',
choices=['default', 'thin']),
OptionKey('b_thinlto_cache'): BaseOption(options.UserBooleanOption, 'Use LLVM ThinLTO caching for faster incremental builds', False),
Expand Down
24 changes: 12 additions & 12 deletions mesonbuild/compilers/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -400,8 +400,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: this one ends with a ., while other options don't...

Copy link
Member Author

Choose a reason for hiding this comment

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

That has apparently always existed. The main branch right now has that peroid

['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('std')
std_opt = opts[key]
Expand Down Expand Up @@ -450,8 +450,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -581,8 +581,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -664,8 +664,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -757,8 +757,8 @@ def _get_options_impl(self, opts: 'MutableKeyedOptionDictType', cpp_stds: T.List
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
'default',
['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down
5 changes: 3 additions & 2 deletions mesonbuild/compilers/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,13 @@ def get_options(self) -> 'MutableKeyedOptionDictType':

opts = super().get_options()

# XXX: cpp_std is correct, the annotations are wrong
key = self.form_compileropt_key('std')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ language standard to use with CUDA',
cpp_stds,
'none')
'none',
cpp_stds)

key = self.form_compileropt_key('ccbindir')
opts[key] = options.UserStringOption(
Expand Down
8 changes: 4 additions & 4 deletions mesonbuild/compilers/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Python version to target',
['2', '3'],
'3')
'3',
['2', '3'])

key = self.form_compileropt_key('language')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Output C or C++ files',
['c', 'cpp'],
'c')
'c',
['c', 'cpp'])

return opts

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Fortran language standard to use',
['none'],
'none')
'none',
['none'])

return opts

Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/compilers/mixins/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:
opts[key] = options.UserIntegerOption(
self.make_option_name(key),
'Number of threads to use in web assembly, set to 0 to disable',
(0, None, 4)) # Default was picked at random
4, # Default was picked at random
min_value=0)
Copy link
Member

Choose a reason for hiding this comment

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

I think this may be a good case where it's worth explaining the change in the commit message. After thinking about this, I think that this works because the dataclass also fits a historic mess where the "value" member isn't consistently used by all option types, when we do stuff like overloading it with minimum/maximum values instead?


return opts

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/objc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def get_options(self) -> 'coredata.MutableKeyedOptionDictType':
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C language standard to use',
['none', 'c89', 'c99', 'c11', 'c17', 'gnu89', 'gnu99', 'gnu11', 'gnu17'],
'none')
'none',
['none', 'c89', 'c99', 'c11', 'c17', 'gnu89', 'gnu99', 'gnu11', 'gnu17'])

return opts

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/objcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ language standard to use',
'none',
['none', 'c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++2b',
'gnu++98', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++20',
'gnu++2b'],
'none')
'gnu++2b'])

return opts

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def get_options(self) -> MutableKeyedOptionDictType:
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Rust edition to use',
['none', '2015', '2018', '2021'],
'none')
'none',
['none', '2015', '2018', '2021'])

return opts

Expand Down
3 changes: 2 additions & 1 deletion mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ def init_backend_options(self, backend_name: str) -> None:
'backend_max_links',
'Maximum number of linker processes to run or 0 for no '
'limit',
(0, None, 0)))
0,
min_value=0))
elif backend_name.startswith('vs'):
self.optstore.add_system_option('backend_startup_project', options.UserStringOption(
'backend_startup_project',
Expand Down
16 changes: 9 additions & 7 deletions mesonbuild/optinterpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def func_option(self, args: T.Tuple[str], kwargs: 'FuncOptionArgs') -> None:
KwargInfo('value', str, default=''),
)
def string_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: StringArgs) -> options.UserOption:
return options.UserStringOption(name, description, kwargs['value'], *args)
return options.UserStringOption(name, description, kwargs['value'], None, *args)

@typed_kwargs(
'boolean option',
Expand All @@ -226,7 +226,8 @@ def string_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPREC
),
)
def boolean_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: BooleanArgs) -> options.UserOption:
return options.UserBooleanOption(name, description, kwargs['value'], *args)
yielding, deprecated = args
return options.UserBooleanOption(name, description, kwargs['value'], yielding=yielding, deprecated=deprecated)

@typed_kwargs(
'combo option',
Expand All @@ -238,7 +239,7 @@ def combo_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPRECA
value = kwargs['value']
if value is None:
value = kwargs['choices'][0]
return options.UserComboOption(name, description, choices, value, *args)
return options.UserComboOption(name, description, value, choices, *args)

@typed_kwargs(
'integer option',
Expand All @@ -253,9 +254,8 @@ def combo_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPRECA
KwargInfo('max', (int, NoneType)),
)
def integer_parser(self, name: str, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: IntegerArgs) -> options.UserOption:
value = kwargs['value']
inttuple = (kwargs['min'], kwargs['max'], value)
return options.UserIntegerOption(name, description, inttuple, *args)
return options.UserIntegerOption(
name, description, kwargs['value'], None, *args, min_value=kwargs['min'], max_value=kwargs['max'])

@typed_kwargs(
'string array option',
Expand All @@ -270,8 +270,10 @@ def string_array_parser(self, name: str, description: str, args: T.Tuple[bool, _
FeatureDeprecated('String value for array option', '1.3.0').use(self.subproject)
else:
raise mesonlib.MesonException('Value does not define an array: ' + value)
# XXX: the value of choices is correct, the annotation is wrong.
# the annotation will be fixed in a later commit
return options.UserArrayOption(name, description, value,
choices=choices,
choices=choices, # type: ignore[arg-type]
yielding=args[0],
deprecated=args[1])

Expand Down
Loading