interpreter: use typed_pos_args for get_variable

pull/8953/head
Dylan Baker 3 years ago
parent 4d05f9d64b
commit 20c5dfe869
  1. 21
      mesonbuild/interpreter/interpreter.py

@ -11,7 +11,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from mesonbuild.interpreterbase.baseobjects import TYPE_kwargs
from .. import mparser from .. import mparser
from .. import environment from .. import environment
from .. import coredata from .. import coredata
@ -32,7 +31,7 @@ from ..interpreterbase import InterpreterException, InvalidArguments, InvalidCod
from ..interpreterbase import Disabler, disablerIfNotFound from ..interpreterbase import Disabler, disablerIfNotFound
from ..interpreterbase import FeatureNew, FeatureDeprecated, FeatureNewKwargs, FeatureDeprecatedKwargs from ..interpreterbase import FeatureNew, FeatureDeprecated, FeatureNewKwargs, FeatureDeprecatedKwargs
from ..interpreterbase import ObjectHolder, RangeHolder from ..interpreterbase import ObjectHolder, RangeHolder
from ..interpreterbase import TYPE_nkwargs, TYPE_nvar, TYPE_var from ..interpreterbase.baseobjects import TYPE_nkwargs, TYPE_nvar, TYPE_var, TYPE_kwargs
from ..modules import ExtensionModule, ModuleObject, MutableModuleObject, NewExtensionModule, NotFoundExtensionModule from ..modules import ExtensionModule, ModuleObject, MutableModuleObject, NewExtensionModule, NotFoundExtensionModule
from ..cmake import CMakeInterpreter from ..cmake import CMakeInterpreter
from ..backend.backends import Backend, ExecutableSerialisation from ..backend.backends import Backend, ExecutableSerialisation
@ -2734,24 +2733,22 @@ This will become a hard error in the future.''', location=self.current_node)
varname, value = args varname, value = args
self.set_variable(varname, value, holderify=True) self.set_variable(varname, value, holderify=True)
@typed_pos_args('get_variable', (str, Disabler), optargs=[object])
@noKwargs @noKwargs
@noArgsFlattening @noArgsFlattening
@permissive_unholder_return @permissive_unholder_return
def func_get_variable(self, node, args, kwargs): def func_get_variable(self, node: mparser.BaseNode, args: T.Tuple[T.Union[str, Disabler], T.Optional[object]],
if len(args) < 1 or len(args) > 2: kwargs: 'TYPE_kwargs') -> 'TYPE_var':
raise InvalidCode('Get_variable takes one or two arguments.') varname, fallback = args
varname = args[0]
if isinstance(varname, Disabler): if isinstance(varname, Disabler):
return varname return varname
if not isinstance(varname, str):
raise InterpreterException('First argument must be a string.')
try: try:
return self.variables[varname] return self.variables[varname]
except KeyError: except KeyError:
pass if fallback is not None:
if len(args) == 2: return fallback
return args[1] raise InterpreterException(f'Tried to get unknown variable "{varname}".')
raise InterpreterException('Tried to get unknown variable "%s".' % varname)
@typed_pos_args('is_variable', str) @typed_pos_args('is_variable', str)
@noKwargs @noKwargs

Loading…
Cancel
Save