Skip to content

Commit

Permalink
Merge pull request #9623 from dcbaker/submit/keyval-typeing
Browse files Browse the repository at this point in the history
Add type annotations and typed_pos_args to the keyval module
  • Loading branch information
jpakkane authored Nov 28, 2021
2 parents 99d809b + e38c4de commit 69fa37e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
33 changes: 17 additions & 16 deletions mesonbuild/modules/keyval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -49,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 @@ -68,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)
1 change: 1 addition & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'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',
Expand Down

0 comments on commit 69fa37e

Please sign in to comment.