Skip to content

Commit 9d1b59f

Browse files
committed
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.
1 parent f645bcf commit 9d1b59f

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

mesonbuild/cmake/interpreter.py

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class OutputTargetMap:
131131
rm_so_version = re.compile(r'(\.[0-9]+)+$')
132132

133133
def __init__(self, build_dir: Path):
134-
self.tgt_map = {} # type: T.Dict[str, T.Union['ConverterTarget', 'ConverterCustomTarget']]
134+
self.tgt_map: T.Dict[str, T.Union['ConverterTarget', 'ConverterCustomTarget']] = {}
135135
self.build_dir = build_dir
136136

137137
def add(self, tgt: T.Union['ConverterTarget', 'ConverterCustomTarget']) -> None:
@@ -220,38 +220,38 @@ def __init__(self, target: CMakeTarget, env: 'Environment', for_machine: Machine
220220
self.full_name = target.full_name
221221
self.type = target.type
222222
self.install = target.install
223-
self.install_dir = None # type: T.Optional[Path]
223+
self.install_dir: T.Optional[Path] = None
224224
self.link_libraries = target.link_libraries
225225
self.link_flags = target.link_flags + target.link_lang_flags
226-
self.depends_raw = [] # type: T.List[str]
227-
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
226+
self.depends_raw: T.List[str] = []
227+
self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
228228

229229
if target.install_paths:
230230
self.install_dir = target.install_paths[0]
231231

232-
self.languages = set() # type: T.Set[str]
233-
self.sources = [] # type: T.List[Path]
234-
self.generated = [] # type: T.List[Path]
235-
self.generated_ctgt = [] # type: T.List[CustomTargetReference]
236-
self.includes = [] # type: T.List[Path]
237-
self.sys_includes = [] # type: T.List[Path]
238-
self.link_with = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
239-
self.object_libs = [] # type: T.List[ConverterTarget]
240-
self.compile_opts = {} # type: T.Dict[str, T.List[str]]
241-
self.public_compile_opts = [] # type: T.List[str]
232+
self.languages: T.Set[str] = set()
233+
self.sources: T.List[Path] = []
234+
self.generated: T.List[Path] = []
235+
self.generated_ctgt: T.List[CustomTargetReference] = []
236+
self.includes: T.List[Path] = []
237+
self.sys_includes: T.List[Path] = []
238+
self.link_with: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
239+
self.object_libs: T.List[ConverterTarget] = []
240+
self.compile_opts: T.Dict[str, T.List[str]] = {}
241+
self.public_compile_opts: T.List[str] = []
242242
self.pie = False
243243

244244
# Project default override options (c_std, cpp_std, etc.)
245-
self.override_options = [] # type: T.List[str]
245+
self.override_options: T.List[str] = []
246246

247247
# Convert the target name to a valid meson target name
248248
self.name = _sanitize_cmake_name(self.name)
249249

250-
self.generated_raw = [] # type: T.List[Path]
250+
self.generated_raw: T.List[Path] = []
251251

252252
for i in target.files:
253-
languages = set() # type: T.Set[str]
254-
src_suffixes = set() # type: T.Set[str]
253+
languages: T.Set[str] = set()
254+
src_suffixes: T.Set[str] = set()
255255

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

518518
@lru_cache(maxsize=None)
519519
def _all_source_suffixes(self) -> 'ImmutableListProtocol[str]':
520-
suffixes = [] # type: T.List[str]
520+
suffixes: T.List[str] = []
521521
for exts in lang_suffixes.values():
522522
suffixes.extend(exts)
523523
return suffixes
@@ -612,12 +612,12 @@ def __init__(self, target: CMakeGeneratorTarget, env: 'Environment', for_machine
612612
self.cmake_name = str(self.name)
613613
self.original_outputs = list(target.outputs)
614614
self.outputs = [x.name for x in self.original_outputs]
615-
self.conflict_map = {} # type: T.Dict[str, str]
616-
self.command = [] # type: T.List[T.List[T.Union[str, ConverterTarget]]]
615+
self.conflict_map: T.Dict[str, str] = {}
616+
self.command: T.List[T.List[T.Union[str, ConverterTarget]]] = []
617617
self.working_dir = target.working_dir
618618
self.depends_raw = target.depends
619-
self.inputs = [] # type: T.List[T.Union[str, CustomTargetReference]]
620-
self.depends = [] # type: T.List[T.Union[ConverterTarget, ConverterCustomTarget]]
619+
self.inputs: T.List[T.Union[str, CustomTargetReference]] = []
620+
self.depends: T.List[T.Union[ConverterTarget, ConverterCustomTarget]] = []
621621
self.current_bin_dir = target.current_bin_dir
622622
self.current_src_dir = target.current_src_dir
623623
self.env = env
@@ -652,7 +652,7 @@ def ensure_absolute(x: Path) -> Path:
652652
# Ensure that there is no duplicate output in the project so
653653
# that meson can handle cases where the same filename is
654654
# generated in multiple directories
655-
temp_outputs = [] # type: T.List[str]
655+
temp_outputs: T.List[str] = []
656656
for i in self.outputs:
657657
if i in all_outputs:
658658
old = str(i)
@@ -664,11 +664,11 @@ def ensure_absolute(x: Path) -> Path:
664664
self.outputs = temp_outputs
665665

666666
# Check if the command is a build target
667-
commands = [] # type: T.List[T.List[T.Union[str, ConverterTarget]]]
667+
commands: T.List[T.List[T.Union[str, ConverterTarget]]] = []
668668
for curr_cmd in self._raw_target.command:
669669
assert isinstance(curr_cmd, list)
670670
assert curr_cmd[0] != '', "An empty string is not a valid executable"
671-
cmd = [] # type: T.List[T.Union[str, ConverterTarget]]
671+
cmd: T.List[T.Union[str, ConverterTarget]] = []
672672

673673
for j in curr_cmd:
674674
if not j:
@@ -775,25 +775,25 @@ def __init__(self, build: 'Build', subdir: Path, src_dir: Path, install_prefix:
775775
self.env = env
776776
self.for_machine = MachineChoice.HOST # TODO make parameter
777777
self.backend_name = backend.name
778-
self.linkers = set() # type: T.Set[str]
778+
self.linkers: T.Set[str] = set()
779779
self.fileapi = CMakeFileAPI(self.build_dir)
780780

781781
# Raw CMake results
782-
self.bs_files = [] # type: T.List[Path]
783-
self.codemodel_configs = None # type: T.Optional[T.List[CMakeConfiguration]]
784-
self.cmake_stderr = None # type: T.Optional[str]
782+
self.bs_files: T.List[Path] = []
783+
self.codemodel_configs: T.Optional[T.List[CMakeConfiguration]] = None
784+
self.cmake_stderr: T.Optional[str] = None
785785

786786
# Analysed data
787787
self.project_name = ''
788-
self.languages = [] # type: T.List[str]
789-
self.targets = [] # type: T.List[ConverterTarget]
790-
self.custom_targets = [] # type: T.List[ConverterCustomTarget]
788+
self.languages: T.List[str] = []
789+
self.targets: T.List[ConverterTarget] = []
790+
self.custom_targets: T.List[ConverterCustomTarget] = []
791791
self.trace: CMakeTraceParser
792792
self.output_target_map = OutputTargetMap(self.build_dir)
793793

794794
# Generated meson data
795-
self.generated_targets = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]]
796-
self.internal_name_map = {} # type: T.Dict[str, str]
795+
self.generated_targets: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
796+
self.internal_name_map: T.Dict[str, str] = {}
797797

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

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

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

10281028
# Add the targets
1029-
processing = [] # type: T.List[str]
1030-
processed = {} # type: T.Dict[str, T.Dict[str, T.Optional[str]]]
1031-
name_map = {} # type: T.Dict[str, str]
1029+
processing: T.List[str] = []
1030+
processed: T.Dict[str, T.Dict[str, T.Optional[str]]] = {}
1031+
name_map: T.Dict[str, str] = {}
10321032

10331033
def extract_tgt(tgt: T.Union[ConverterTarget, ConverterCustomTarget, CustomTargetReference]) -> IdNode:
10341034
tgt_name = None
@@ -1056,13 +1056,13 @@ def process_target(tgt: ConverterTarget) -> None:
10561056
detect_cycle(tgt)
10571057

10581058
# First handle inter target dependencies
1059-
link_with = [] # type: T.List[IdNode]
1060-
objec_libs = [] # type: T.List[IdNode]
1061-
sources = [] # type: T.List[Path]
1062-
generated = [] # type: T.List[T.Union[IdNode, IndexNode]]
1063-
generated_filenames = [] # type: T.List[str]
1064-
custom_targets = [] # type: T.List[ConverterCustomTarget]
1065-
dependencies = [] # type: T.List[IdNode]
1059+
link_with: T.List[IdNode] = []
1060+
objec_libs: T.List[IdNode] = []
1061+
sources: T.List[Path] = []
1062+
generated: T.List[T.Union[IdNode, IndexNode]] = []
1063+
generated_filenames: T.List[str] = []
1064+
custom_targets: T.List[ConverterCustomTarget] = []
1065+
dependencies: T.List[IdNode] = []
10661066
for i in tgt.link_with:
10671067
assert isinstance(i, ConverterTarget)
10681068
if i.name not in processed:
@@ -1123,15 +1123,15 @@ def process_target(tgt: ConverterTarget) -> None:
11231123
install_tgt = options.get_install(tgt.cmake_name, tgt.install)
11241124

11251125
# Generate target kwargs
1126-
tgt_kwargs = {
1126+
tgt_kwargs: TYPE_mixed_kwargs = {
11271127
'build_by_default': install_tgt,
11281128
'link_args': options.get_link_args(tgt.cmake_name, tgt.link_flags + tgt.link_libraries),
11291129
'link_with': link_with,
11301130
'include_directories': id_node(inc_var),
11311131
'install': install_tgt,
11321132
'override_options': options.get_override_options(tgt.cmake_name, tgt.override_options),
11331133
'objects': [method(x, 'extract_all_objects') for x in objec_libs],
1134-
} # type: TYPE_mixed_kwargs
1134+
}
11351135

11361136
# Only set if installed and only override if it is set
11371137
if install_tgt and tgt.install_dir:
@@ -1148,12 +1148,12 @@ def process_target(tgt: ConverterTarget) -> None:
11481148
tgt_kwargs['pic'] = tgt.pie
11491149

11501150
# declare_dependency kwargs
1151-
dep_kwargs = {
1151+
dep_kwargs: TYPE_mixed_kwargs = {
11521152
'link_args': tgt.link_flags + tgt.link_libraries,
11531153
'link_with': id_node(tgt_var),
11541154
'compile_args': tgt.public_compile_opts,
11551155
'include_directories': id_node(inc_var),
1156-
} # type: TYPE_mixed_kwargs
1156+
}
11571157

11581158
if dependencies:
11591159
generated += dependencies
@@ -1214,7 +1214,7 @@ def resolve_source(x: T.Union[str, ConverterTarget, ConverterCustomTarget, Custo
12141214
return x
12151215

12161216
# Generate the command list
1217-
command = [] # type: T.List[T.Union[str, IdNode, IndexNode]]
1217+
command: T.List[T.Union[str, IdNode, IndexNode]] = []
12181218
command += mesonlib.get_meson_command()
12191219
command += ['--internal', 'cmake_run_ctgt']
12201220
command += ['-o', '@OUTPUT@']
@@ -1226,12 +1226,12 @@ def resolve_source(x: T.Union[str, ConverterTarget, ConverterCustomTarget, Custo
12261226
for cmd in tgt.command:
12271227
command += [resolve_source(x) for x in cmd] + [';;;']
12281228

1229-
tgt_kwargs = {
1229+
tgt_kwargs: TYPE_mixed_kwargs = {
12301230
'input': [resolve_source(x) for x in tgt.inputs],
12311231
'output': tgt.outputs,
12321232
'command': command,
12331233
'depends': [resolve_source(x) for x in tgt.depends],
1234-
} # type: TYPE_mixed_kwargs
1234+
}
12351235

12361236
root_cb.lines += [assign(tgt_var, function('custom_target', [tgt.name], tgt_kwargs))]
12371237
processed[tgt.name] = {'inc': None, 'src': None, 'dep': None, 'tgt': tgt_var, 'func': 'custom_target'}

0 commit comments

Comments
 (0)