diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2021-11-28 18:34:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 18:34:10 +0200 |
commit | 69fa37e9ca885f55c92670bb228213db1b4ebbd8 (patch) | |
tree | 28146e44c3733868cecbee77a1375c9dbacfa226 | |
parent | 99d809b59d30d17d3ad8826209f56b7f2a0370d3 (diff) | |
parent | e38c4defcbc357f2b719027506d64fdf3eb72e03 (diff) | |
download | meson-69fa37e9ca885f55c92670bb228213db1b4ebbd8.zip meson-69fa37e9ca885f55c92670bb228213db1b4ebbd8.tar.gz meson-69fa37e9ca885f55c92670bb228213db1b4ebbd8.tar.bz2 |
Merge pull request #9623 from dcbaker/submit/keyval-typeing
Add type annotations and typed_pos_args to the keyval module
-rw-r--r-- | mesonbuild/modules/keyval.py | 33 | ||||
-rwxr-xr-x | run_mypy.py | 1 |
2 files changed, 18 insertions, 16 deletions
diff --git a/mesonbuild/modules/keyval.py b/mesonbuild/modules/keyval.py index b2d54db..7cebc91 100644 --- a/mesonbuild/modules/keyval.py +++ b/mesonbuild/modules/keyval.py @@ -12,25 +12,29 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import ExtensionModule +import os +import typing as T +from . import ExtensionModule from .. import mesonlib -from ..mesonlib import typeslistify -from ..interpreterbase import FeatureNew, noKwargs, InvalidCode +from ..interpreterbase import FeatureNew, noKwargs, typed_pos_args -import os +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: @@ -49,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 @@ -68,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) diff --git a/run_mypy.py b/run_mypy.py index d71ee86..1c2d644 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -42,6 +42,7 @@ modules = [ 'mesonbuild/modules/fs.py', 'mesonbuild/modules/i18n.py', 'mesonbuild/modules/java.py', + 'mesonbuild/modules/keyval.py', 'mesonbuild/modules/qt.py', 'mesonbuild/modules/unstable_external_project.py', 'mesonbuild/modules/unstable_rust.py', |