Deprecate passing strings to non-string options

Currently Meson allow the following (Muon does not):
```meson
option('foo', type : 'boolean', value : 'true')
option('bar', type : 'integer', value : '42')
```

This is possibly a holdover from very old code, but it's a bad idea and
we should stop doing it. This deprecation is the first stop on that
journey.
pull/11277/head
Dylan Baker 2 years ago committed by Eli Schwartz
parent f5eaebb4b4
commit 43a2404707
  1. 9
      docs/markdown/snippets/coercing_option_values_deprecated.md
  2. 4
      mesonbuild/optinterpreter.py
  3. 2
      test cases/common/247 deprecated option/meson_options.txt
  4. 5
      test cases/common/40 options/meson.build
  5. 4
      test cases/common/40 options/meson_options.txt
  6. 7
      test cases/common/40 options/test.json

@ -0,0 +1,9 @@
## coercing values in the option() function is deprecated
Currently code such as:
```meson
option('foo', type : 'boolean', value : 'false')
```
works, because Meson coerces `'false'` to `false`.
This should be avoided, and will now result in a deprecation warning.

@ -20,7 +20,7 @@ from . import coredata
from . import mesonlib
from . import mparser
from . import mlog
from .interpreterbase import FeatureNew, typed_pos_args, typed_kwargs, ContainerTypeInfo, KwargInfo
from .interpreterbase import FeatureNew, typed_pos_args, typed_kwargs, ContainerTypeInfo, KwargInfo, FeatureDeprecated
from .interpreter.type_checking import NoneType, in_set_validator
if T.TYPE_CHECKING:
from .interpreterbase import TYPE_var, TYPE_kwargs
@ -221,6 +221,7 @@ class OptionInterpreter:
(bool, str),
default=True,
validator=lambda x: None if isinstance(x, bool) or x in {'true', 'false'} else 'boolean options must have boolean values',
deprecated_values={'true': ('1.1.0', 'use a boolean, not a string'), 'false': ('1.1.0', 'use a boolean, not a string')},
),
)
def boolean_parser(self, description: str, yield_: bool, kwargs: BooleanArgs) -> coredata.UserOption:
@ -244,6 +245,7 @@ class OptionInterpreter:
'value',
(int, str),
default=True,
feature_validator=lambda x: [FeatureDeprecated('number values as strings', '1.1.0', 'use a raw number instead')] if isinstance(x, str) else [],
convertor=int,
),
KwargInfo('min', (int, NoneType)),

@ -16,7 +16,7 @@ option('o5', type: 'boolean', deprecated: {'enabled': 'true', 'disabled': 'false
# A boolean option has been replaced by a feature with another name, old true/false values
# are accepted by the new option for backward compatibility.
option('o6', type: 'boolean', value: 'true', deprecated: 'o7')
option('o6', type: 'boolean', value: true, deprecated: 'o7')
option('o7', type: 'feature', value: 'enabled', deprecated: {'true': 'enabled', 'false': 'disabled'})
# A project option is replaced by a module option

@ -1,4 +1,4 @@
project('options', 'c')
project('options', 'c', meson_version : '>= 1.0.0')
if get_option('testoption') != 'optval'
error('Incorrect value to test option')
@ -43,3 +43,6 @@ if get_option('CASESENSITIVE') != 'ALL CAPS'
endif
assert(get_option('wrap_mode') == 'default', 'Wrap mode option is broken.')
assert(get_option('boolean_string') == false)
assert(get_option('boolean_string2') == true)
assert(get_option('integer_string') == 42)

@ -7,3 +7,7 @@ option('integer_opt', type : 'integer', min : 0, max : -(-5), value : 3)
option('neg' + '_' + 'int' + '_' + 'opt', type : 'integer', min : -5, max : 5, value : -3)
option('CaseSenSiTivE', type : 'string', value: 'Some CAPS', description : 'An option with mixed capitaliziation')
option('CASESENSITIVE', type : 'string', value: 'ALL CAPS', description : 'An option with all caps')
option('boolean_string', type : 'boolean', value : 'false')
option('boolean_string2', type : 'boolean', value : 'true')
option('integer_string', type : 'integer', value : '42')

@ -0,0 +1,7 @@
{
"stdout": [
{
"line": " * 1.1.0: {'\"boolean option\" keyword argument \"value\" value \"false\"', '\"boolean option\" keyword argument \"value\" value \"true\"', 'number values as strings'}"
}
]
}
Loading…
Cancel
Save