migrate some type comments to modern type annotations

flake8 6 upgrades to pyflakes 3, and in turn this means that support for
parsing `# type: ` style annotations has been removed.

https://github.com/PyCQA/pyflakes/pull/684

This caused one file to fail linting, because it had a typing import
which was only used by a type comment.

```
mesonbuild/cmake/interpreter.py:55:5: F401 '.common.CMakeConfiguration' imported but unused
```

Updating it to actual annotations allows pyflakes to detect its usage
again, and flake8 passes. Do the whole file while we are here.
pull/11104/head
Eli Schwartz 2 years ago
parent f645bcf68d
commit 9d1b59fa7f
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 108
      mesonbuild/cmake/interpreter.py

@ -131,7 +131,7 @@ class OutputTargetMap:
rm_so_version = re.compile(r'(\.[0-9]+)+$') rm_so_version = re.compile(r'(\.[0-9]+)+$')
def __init__(self, build_dir: Path): def __init__(self, build_dir: Path):
self.tgt_map = {} # type: T.Dict[str, T.Union['ConverterTarget', 'ConverterCustomTarget']] self.tgt_map: T.Dict[str, T.Union['ConverterTarget', 'ConverterCustomTarget']] = {}
self.build_dir = build_dir self.build_dir = build_dir
def add(self, tgt: T.Union['ConverterTarget', 'ConverterCustomTarget']) -> None: def add(self, tgt: T.Union['ConverterTarget', 'ConverterCustomTarget']) -> None:
@ -220,38 +220,38 @@ class ConverterTarget:
self.full_name = target.full_name self.full_name = target.full_name
self.type = target.type self.type = target.type
self.install = target.install self.install = target.install
self.install_dir = None # type: T.Optional[Path] self.install_dir: T.Optional[Path] = None
self.link_libraries = target.link_libraries self.link_libraries = target.link_libraries
self.link_flags = target.link_flags + target.link_lang_flags self.link_flags = target.link_flags + target.link_lang_flags
self.depends_raw = [] # type: T.List[str] self.depends_raw: T.List[str] = []
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
if target.install_paths: if target.install_paths:
self.install_dir = target.install_paths[0] self.install_dir = target.install_paths[0]
self.languages = set() # type: T.Set[str] self.languages: T.Set[str] = set()
self.sources = [] # type: T.List[Path] self.sources: T.List[Path] = []
self.generated = [] # type: T.List[Path] self.generated: T.List[Path] = []
self.generated_ctgt = [] # type: T.List[CustomTargetReference] self.generated_ctgt: T.List[CustomTargetReference] = []
self.includes = [] # type: T.List[Path] self.includes: T.List[Path] = []
self.sys_includes = [] # type: T.List[Path] self.sys_includes: T.List[Path] = []
self.link_with = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] self.link_with: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
self.object_libs = [] # type: T.List[ConverterTarget] self.object_libs: T.List[ConverterTarget] = []
self.compile_opts = {} # type: T.Dict[str, T.List[str]] self.compile_opts: T.Dict[str, T.List[str]] = {}
self.public_compile_opts = [] # type: T.List[str] self.public_compile_opts: T.List[str] = []
self.pie = False self.pie = False
# Project default override options (c_std, cpp_std, etc.) # Project default override options (c_std, cpp_std, etc.)
self.override_options = [] # type: T.List[str] self.override_options: T.List[str] = []
# Convert the target name to a valid meson target name # Convert the target name to a valid meson target name
self.name = _sanitize_cmake_name(self.name) self.name = _sanitize_cmake_name(self.name)
self.generated_raw = [] # type: T.List[Path] self.generated_raw: T.List[Path] = []
for i in target.files: for i in target.files:
languages = set() # type: T.Set[str] languages: T.Set[str] = set()
src_suffixes = set() # type: T.Set[str] src_suffixes: T.Set[str] = set()
# Insert suffixes # Insert suffixes
for j in i.sources: for j in i.sources:
@ -517,7 +517,7 @@ class ConverterTarget:
@lru_cache(maxsize=None) @lru_cache(maxsize=None)
def _all_source_suffixes(self) -> 'ImmutableListProtocol[str]': def _all_source_suffixes(self) -> 'ImmutableListProtocol[str]':
suffixes = [] # type: T.List[str] suffixes: T.List[str] = []
for exts in lang_suffixes.values(): for exts in lang_suffixes.values():
suffixes.extend(exts) suffixes.extend(exts)
return suffixes return suffixes
@ -612,12 +612,12 @@ class ConverterCustomTarget:
self.cmake_name = str(self.name) self.cmake_name = str(self.name)
self.original_outputs = list(target.outputs) self.original_outputs = list(target.outputs)
self.outputs = [x.name for x in self.original_outputs] self.outputs = [x.name for x in self.original_outputs]
self.conflict_map = {} # type: T.Dict[str, str] self.conflict_map: T.Dict[str, str] = {}
self.command = [] # type: T.List[T.List[T.Union[str, ConverterTarget]]] self.command: T.List[T.List[T.Union[str, ConverterTarget]]] = []
self.working_dir = target.working_dir self.working_dir = target.working_dir
self.depends_raw = target.depends self.depends_raw = target.depends
self.inputs = [] # type: T.List[T.Union[str, CustomTargetReference]] self.inputs: T.List[T.Union[str, CustomTargetReference]] = []
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
self.current_bin_dir = target.current_bin_dir self.current_bin_dir = target.current_bin_dir
self.current_src_dir = target.current_src_dir self.current_src_dir = target.current_src_dir
self.env = env self.env = env
@ -652,7 +652,7 @@ class ConverterCustomTarget:
# Ensure that there is no duplicate output in the project so # Ensure that there is no duplicate output in the project so
# that meson can handle cases where the same filename is # that meson can handle cases where the same filename is
# generated in multiple directories # generated in multiple directories
temp_outputs = [] # type: T.List[str] temp_outputs: T.List[str] = []
for i in self.outputs: for i in self.outputs:
if i in all_outputs: if i in all_outputs:
old = str(i) old = str(i)
@ -664,11 +664,11 @@ class ConverterCustomTarget:
self.outputs = temp_outputs self.outputs = temp_outputs
# Check if the command is a build target # Check if the command is a build target
commands = [] # type: T.List[T.List[T.Union[str, ConverterTarget]]] commands: T.List[T.List[T.Union[str, ConverterTarget]]] = []
for curr_cmd in self._raw_target.command: for curr_cmd in self._raw_target.command:
assert isinstance(curr_cmd, list) assert isinstance(curr_cmd, list)
assert curr_cmd[0] != '', "An empty string is not a valid executable" assert curr_cmd[0] != '', "An empty string is not a valid executable"
cmd = [] # type: T.List[T.Union[str, ConverterTarget]] cmd: T.List[T.Union[str, ConverterTarget]] = []
for j in curr_cmd: for j in curr_cmd:
if not j: if not j:
@ -775,25 +775,25 @@ class CMakeInterpreter:
self.env = env self.env = env
self.for_machine = MachineChoice.HOST # TODO make parameter self.for_machine = MachineChoice.HOST # TODO make parameter
self.backend_name = backend.name self.backend_name = backend.name
self.linkers = set() # type: T.Set[str] self.linkers: T.Set[str] = set()
self.fileapi = CMakeFileAPI(self.build_dir) self.fileapi = CMakeFileAPI(self.build_dir)
# Raw CMake results # Raw CMake results
self.bs_files = [] # type: T.List[Path] self.bs_files: T.List[Path] = []
self.codemodel_configs = None # type: T.Optional[T.List[CMakeConfiguration]] self.codemodel_configs: T.Optional[T.List[CMakeConfiguration]] = None
self.cmake_stderr = None # type: T.Optional[str] self.cmake_stderr: T.Optional[str] = None
# Analysed data # Analysed data
self.project_name = '' self.project_name = ''
self.languages = [] # type: T.List[str] self.languages: T.List[str] = []
self.targets = [] # type: T.List[ConverterTarget] self.targets: T.List[ConverterTarget] = []
self.custom_targets = [] # type: T.List[ConverterCustomTarget] self.custom_targets: T.List[ConverterCustomTarget] = []
self.trace: CMakeTraceParser self.trace: CMakeTraceParser
self.output_target_map = OutputTargetMap(self.build_dir) self.output_target_map = OutputTargetMap(self.build_dir)
# Generated meson data # Generated meson data
self.generated_targets = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]] self.generated_targets: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
self.internal_name_map = {} # type: T.Dict[str, str] self.internal_name_map: T.Dict[str, str] = {}
# Do some special handling for object libraries for certain configurations # Do some special handling for object libraries for certain configurations
self._object_lib_workaround = False self._object_lib_workaround = False
@ -890,7 +890,7 @@ class CMakeInterpreter:
self.trace.parse(self.cmake_stderr) self.trace.parse(self.cmake_stderr)
# Find all targets # Find all targets
added_target_names = [] # type: T.List[str] added_target_names: T.List[str] = []
for i_0 in self.codemodel_configs: for i_0 in self.codemodel_configs:
for j_0 in i_0.projects: for j_0 in i_0.projects:
if not self.project_name: if not self.project_name:
@ -927,7 +927,7 @@ class CMakeInterpreter:
# First pass: Basic target cleanup # First pass: Basic target cleanup
object_libs = [] object_libs = []
custom_target_outputs = [] # type: T.List[str] custom_target_outputs: T.List[str] = []
for ctgt in self.custom_targets: for ctgt in self.custom_targets:
ctgt.postprocess(self.output_target_map, self.src_dir, custom_target_outputs, self.trace) ctgt.postprocess(self.output_target_map, self.src_dir, custom_target_outputs, self.trace)
for tgt in self.targets: for tgt in self.targets:
@ -1026,9 +1026,9 @@ class CMakeInterpreter:
# Add the run script for custom commands # Add the run script for custom commands
# Add the targets # Add the targets
processing = [] # type: T.List[str] processing: T.List[str] = []
processed = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]] processed: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
name_map = {} # type: T.Dict[str, str] name_map: T.Dict[str, str] = {}
def extract_tgt(tgt: T.Union[ConverterTarget, ConverterCustomTarget, CustomTargetReference]) -> IdNode: def extract_tgt(tgt: T.Union[ConverterTarget, ConverterCustomTarget, CustomTargetReference]) -> IdNode:
tgt_name = None tgt_name = None
@ -1056,13 +1056,13 @@ class CMakeInterpreter:
detect_cycle(tgt) detect_cycle(tgt)
# First handle inter target dependencies # First handle inter target dependencies
link_with = [] # type: T.List[IdNode] link_with: T.List[IdNode] = []
objec_libs = [] # type: T.List[IdNode] objec_libs: T.List[IdNode] = []
sources = [] # type: T.List[Path] sources: T.List[Path] = []
generated = [] # type: T.List[T.Union[IdNode, IndexNode]] generated: T.List[T.Union[IdNode, IndexNode]] = []
generated_filenames = [] # type: T.List[str] generated_filenames: T.List[str] = []
custom_targets = [] # type: T.List[ConverterCustomTarget] custom_targets: T.List[ConverterCustomTarget] = []
dependencies = [] # type: T.List[IdNode] dependencies: T.List[IdNode] = []
for i in tgt.link_with: for i in tgt.link_with:
assert isinstance(i, ConverterTarget) assert isinstance(i, ConverterTarget)
if i.name not in processed: if i.name not in processed:
@ -1123,7 +1123,7 @@ class CMakeInterpreter:
install_tgt = options.get_install(tgt.cmake_name, tgt.install) install_tgt = options.get_install(tgt.cmake_name, tgt.install)
# Generate target kwargs # Generate target kwargs
tgt_kwargs = { tgt_kwargs: TYPE_mixed_kwargs = {
'build_by_default': install_tgt, 'build_by_default': install_tgt,
'link_args': options.get_link_args(tgt.cmake_name, tgt.link_flags + tgt.link_libraries), 'link_args': options.get_link_args(tgt.cmake_name, tgt.link_flags + tgt.link_libraries),
'link_with': link_with, 'link_with': link_with,
@ -1131,7 +1131,7 @@ class CMakeInterpreter:
'install': install_tgt, 'install': install_tgt,
'override_options': options.get_override_options(tgt.cmake_name, tgt.override_options), 'override_options': options.get_override_options(tgt.cmake_name, tgt.override_options),
'objects': [method(x, 'extract_all_objects') for x in objec_libs], 'objects': [method(x, 'extract_all_objects') for x in objec_libs],
} # type: TYPE_mixed_kwargs }
# Only set if installed and only override if it is set # Only set if installed and only override if it is set
if install_tgt and tgt.install_dir: if install_tgt and tgt.install_dir:
@ -1148,12 +1148,12 @@ class CMakeInterpreter:
tgt_kwargs['pic'] = tgt.pie tgt_kwargs['pic'] = tgt.pie
# declare_dependency kwargs # declare_dependency kwargs
dep_kwargs = { dep_kwargs: TYPE_mixed_kwargs = {
'link_args': tgt.link_flags + tgt.link_libraries, 'link_args': tgt.link_flags + tgt.link_libraries,
'link_with': id_node(tgt_var), 'link_with': id_node(tgt_var),
'compile_args': tgt.public_compile_opts, 'compile_args': tgt.public_compile_opts,
'include_directories': id_node(inc_var), 'include_directories': id_node(inc_var),
} # type: TYPE_mixed_kwargs }
if dependencies: if dependencies:
generated += dependencies generated += dependencies
@ -1214,7 +1214,7 @@ class CMakeInterpreter:
return x return x
# Generate the command list # Generate the command list
command = [] # type: T.List[T.Union[str, IdNode, IndexNode]] command: T.List[T.Union[str, IdNode, IndexNode]] = []
command += mesonlib.get_meson_command() command += mesonlib.get_meson_command()
command += ['--internal', 'cmake_run_ctgt'] command += ['--internal', 'cmake_run_ctgt']
command += ['-o', '@OUTPUT@'] command += ['-o', '@OUTPUT@']
@ -1226,12 +1226,12 @@ class CMakeInterpreter:
for cmd in tgt.command: for cmd in tgt.command:
command += [resolve_source(x) for x in cmd] + [';;;'] command += [resolve_source(x) for x in cmd] + [';;;']
tgt_kwargs = { tgt_kwargs: TYPE_mixed_kwargs = {
'input': [resolve_source(x) for x in tgt.inputs], 'input': [resolve_source(x) for x in tgt.inputs],
'output': tgt.outputs, 'output': tgt.outputs,
'command': command, 'command': command,
'depends': [resolve_source(x) for x in tgt.depends], 'depends': [resolve_source(x) for x in tgt.depends],
} # type: TYPE_mixed_kwargs }
root_cb.lines += [assign(tgt_var, function('custom_target', [tgt.name], tgt_kwargs))] root_cb.lines += [assign(tgt_var, function('custom_target', [tgt.name], tgt_kwargs))]
processed[tgt.name] = {'inc': None, 'src': None, 'dep': None, 'tgt': tgt_var, 'func': 'custom_target'} processed[tgt.name] = {'inc': None, 'src': None, 'dep': None, 'tgt': tgt_var, 'func': 'custom_target'}

Loading…
Cancel
Save