From 3bbd06557686424d04859973fde21f69a85b47e6 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:11:45 -0500 Subject: [PATCH] fs: use pathlib.Path, add type hint check --- azure-pipelines.yml | 2 +- mesonbuild/modules/fs.py | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5cac9da5a..ab0c7f267 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -24,7 +24,7 @@ jobs: displayName: 'Install Pylint, MyPy' - script: pylint mesonbuild displayName: Lint Checks - - script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/ + - script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/ mesonbuild/modules/fs.py - job: vs2015 diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 61ad9178c..f4b8080f6 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -12,13 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os +import typing +from pathlib import Path from . import ExtensionModule from . import ModuleReturnValue from ..mesonlib import MesonException from ..interpreterbase import stringArgs, noKwargs +if typing.TYPE_CHECKING: + from ..interpreter import ModuleState class FSModule(ExtensionModule): @@ -28,32 +31,32 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs - def exists(self, state, args, kwargs): + def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') - test_file = os.path.join(state.source_root, state.subdir, args[0]) - return ModuleReturnValue(os.path.exists(test_file), []) + test_file = Path(state.source_root) / state.subdir / args[0] + return ModuleReturnValue(test_file.exists(), []) - def _check(self, check_fun, state, args): + def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') - test_file = os.path.join(state.source_root, state.subdir, args[0]) - return ModuleReturnValue(check_fun(test_file), []) + test_file = Path(state.source_root) / state.subdir / args[0] + return ModuleReturnValue(getattr(test_file, check)(), []) @stringArgs @noKwargs - def is_symlink(self, state, args, kwargs): - return self._check(os.path.islink, state, args) + def is_symlink(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_symlink', state, args) @stringArgs @noKwargs - def is_file(self, state, args, kwargs): - return self._check(os.path.isfile, state, args) + def is_file(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_file', state, args) @stringArgs @noKwargs - def is_dir(self, state, args, kwargs): - return self._check(os.path.isdir, state, args) + def is_dir(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_dir', state, args) -def initialize(*args, **kwargs): +def initialize(*args, **kwargs) -> FSModule: return FSModule(*args, **kwargs)