Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.

PyCQA/pyflakes#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.
  • Loading branch information
eli-schwartz committed Nov 24, 2022
commit 9d1b59fa7ff1a1a31599a04a02c48c6b41026c2c
108 changes: 54 additions & 54 deletions mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class OutputTargetMap:
rm_so_version = re.compile(r'(\.[0-9]+)+$')

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

def add(self, tgt: T.Union['ConverterTarget', 'ConverterCustomTarget']) -> None:
Expand Down Expand Up @@ -220,38 +220,38 @@ def __init__(self, target: CMakeTarget, env: 'Environment', for_machine: Machine
self.full_name = target.full_name
self.type = target.type
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_flags = target.link_flags + target.link_lang_flags
self.depends_raw = [] # type: T.List[str]
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
self.depends_raw: T.List[str] = []
self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []

if target.install_paths:
self.install_dir = target.install_paths[0]

self.languages = set() # type: T.Set[str]
self.sources = [] # type: T.List[Path]
self.generated = [] # type: T.List[Path]
self.generated_ctgt = [] # type: T.List[CustomTargetReference]
self.includes = [] # type: T.List[Path]
self.sys_includes = [] # type: T.List[Path]
self.link_with = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
self.object_libs = [] # type: T.List[ConverterTarget]
self.compile_opts = {} # type: T.Dict[str, T.List[str]]
self.public_compile_opts = [] # type: T.List[str]
self.languages: T.Set[str] = set()
self.sources: T.List[Path] = []
self.generated: T.List[Path] = []
self.generated_ctgt: T.List[CustomTargetReference] = []
self.includes: T.List[Path] = []
self.sys_includes: T.List[Path] = []
self.link_with: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
self.object_libs: T.List[ConverterTarget] = []
self.compile_opts: T.Dict[str, T.List[str]] = {}
self.public_compile_opts: T.List[str] = []
self.pie = False

# 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
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:
languages = set() # type: T.Set[str]
src_suffixes = set() # type: T.Set[str]
languages: T.Set[str] = set()
src_suffixes: T.Set[str] = set()

# Insert suffixes
for j in i.sources:
Expand Down Expand Up @@ -517,7 +517,7 @@ def _append_objlib_sources(self, tgt: 'ConverterTarget') -> None:

@lru_cache(maxsize=None)
def _all_source_suffixes(self) -> 'ImmutableListProtocol[str]':
suffixes = [] # type: T.List[str]
suffixes: T.List[str] = []
for exts in lang_suffixes.values():
suffixes.extend(exts)
return suffixes
Expand Down Expand Up @@ -612,12 +612,12 @@ def __init__(self, target: CMakeGeneratorTarget, env: 'Environment', for_machine
self.cmake_name = str(self.name)
self.original_outputs = list(target.outputs)
self.outputs = [x.name for x in self.original_outputs]
self.conflict_map = {} # type: T.Dict[str, str]
self.command = [] # type: T.List[T.List[T.Union[str, ConverterTarget]]]
self.conflict_map: T.Dict[str, str] = {}
self.command: T.List[T.List[T.Union[str, ConverterTarget]]] = []
self.working_dir = target.working_dir
self.depends_raw = target.depends
self.inputs = [] # type: T.List[T.Union[str, CustomTargetReference]]
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
self.inputs: T.List[T.Union[str, CustomTargetReference]] = []
self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
self.current_bin_dir = target.current_bin_dir
self.current_src_dir = target.current_src_dir
self.env = env
Expand Down Expand Up @@ -652,7 +652,7 @@ def ensure_absolute(x: Path) -> Path:
# Ensure that there is no duplicate output in the project so
# that meson can handle cases where the same filename is
# generated in multiple directories
temp_outputs = [] # type: T.List[str]
temp_outputs: T.List[str] = []
for i in self.outputs:
if i in all_outputs:
old = str(i)
Expand All @@ -664,11 +664,11 @@ def ensure_absolute(x: Path) -> Path:
self.outputs = temp_outputs

# 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:
assert isinstance(curr_cmd, list)
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:
if not j:
Expand Down Expand Up @@ -775,25 +775,25 @@ def __init__(self, build: 'Build', subdir: Path, src_dir: Path, install_prefix:
self.env = env
self.for_machine = MachineChoice.HOST # TODO make parameter
self.backend_name = backend.name
self.linkers = set() # type: T.Set[str]
self.linkers: T.Set[str] = set()
self.fileapi = CMakeFileAPI(self.build_dir)

# Raw CMake results
self.bs_files = [] # type: T.List[Path]
self.codemodel_configs = None # type: T.Optional[T.List[CMakeConfiguration]]
self.cmake_stderr = None # type: T.Optional[str]
self.bs_files: T.List[Path] = []
self.codemodel_configs: T.Optional[T.List[CMakeConfiguration]] = None
self.cmake_stderr: T.Optional[str] = None

# Analysed data
self.project_name = ''
self.languages = [] # type: T.List[str]
self.targets = [] # type: T.List[ConverterTarget]
self.custom_targets = [] # type: T.List[ConverterCustomTarget]
self.languages: T.List[str] = []
self.targets: T.List[ConverterTarget] = []
self.custom_targets: T.List[ConverterCustomTarget] = []
self.trace: CMakeTraceParser
self.output_target_map = OutputTargetMap(self.build_dir)

# Generated meson data
self.generated_targets = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]]
self.internal_name_map = {} # type: T.Dict[str, str]
self.generated_targets: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
self.internal_name_map: T.Dict[str, str] = {}

# Do some special handling for object libraries for certain configurations
self._object_lib_workaround = False
Expand Down Expand Up @@ -890,7 +890,7 @@ def analyse(self) -> None:
self.trace.parse(self.cmake_stderr)

# Find all targets
added_target_names = [] # type: T.List[str]
added_target_names: T.List[str] = []
for i_0 in self.codemodel_configs:
for j_0 in i_0.projects:
if not self.project_name:
Expand Down Expand Up @@ -927,7 +927,7 @@ def analyse(self) -> None:

# First pass: Basic target cleanup
object_libs = []
custom_target_outputs = [] # type: T.List[str]
custom_target_outputs: T.List[str] = []
for ctgt in self.custom_targets:
ctgt.postprocess(self.output_target_map, self.src_dir, custom_target_outputs, self.trace)
for tgt in self.targets:
Expand Down Expand Up @@ -1026,9 +1026,9 @@ def assign(var_name: str, value: BaseNode) -> AssignmentNode:
# Add the run script for custom commands

# Add the targets
processing = [] # type: T.List[str]
processed = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]]
name_map = {} # type: T.Dict[str, str]
processing: T.List[str] = []
processed: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
name_map: T.Dict[str, str] = {}

def extract_tgt(tgt: T.Union[ConverterTarget, ConverterCustomTarget, CustomTargetReference]) -> IdNode:
tgt_name = None
Expand Down Expand Up @@ -1056,13 +1056,13 @@ def process_target(tgt: ConverterTarget) -> None:
detect_cycle(tgt)

# First handle inter target dependencies
link_with = [] # type: T.List[IdNode]
objec_libs = [] # type: T.List[IdNode]
sources = [] # type: T.List[Path]
generated = [] # type: T.List[T.Union[IdNode, IndexNode]]
generated_filenames = [] # type: T.List[str]
custom_targets = [] # type: T.List[ConverterCustomTarget]
dependencies = [] # type: T.List[IdNode]
link_with: T.List[IdNode] = []
objec_libs: T.List[IdNode] = []
sources: T.List[Path] = []
generated: T.List[T.Union[IdNode, IndexNode]] = []
generated_filenames: T.List[str] = []
custom_targets: T.List[ConverterCustomTarget] = []
dependencies: T.List[IdNode] = []
for i in tgt.link_with:
assert isinstance(i, ConverterTarget)
if i.name not in processed:
Expand Down Expand Up @@ -1123,15 +1123,15 @@ def process_target(tgt: ConverterTarget) -> None:
install_tgt = options.get_install(tgt.cmake_name, tgt.install)

# Generate target kwargs
tgt_kwargs = {
tgt_kwargs: TYPE_mixed_kwargs = {
'build_by_default': install_tgt,
'link_args': options.get_link_args(tgt.cmake_name, tgt.link_flags + tgt.link_libraries),
'link_with': link_with,
'include_directories': id_node(inc_var),
'install': install_tgt,
'override_options': options.get_override_options(tgt.cmake_name, tgt.override_options),
'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
if install_tgt and tgt.install_dir:
Expand All @@ -1148,12 +1148,12 @@ def process_target(tgt: ConverterTarget) -> None:
tgt_kwargs['pic'] = tgt.pie

# declare_dependency kwargs
dep_kwargs = {
dep_kwargs: TYPE_mixed_kwargs = {
'link_args': tgt.link_flags + tgt.link_libraries,
'link_with': id_node(tgt_var),
'compile_args': tgt.public_compile_opts,
'include_directories': id_node(inc_var),
} # type: TYPE_mixed_kwargs
}

if dependencies:
generated += dependencies
Expand Down Expand Up @@ -1214,7 +1214,7 @@ def resolve_source(x: T.Union[str, ConverterTarget, ConverterCustomTarget, Custo
return x

# 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 += ['--internal', 'cmake_run_ctgt']
command += ['-o', '@OUTPUT@']
Expand All @@ -1226,12 +1226,12 @@ def resolve_source(x: T.Union[str, ConverterTarget, ConverterCustomTarget, Custo
for cmd in tgt.command:
command += [resolve_source(x) for x in cmd] + [';;;']

tgt_kwargs = {
tgt_kwargs: TYPE_mixed_kwargs = {
'input': [resolve_source(x) for x in tgt.inputs],
'output': tgt.outputs,
'command': command,
'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))]
processed[tgt.name] = {'inc': None, 'src': None, 'dep': None, 'tgt': tgt_var, 'func': 'custom_target'}
Expand Down