aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2023-12-13 13:35:37 -0800
committerEli Schwartz <eschwartz93@gmail.com>2024-01-08 19:33:05 -0500
commit7d0d610045b9ae5860eb1c7e0e50bb04eaa17673 (patch)
tree8dd348f390f9ebd77164e4be86bf150cfa36e530
parent25fa0e97216922a5d81181090c531493f1df1055 (diff)
downloadmeson-7d0d610045b9ae5860eb1c7e0e50bb04eaa17673.zip
meson-7d0d610045b9ae5860eb1c7e0e50bb04eaa17673.tar.gz
meson-7d0d610045b9ae5860eb1c7e0e50bb04eaa17673.tar.bz2
ast/introspection: make mypy clean
-rw-r--r--mesonbuild/ast/interpreter.py22
-rw-r--r--mesonbuild/ast/introspection.py37
-rw-r--r--mesonbuild/build.py2
-rwxr-xr-xrun_mypy.py1
4 files changed, 32 insertions, 30 deletions
diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py
index b6f7f99..783387d 100644
--- a/mesonbuild/ast/interpreter.py
+++ b/mesonbuild/ast/interpreter.py
@@ -49,7 +49,7 @@ from ..mparser import (
if T.TYPE_CHECKING:
from .visitor import AstVisitor
from ..interpreter import Interpreter
- from ..interpreterbase import TYPE_nkwargs, TYPE_nvar
+ from ..interpreterbase import TYPE_nkwargs, TYPE_var
from ..mparser import (
AndNode,
ComparisonNode,
@@ -159,7 +159,7 @@ class AstInterpreter(InterpreterBase):
def _holderify(self, res: _T) -> _T:
return res
- def func_do_nothing(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> bool:
+ def func_do_nothing(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> bool:
return True
def load_root_meson_file(self) -> None:
@@ -167,7 +167,7 @@ class AstInterpreter(InterpreterBase):
for i in self.visitors:
self.ast.accept(i)
- def func_subdir(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ def func_subdir(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> None:
args = self.flatten_args(args)
if len(args) != 1 or not isinstance(args[0], str):
sys.stderr.write(f'Unable to evaluate subdir({args}) in AstInterpreter --> Skipping\n')
@@ -210,7 +210,7 @@ class AstInterpreter(InterpreterBase):
assert isinstance(node, mparser.FormatStringNode)
return node.value
- def evaluate_arraystatement(self, cur: mparser.ArrayNode) -> TYPE_nvar:
+ def evaluate_arraystatement(self, cur: mparser.ArrayNode) -> TYPE_var:
return self.reduce_arguments(cur.args)[0]
def evaluate_arithmeticstatement(self, cur: ArithmeticNode) -> int:
@@ -261,9 +261,9 @@ class AstInterpreter(InterpreterBase):
args: mparser.ArgumentNode,
key_resolver: T.Callable[[mparser.BaseNode], str] = default_resolve_key,
duplicate_key_error: T.Optional[str] = None,
- ) -> T.Tuple[T.List[TYPE_nvar], TYPE_nkwargs]:
+ ) -> T.Tuple[T.List[TYPE_var], TYPE_nkwargs]:
if isinstance(args, ArgumentNode):
- kwargs: T.Dict[str, TYPE_nvar] = {}
+ kwargs: T.Dict[str, TYPE_var] = {}
for key, val in args.kwargs.items():
kwargs[key_resolver(key)] = val
if args.incorrect_order():
@@ -372,7 +372,7 @@ class AstInterpreter(InterpreterBase):
elif isinstance(node, MethodNode):
src = quick_resolve(node.source_object)
margs = self.flatten_args(node.args.arguments, include_unknown_args, id_loop_detect)
- mkwargs: T.Dict[str, TYPE_nvar] = {}
+ mkwargs: T.Dict[str, TYPE_var] = {}
method_name = node.name.value
try:
if isinstance(src, str):
@@ -392,7 +392,7 @@ class AstInterpreter(InterpreterBase):
if isinstance(result, BaseNode):
result = self.resolve_node(result, include_unknown_args, id_loop_detect)
elif isinstance(result, list):
- new_res: T.List[TYPE_nvar] = []
+ new_res: T.List[TYPE_var] = []
for i in result:
if isinstance(i, BaseNode):
resolved = self.resolve_node(i, include_unknown_args, id_loop_detect)
@@ -404,14 +404,14 @@ class AstInterpreter(InterpreterBase):
return result
- def flatten_args(self, args_raw: T.Union[TYPE_nvar, T.Sequence[TYPE_nvar]], include_unknown_args: bool = False, id_loop_detect: T.Optional[T.List[str]] = None) -> T.List[TYPE_nvar]:
+ def flatten_args(self, args_raw: T.Union[TYPE_var, T.Sequence[TYPE_var]], include_unknown_args: bool = False, id_loop_detect: T.Optional[T.List[str]] = None) -> T.List[TYPE_var]:
# Make sure we are always dealing with lists
if isinstance(args_raw, list):
args = args_raw
else:
args = [args_raw]
- flattened_args: T.List[TYPE_nvar] = []
+ flattened_args: T.List[TYPE_var] = []
# Resolve the contents of args
for i in args:
@@ -425,7 +425,7 @@ class AstInterpreter(InterpreterBase):
flattened_args += [i]
return flattened_args
- def flatten_kwargs(self, kwargs: T.Dict[str, TYPE_nvar], include_unknown_args: bool = False) -> T.Dict[str, TYPE_nvar]:
+ def flatten_kwargs(self, kwargs: T.Dict[str, TYPE_var], include_unknown_args: bool = False) -> T.Dict[str, TYPE_var]:
flattened_kwargs = {}
for key, val in kwargs.items():
if isinstance(val, BaseNode):
diff --git a/mesonbuild/ast/introspection.py b/mesonbuild/ast/introspection.py
index 9a39314..49e58f4 100644
--- a/mesonbuild/ast/introspection.py
+++ b/mesonbuild/ast/introspection.py
@@ -21,7 +21,7 @@ from .interpreter import AstInterpreter
if T.TYPE_CHECKING:
from ..build import BuildTarget
- from ..interpreterbase import TYPE_nvar
+ from ..interpreterbase import TYPE_var
from .visitor import AstVisitor
@@ -85,7 +85,7 @@ class IntrospectionInterpreter(AstInterpreter):
'both_libraries': self.func_both_lib,
})
- def func_project(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ def func_project(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> None:
if self.project_node:
raise InvalidArguments('Second call to project()')
self.project_node = node
@@ -146,9 +146,10 @@ class IntrospectionInterpreter(AstInterpreter):
except (mesonlib.MesonException, RuntimeError):
return
- def func_add_languages(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ def func_add_languages(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> None:
kwargs = self.flatten_kwargs(kwargs)
required = kwargs.get('required', True)
+ assert isinstance(required, (bool, cdata.UserFeatureOption)), 'for mypy'
if isinstance(required, cdata.UserFeatureOption):
required = required.is_enabled()
if 'native' in kwargs:
@@ -158,7 +159,7 @@ class IntrospectionInterpreter(AstInterpreter):
for for_machine in [MachineChoice.BUILD, MachineChoice.HOST]:
self._add_languages(args, required, for_machine)
- def _add_languages(self, raw_langs: T.List[TYPE_nvar], required: bool, for_machine: MachineChoice) -> None:
+ def _add_languages(self, raw_langs: T.List[TYPE_var], required: bool, for_machine: MachineChoice) -> None:
langs: T.List[str] = []
for l in self.flatten_args(raw_langs):
if isinstance(l, str):
@@ -185,7 +186,7 @@ class IntrospectionInterpreter(AstInterpreter):
options[k] = v
self.coredata.add_compiler_options(options, lang, for_machine, self.environment)
- def func_dependency(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ def func_dependency(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> None:
args = self.flatten_args(args)
kwargs = self.flatten_kwargs(kwargs)
if not args:
@@ -209,7 +210,7 @@ class IntrospectionInterpreter(AstInterpreter):
'node': node
}]
- def build_target(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs_raw: T.Dict[str, TYPE_nvar], targetclass: T.Type[BuildTarget]) -> T.Optional[T.Dict[str, T.Any]]:
+ def build_target(self, node: BaseNode, args: T.List[TYPE_var], kwargs_raw: T.Dict[str, TYPE_var], targetclass: T.Type[BuildTarget]) -> T.Optional[T.Dict[str, T.Any]]:
args = self.flatten_args(args)
if not args or not isinstance(args[0], str):
return None
@@ -270,7 +271,7 @@ class IntrospectionInterpreter(AstInterpreter):
empty_sources: T.List[T.Any] = []
# Passing the unresolved sources list causes errors
kwargs_reduced['_allow_no_sources'] = True
- target = targetclass(name, self.subdir, self.subproject, for_machine, empty_sources, [], objects,
+ target = targetclass(name, self.subdir, self.subproject, for_machine, empty_sources, None, objects,
self.environment, self.coredata.compilers[for_machine], kwargs_reduced)
target.process_compilers_late()
@@ -292,7 +293,7 @@ class IntrospectionInterpreter(AstInterpreter):
self.targets += [new_target]
return new_target
- def build_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def build_library(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
default_library = self.coredata.get_option(OptionKey('default_library'))
if default_library == 'shared':
return self.build_target(node, args, kwargs, SharedLibrary)
@@ -302,28 +303,28 @@ class IntrospectionInterpreter(AstInterpreter):
return self.build_target(node, args, kwargs, SharedLibrary)
return None
- def func_executable(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_executable(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, Executable)
- def func_static_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_static_lib(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, StaticLibrary)
- def func_shared_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_shared_lib(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, SharedLibrary)
- def func_both_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_both_lib(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, SharedLibrary)
- def func_shared_module(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_shared_module(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, SharedModule)
- def func_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_library(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_library(node, args, kwargs)
- def func_jar(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_jar(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
return self.build_target(node, args, kwargs, Jar)
- def func_build_target(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ def func_build_target(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Optional[T.Dict[str, T.Any]]:
if 'target_type' not in kwargs:
return None
target_type = kwargs.pop('target_type')
@@ -358,11 +359,11 @@ class IntrospectionInterpreter(AstInterpreter):
and also calls parse_project() on every subproject.
'''
if not self.ast.lines:
- return
+ return None
project = self.ast.lines[0]
# first line is always project()
if not isinstance(project, FunctionNode):
- return
+ return None
for kw, val in project.args.kwargs.items():
assert isinstance(kw, IdNode), 'for mypy'
if kw.value == 'subproject_dir':
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 68513bb..51c4834 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -887,7 +887,7 @@ class BuildTarget(Target):
removed = True
return removed
- def process_compilers_late(self):
+ def process_compilers_late(self) -> None:
"""Processes additional compilers after kwargs have been evaluated.
This can add extra compilers that might be required by keyword
diff --git a/run_mypy.py b/run_mypy.py
index 8e04a76..a9b52d9 100755
--- a/run_mypy.py
+++ b/run_mypy.py
@@ -24,6 +24,7 @@ modules = [
'mesonbuild/wrap/',
# specific files
+ 'mesonbuild/ast/introspection.py',
'mesonbuild/ast/printer.py',
'mesonbuild/ast/postprocess.py',
'mesonbuild/ast/visitor.py',