From 351a1e9ec93fecee8923356c5647fa526524c54d Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 30 Jun 2021 12:46:33 -0700 Subject: [PATCH] interpreter: use typed_pos_args for func_import and make the helper method private --- mesonbuild/interpreter/interpreter.py | 34 +++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index 693924f7f..dc2fd1a55 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -601,35 +601,33 @@ class Interpreter(InterpreterBase, HoldableObject): dep = df.lookup(kwargs, force_fallback=True) self.build.stdlibs[for_machine][l] = dep - def import_module(self, modname): - if modname in self.modules: - return - try: - module = importlib.import_module('mesonbuild.modules.' + modname) - except ImportError: - raise InvalidArguments(f'Module "{modname}" does not exist') - ext_module = module.initialize(self) - assert isinstance(ext_module, ModuleObject) - self.modules[modname] = ext_module - - @stringArgs + @typed_pos_args('import', str) @noKwargs - def func_import(self, node, args, kwargs): - if len(args) != 1: - raise InvalidCode('Import takes one argument.') + def func_import(self, node: mparser.BaseNode, args: T.Tuple[str], kwargs) -> ModuleObject: modname = args[0] if modname.startswith('unstable-'): plainname = modname.split('-', 1)[1] try: # check if stable module exists - self.import_module(plainname) + self._import_module(plainname) mlog.warning(f'Module {modname} is now stable, please use the {plainname} module instead.') modname = plainname except InvalidArguments: mlog.warning('Module %s has no backwards or forwards compatibility and might not exist in future releases.' % modname, location=node) modname = 'unstable_' + plainname - self.import_module(modname) - return self.modules[modname] + + if modname in self.modules: + return self.modules[modname] + + try: + module = importlib.import_module('mesonbuild.modules.' + modname) + except ImportError: + raise InvalidArguments(f'Module "{modname}" does not exist') + ext_module = module.initialize(self) + assert isinstance(ext_module, ModuleObject) + self.modules[modname] = ext_module + + return ext_module @stringArgs @noKwargs