modules/keyval: add type annotations
And use typed_pos_args
# limitations under the License.
import os
+import typing as T
from . import ExtensionModule
from .. import mesonlib
-from ..interpreterbase import FeatureNew, noKwargs, InvalidCode
-from ..mesonlib import typeslistify
+from ..interpreterbase import FeatureNew, noKwargs, typed_pos_args
+
+if T.TYPE_CHECKING:
+ from ..interpreter import Interpreter
+ from . import ModuleState
class KeyvalModule(ExtensionModule):
@FeatureNew('Keyval Module', '0.55.0')
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
+ def __init__(self, interp: 'Interpreter'):
+ super().__init__(interp)
self.methods.update({
'load': self.load,
})
- def _load_file(self, path_to_config):
- result = dict()
+ @staticmethod
+ def _load_file(path_to_config: str) -> T.Dict[str, str]:
+ result: T.Dict[str, str] = {}
try:
with open(path_to_config, encoding='utf-8') as f:
for line in f:
@@ -48,12 +53,9 @@ class KeyvalModule(ExtensionModule):
return result
@noKwargs
- def load(self, state, args, kwargs):
- sources = typeslistify(args, (str, mesonlib.File))
- if len(sources) != 1:
- raise InvalidCode('load takes only one file input.')
-
- s = sources[0]
+ @typed_pos_args('keyval.laod', (str, mesonlib.File))
+ def load(self, state: 'ModuleState', args: T.Tuple['mesonlib.FileOrString'], kwargs: T.Dict[str, T.Any]) -> T.Dict[str, str]:
+ s = args[0]
is_built = False
if isinstance(s, mesonlib.File):
is_built = is_built or s.is_built
@@ -67,5 +69,5 @@ class KeyvalModule(ExtensionModule):
return self._load_file(s)
-def initialize(*args, **kwargs):
- return KeyvalModule(*args, **kwargs)
+def initialize(interp: 'Interpreter') -> KeyvalModule:
+ return KeyvalModule(interp)
