From c637b913c94817c81e9a35287b995741a7089413 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Sun, 30 Aug 2020 13:26:41 +0200 Subject: [PATCH] typing: fully annotate mcompile, minit, and msetup --- mesonbuild/mcompile.py | 12 ++++++------ mesonbuild/minit.py | 12 ++++++++---- mesonbuild/msetup.py | 10 +++++----- run_mypy.py | 3 +++ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/mesonbuild/mcompile.py b/mesonbuild/mcompile.py index 8e2a38fbf..5466ad257 100644 --- a/mesonbuild/mcompile.py +++ b/mesonbuild/mcompile.py @@ -32,7 +32,7 @@ if T.TYPE_CHECKING: import argparse def array_arg(value: str) -> T.List[str]: - return UserArrayOption(None, value, allow_dups=True, user_input=True).value + return T.cast(T.List[str], UserArrayOption(None, value, allow_dups=True, user_input=True).value) def validate_builddir(builddir: Path) -> None: if not (builddir / 'meson-private' / 'coredata.dat' ).is_file(): @@ -45,7 +45,7 @@ def get_backend_from_coredata(builddir: Path) -> str: """ Gets `backend` option value from coredata """ - return coredata.load(str(builddir)).get_builtin_option('backend') + return T.cast(str, coredata.load(str(builddir)).get_builtin_option('backend')) def parse_introspect_data(builddir: Path) -> T.Dict[str, T.List[dict]]: """ @@ -97,12 +97,12 @@ class ParsedTargetName: } return type in allowed_types -def get_target_from_intro_data(target: ParsedTargetName, builddir: Path, introspect_data: dict) -> dict: +def get_target_from_intro_data(target: ParsedTargetName, builddir: Path, introspect_data: T.Dict[str, T.Any]) -> T.Dict[str, T.Any]: if target.name not in introspect_data: raise MesonException('Can\'t invoke target `{}`: target not found'.format(target.full_name)) intro_targets = introspect_data[target.name] - found_targets = [] + found_targets = [] # type: T.List[T.Dict[str, T.Any]] resolved_bdir = builddir.resolve() @@ -169,9 +169,9 @@ def generate_target_name_vs(target: ParsedTargetName, builddir: Path, introspect # Normalize project name # Source: https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-build-specific-targets-in-solutions-by-using-msbuild-exe - target_name = re.sub('[\%\$\@\;\.\(\)\']', '_', intro_target['id']) + target_name = re.sub('[\%\$\@\;\.\(\)\']', '_', intro_target['id']) # type: str rel_path = Path(intro_target['filename'][0]).relative_to(builddir.resolve()).parent - if rel_path != '.': + if rel_path != Path('.'): target_name = str(rel_path / target_name) return target_name diff --git a/mesonbuild/minit.py b/mesonbuild/minit.py index 06e6dd4f3..4a38313a1 100644 --- a/mesonbuild/minit.py +++ b/mesonbuild/minit.py @@ -25,6 +25,10 @@ from glob import glob from mesonbuild import mesonlib from mesonbuild.environment import detect_ninja from mesonbuild.templates.samplefactory import sameple_generator +import typing as T + +if T.TYPE_CHECKING: + import argparse ''' we currently have one meson template at this time. @@ -49,7 +53,7 @@ meson compile -C builddir ''' -def create_sample(options) -> None: +def create_sample(options: 'argparse.Namespace') -> None: ''' Based on what arguments are passed we check for a match in language then check for project type and create new Meson samples project. @@ -63,7 +67,7 @@ def create_sample(options) -> None: raise RuntimeError('Unreachable code') print(INFO_MESSAGE) -def autodetect_options(options, sample: bool = False) -> None: +def autodetect_options(options: 'argparse.Namespace', sample: bool = False) -> None: ''' Here we autodetect options for args not passed in so don't have to think about it. @@ -129,7 +133,7 @@ def autodetect_options(options, sample: bool = False) -> None: raise SystemExit("Can't autodetect language, please specify it with -l.") print("Detected language: " + options.language) -def add_arguments(parser): +def add_arguments(parser: 'argparse.ArgumentParser') -> None: ''' Here we add args for that the user can passed when making a new Meson project. @@ -146,7 +150,7 @@ def add_arguments(parser): parser.add_argument('--type', default=DEFAULT_PROJECT, choices=('executable', 'library'), help="project type. default: {} based project".format(DEFAULT_PROJECT)) parser.add_argument('--version', default=DEFAULT_VERSION, help="project version. default: {}".format(DEFAULT_VERSION)) -def run(options) -> int: +def run(options: 'argparse.Namespace') -> int: ''' Here we generate the new Meson sample project. ''' diff --git a/mesonbuild/msetup.py b/mesonbuild/msetup.py index 252151194..f940a5837 100644 --- a/mesonbuild/msetup.py +++ b/mesonbuild/msetup.py @@ -31,7 +31,7 @@ from . import mintro from .mconf import make_lower_case from .mesonlib import MesonException -def add_arguments(parser): +def add_arguments(parser: argparse.ArgumentParser) -> None: coredata.register_builtin_arguments(parser) parser.add_argument('--native-file', default=[], @@ -59,7 +59,7 @@ def add_arguments(parser): parser.add_argument('sourcedir', nargs='?', default=None) class MesonApp: - def __init__(self, options): + def __init__(self, options: argparse.Namespace) -> None: (self.source_dir, self.build_dir) = self.validate_dirs(options.builddir, options.sourcedir, options.reconfigure, @@ -150,7 +150,7 @@ class MesonApp: raise SystemExit('Directory does not contain a valid build tree:\n{}'.format(build_dir)) return src_dir, build_dir - def generate(self): + def generate(self) -> None: env = environment.Environment(self.source_dir, self.build_dir, self.options) mlog.initialize(env.get_log_dir(), self.options.fatal_warnings) if self.options.profile: @@ -158,7 +158,7 @@ class MesonApp: with mesonlib.BuildDirLock(self.build_dir): self._generate(env) - def _generate(self, env): + def _generate(self, env: environment.Environment) -> None: mlog.debug('Build started at', datetime.datetime.now().isoformat()) mlog.debug('Main binary:', sys.executable) mlog.debug('Build Options:', coredata.get_cmd_line_options(self.build_dir, self.options)) @@ -239,7 +239,7 @@ class MesonApp: os.unlink(cdf) raise -def run(options) -> int: +def run(options: argparse.Namespace) -> int: coredata.parse_cmd_line_options(options) app = MesonApp(options) app.generate() diff --git a/run_mypy.py b/run_mypy.py index 3369b1a76..38457325b 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -32,7 +32,10 @@ normal_modules = [ strict_modules = [ 'mesonbuild/interpreterbase.py', + 'mesonbuild/minit.py', 'mesonbuild/mparser.py', + 'mesonbuild/msetup.py', + 'mesonbuild/mcompile.py', 'mesonbuild/mesonlib.py', 'mesonbuild/mlog.py', 'mesonbuild/ast',