Skip to content

Commit

Permalink
ast/introspection: make mypy clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dcbaker committed Dec 14, 2023
1 parent 179a9b5 commit aa67d0f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
22 changes: 11 additions & 11 deletions mesonbuild/ast/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
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,
Expand Down Expand Up @@ -159,15 +159,15 @@ def _unholder_args(self, args: _T, kwargs: _V) -> T.Tuple[_T, _V]:
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:
super().load_root_meson_file()
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')
Expand Down Expand Up @@ -210,7 +210,7 @@ def evaluate_fstring(self, node: mparser.FormatStringNode) -> str:
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:
Expand Down Expand Up @@ -261,9 +261,9 @@ def reduce_arguments(
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():
Expand Down Expand Up @@ -372,7 +372,7 @@ def quick_resolve(n: BaseNode, loop_detect: T.Optional[T.List[str]] = None) -> T
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):
Expand All @@ -392,7 +392,7 @@ def quick_resolve(n: BaseNode, loop_detect: T.Optional[T.List[str]] = None) -> T
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)
Expand All @@ -404,14 +404,14 @@ def quick_resolve(n: BaseNode, loop_detect: T.Optional[T.List[str]] = None) -> T

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:
Expand All @@ -425,7 +425,7 @@ def flatten_args(self, args_raw: T.Union[TYPE_nvar, T.Sequence[TYPE_nvar]], incl
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):
Expand Down
37 changes: 19 additions & 18 deletions mesonbuild/ast/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

if T.TYPE_CHECKING:
from ..build import BuildTarget
from ..interpreterbase import TYPE_nvar
from ..interpreterbase import TYPE_var
from .visitor import AstVisitor


Expand Down Expand Up @@ -85,7 +85,7 @@ def __init__(self,
'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
Expand Down Expand Up @@ -146,9 +146,10 @@ def do_subproject(self, dirname: str) -> None:
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:
Expand All @@ -158,7 +159,7 @@ def func_add_languages(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.
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):
Expand All @@ -185,7 +186,7 @@ def _add_languages(self, raw_langs: T.List[TYPE_nvar], required: bool, for_machi
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:
Expand All @@ -209,7 +210,7 @@ def func_dependency(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dic
'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
Expand Down Expand Up @@ -270,7 +271,7 @@ def traverse_nodes(inqueue: T.List[BaseNode]) -> T.List[BaseNode]:
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()

Expand All @@ -292,7 +293,7 @@ def traverse_nodes(inqueue: T.List[BaseNode]) -> T.List[BaseNode]:
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)
Expand All @@ -302,28 +303,28 @@ def build_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[
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')
Expand Down Expand Up @@ -358,11 +359,11 @@ def extract_subproject_dir(self) -> T.Optional[str]:
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':
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ def can_compile_remove_sources(compiler: 'Compiler', sources: T.List['FileOrStri
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
Expand Down
1 change: 1 addition & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'mesonbuild/wrap/',

# specific files
'mesonbuild/ast/introspection.py',
'mesonbuild/ast/printer.py',
'mesonbuild/ast/postprocess.py',
'mesonbuild/ast/visitor.py',
Expand Down

0 comments on commit aa67d0f

Please sign in to comment.