Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations and typed_pos_args to the keyval module #9623

Merged
merged 3 commits into from
Nov 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
modules/keyval: add type annotations
And use typed_pos_args
  • Loading branch information
dcbaker committed Nov 23, 2021
commit 904771c4e1a297dc37676cd9a5fdec826addfc44
30 changes: 16 additions & 14 deletions mesonbuild/modules/keyval.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,28 @@
# 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:
Expand All @@ -48,12 +53,9 @@ def _load_file(self, path_to_config):
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
Expand All @@ -67,5 +69,5 @@ def load(self, state, args, kwargs):
return self._load_file(s)


def initialize(*args, **kwargs):
return KeyvalModule(*args, **kwargs)
def initialize(interp: 'Interpreter') -> KeyvalModule:
return KeyvalModule(interp)