Skip to content

Commit

Permalink
pylint: enable the set_membership plugin
Browse files Browse the repository at this point in the history
Which adds the `use-set-for-membership` check. It's generally faster in
python to use a set with the `in` keyword, because it's a hash check
instead of a linear walk, this is especially true with strings, where
it's actually O(n^2), one loop over the container, and an inner loop of
the strings (as string comparison works by checking that `a[n] == b[n]`,
in a loop).

Also, I'm tired of complaining about this in reviews, let the tools do
it for me :)
  • Loading branch information
dcbaker authored and eli-schwartz committed Nov 30, 2022
1 parent 50f3503 commit 2d349ea
Show file tree
Hide file tree
Showing 34 changed files with 63 additions and 63 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
jobs=0
load-plugins=
pylint.extensions.bad_builtin,
pylint.extensions.set_membership,

[REPORTS]
score=no
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/ast/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def traverse_nodes(inqueue: T.List[BaseNode]) -> T.List[BaseNode]:
extraf_nodes = traverse_nodes(extra_queue)

# Make sure nothing can crash when creating the build class
kwargs_reduced = {k: v for k, v in kwargs.items() if k in targetclass.known_kwargs and k in ['install', 'build_by_default', 'build_always']}
kwargs_reduced = {k: v for k, v in kwargs.items() if k in targetclass.known_kwargs and k in {'install', 'build_by_default', 'build_always'}}
kwargs_reduced = {k: v.value if isinstance(v, ElementaryNode) else v for k, v in kwargs_reduced.items()}
kwargs_reduced = {k: v for k, v in kwargs_reduced.items() if not isinstance(v, BaseNode)}
for_machine = MachineChoice.HOST
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,7 @@ def _scan_fortran_file_deps(src: Path, srcdir: Path, dirname: Path, tdeps, compi
submodmatch = submodre.match(line)
if submodmatch is not None:
parents = submodmatch.group(1).lower().split(':')
assert len(parents) in (1, 2), (
assert len(parents) in {1, 2}, (
'submodule ancestry must be specified as'
f' ancestor:parent but Meson found {parents}')

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,13 @@ def split_link_args(args):

def _get_cl_compiler(self, target):
for lang, c in target.compilers.items():
if lang in ('c', 'cpp'):
if lang in {'c', 'cpp'}:
return c
# No source files, only objects, but we still need a compiler, so
# return a found compiler
if len(target.objects) > 0:
for lang, c in self.environment.coredata.compilers[target.for_machine].items():
if lang in ('c', 'cpp'):
if lang in {'c', 'cpp'}:
return c
raise MesonException('Could not find a C or C++ compiler. MSVC can only build C/C++ projects.')

Expand Down
8 changes: 4 additions & 4 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1829,8 +1829,8 @@ def post_init(self) -> None:
self.suffix = 'abs'
elif ('c' in self.compilers and self.compilers['c'].get_id().startswith('xc16')):
self.suffix = 'elf'
elif ('c' in self.compilers and self.compilers['c'].get_id() in ('ti', 'c2000') or
'cpp' in self.compilers and self.compilers['cpp'].get_id() in ('ti', 'c2000')):
elif ('c' in self.compilers and self.compilers['c'].get_id() in {'ti', 'c2000'} or
'cpp' in self.compilers and self.compilers['cpp'].get_id() in {'ti', 'c2000'}):
self.suffix = 'out'
else:
self.suffix = machine.get_exe_suffix()
Expand Down Expand Up @@ -2176,10 +2176,10 @@ def _validate_darwin_versions(darwin_versions):
raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z where '
'X, Y, Z are numbers, and Y and Z are optional')
parts = v.split('.')
if len(parts) in (1, 2, 3) and int(parts[0]) > 65535:
if len(parts) in {1, 2, 3} and int(parts[0]) > 65535:
raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z '
'where X is [0, 65535] and Y, Z are optional')
if len(parts) in (2, 3) and int(parts[1]) > 255:
if len(parts) in {2, 3} and int(parts[1]) > 255:
raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z '
'where Y is [0, 255] and Y, Z are optional')
if len(parts) == 3 and int(parts[2]) > 255:
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/cmake/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def _flags_to_list(raw: str) -> T.List[str]:
escape = False
elif i == '\\':
escape = True
elif i in ['"', "'"]:
elif i in {'"', "'"}:
in_string = not in_string
elif i in [' ', '\n']:
elif i in {' ', '\n'}:
if in_string:
curr += i
else:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def target_file(arg: str) -> str:

supported = {
# Boolean functions
'BOOL': lambda x: '0' if x.upper() in ['0', 'FALSE', 'OFF', 'N', 'NO', 'IGNORE', 'NOTFOUND'] or x.endswith('-NOTFOUND') else '1',
'BOOL': lambda x: '0' if x.upper() in {'0', 'FALSE', 'OFF', 'N', 'NO', 'IGNORE', 'NOTFOUND'} or x.endswith('-NOTFOUND') else '1',
'AND': lambda x: '1' if all(y == '1' for y in x.split(',')) else '0',
'OR': lambda x: '1' if any(y == '1' for y in x.split(',')) else '0',
'NOT': lambda x: '0' if x == '1' else '1',
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: Path, su
)
continue
self.override_options += [f'{i}_std={std}']
elif j in ['-fPIC', '-fpic', '-fPIE', '-fpie']:
elif j in {'-fPIC', '-fpic', '-fPIE', '-fpie'}:
self.pie = True
elif isinstance(ctgt, ConverterCustomTarget):
# Sometimes projects pass generated source files as compiler
Expand Down Expand Up @@ -1173,10 +1173,10 @@ def process_target(tgt: ConverterTarget) -> None:
src_node = assign(src_var, function('files', sources))
tgt_node = assign(tgt_var, function(tgt_func, [tgt_var, id_node(src_var), *generated], tgt_kwargs))
node_list += [src_node, tgt_node]
if tgt_func in ['static_library', 'shared_library']:
if tgt_func in {'static_library', 'shared_library'}:
dep_node = assign(dep_var, function('declare_dependency', kwargs=dep_kwargs))
node_list += [dep_node]
elif tgt_func in ['shared_module']:
elif tgt_func == 'shared_module':
del dep_kwargs['link_with']
dep_node = assign(dep_var, function('declare_dependency', kwargs=dep_kwargs))
node_list += [dep_node]
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/cmake/traceparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,14 @@ def _parse_common_target_options(self, func: str, private_prop: str, interface_p
if i in ignore:
continue

if i in ['INTERFACE', 'LINK_INTERFACE_LIBRARIES', 'PUBLIC', 'PRIVATE', 'LINK_PUBLIC', 'LINK_PRIVATE']:
if i in {'INTERFACE', 'LINK_INTERFACE_LIBRARIES', 'PUBLIC', 'PRIVATE', 'LINK_PUBLIC', 'LINK_PRIVATE'}:
mode = i
continue

if mode in ['INTERFACE', 'LINK_INTERFACE_LIBRARIES', 'PUBLIC', 'LINK_PUBLIC']:
if mode in {'INTERFACE', 'LINK_INTERFACE_LIBRARIES', 'PUBLIC', 'LINK_PUBLIC'}:
interface += i.split(';')

if mode in ['PUBLIC', 'PRIVATE', 'LINK_PRIVATE']:
if mode in {'PUBLIC', 'PRIVATE', 'LINK_PRIVATE'}:
private += i.split(';')

if paths:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ def get_crt_link_args(self, crt_val: str, buildtype: str) -> T.List[str]:
# native option to override it; override it with /NODEFAULTLIB
host_link_arg_overrides = []
host_crt_compile_args = self.host_compiler.get_crt_compile_args(crt_val, buildtype)
if any(arg in ['/MDd', '/MD', '/MTd'] for arg in host_crt_compile_args):
if any(arg in {'/MDd', '/MD', '/MTd'} for arg in host_crt_compile_args):
host_link_arg_overrides += ['/NODEFAULTLIB:LIBCMT.lib']
return self._to_host_flags(host_link_arg_overrides + self.host_compiler.get_crt_link_args(crt_val, buildtype), _Phase.LINKER)

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/d.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def _get_crt_args(self, crt_val: str, buildtype: str) -> T.List[str]:

if crt_val in self.mscrt_args:
return self.mscrt_args[crt_val]
assert crt_val in ['from_buildtype', 'static_from_buildtype']
assert crt_val in {'from_buildtype', 'static_from_buildtype'}

dbg = 'mdd'
rel = 'md'
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
def module_name_to_filename(self, module_name: str) -> str:
if '_' in module_name: # submodule
s = module_name.lower()
if self.id in ('gcc', 'intel', 'intel-cl'):
if self.id in {'gcc', 'intel', 'intel-cl'}:
filename = s.replace('_', '@') + '.smod'
elif self.id in ('pgi', 'flang'):
elif self.id in {'pgi', 'flang'}:
filename = s.replace('_', '-') + '.mod'
else:
filename = s + '.mod'
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_buildtype_args(self, buildtype: str) -> T.List[str]:
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
build_dir: str) -> T.List[str]:
for idx, i in enumerate(parameter_list):
if i in ['-cp', '-classpath', '-sourcepath'] and idx + 1 < len(parameter_list):
if i in {'-cp', '-classpath', '-sourcepath'} and idx + 1 < len(parameter_list):
path_list = parameter_list[idx + 1].split(os.pathsep)
path_list = [os.path.normpath(os.path.join(build_dir, x)) for x in path_list]
parameter_list[idx + 1] = os.pathsep.join(path_list)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ def has_func_attribute(self, name: str, env: 'Environment') -> T.Tuple[bool, boo
# don't work
m = env.machines[self.for_machine]
if not (m.is_windows() or m.is_cygwin()):
if name in ['dllimport', 'dllexport']:
if name in {'dllimport', 'dllexport'}:
return False, False

return self.compiles(self.attribute_check_func(name), env,
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/compilers/mixins/visualstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
for i in args:
# -mms-bitfields is specific to MinGW-GCC
# -pthread is only valid for GCC
if i in ('-mms-bitfields', '-pthread'):
if i in {'-mms-bitfields', '-pthread'}:
continue
if i.startswith('-LIBPATH:'):
i = '/LIBPATH:' + i[9:]
Expand Down Expand Up @@ -361,7 +361,7 @@ def get_default_include_dirs(self) -> T.List[str]:
def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
if crt_val in self.crt_args:
return self.crt_args[crt_val]
assert crt_val in ['from_buildtype', 'static_from_buildtype']
assert crt_val in {'from_buildtype', 'static_from_buildtype'}
dbg = 'mdd'
rel = 'md'
if crt_val == 'static_from_buildtype':
Expand All @@ -385,7 +385,7 @@ def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
def has_func_attribute(self, name: str, env: 'Environment') -> T.Tuple[bool, bool]:
# MSVC doesn't have __attribute__ like Clang and GCC do, so just return
# false without compiling anything
return name in ['dllimport', 'dllexport'], False
return name in {'dllimport', 'dllexport'}, False

def get_argument_syntax(self) -> str:
return 'msvc'
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def __deepcopy__(self, memo: T.Dict[int, 'InternalDependency']) -> 'InternalDepe
assert isinstance(result, InternalDependency)
memo[id(self)] = result
for k, v in self.__dict__.items():
if k in ['libraries', 'whole_libraries']:
if k in {'libraries', 'whole_libraries'}:
setattr(result, k, copy.copy(v))
else:
setattr(result, k, copy.deepcopy(v, memo))
Expand Down
14 changes: 7 additions & 7 deletions mesonbuild/dependencies/boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def __init__(self, path: Path):
self.version_lib = '{}_{}'.format(self.vers_raw[0], self.vers_raw[1])

# Detecting library type
if self.nvsuffix in ['so', 'dll', 'dll.a', 'dll.lib', 'dylib']:
if self.nvsuffix in {'so', 'dll', 'dll.a', 'dll.lib', 'dylib'}:
self.static = False
elif self.nvsuffix in ['a', 'lib']:
elif self.nvsuffix in {'a', 'lib'}:
self.static = True
else:
raise UnknownFileException(self.path)
Expand All @@ -177,7 +177,7 @@ def __init__(self, path: Path):
for i in tags:
if i == 'mt':
self.mt = True
elif len(i) == 3 and i[1:] in ['32', '64']:
elif len(i) == 3 and i[1:] in {'32', '64'}:
self.arch = i
elif BoostLibraryFile.reg_abi_tag.match(i):
self.runtime_static = 's' in i
Expand Down Expand Up @@ -316,13 +316,13 @@ def vscrt_matches(self, vscrt: str) -> bool:
# If no vscrt tag present, assume that it fits ['/MD', '/MDd', '/MT', '/MTd']
if not vscrt:
return True
if vscrt in ['/MD', '-MD']:
if vscrt in {'/MD', '-MD'}:
return not self.runtime_static and not self.runtime_debug
elif vscrt in ['/MDd', '-MDd']:
elif vscrt in {'/MDd', '-MDd'}:
return not self.runtime_static and self.runtime_debug
elif vscrt in ['/MT', '-MT']:
elif vscrt in {'/MT', '-MT'}:
return (self.runtime_static or not self.static) and not self.runtime_debug
elif vscrt in ['/MTd', '-MTd']:
elif vscrt in {'/MTd', '-MTd'}:
return (self.runtime_static or not self.static) and self.runtime_debug

mlog.warning(f'Boost: unknown vscrt tag {vscrt}. This may cause the compilation to fail. Please consider reporting this as a bug.', once=True)
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/dependencies/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def get_dep_identifier(name: str, kwargs: T.Dict[str, T.Any]) -> 'TV_DepID':
# 'default_options' is only used in fallback case
# 'not_found_message' has no impact on the dependency lookup
# 'include_type' is handled after the dependency lookup
if key in ('version', 'native', 'required', 'fallback', 'allow_fallback', 'default_options',
'not_found_message', 'include_type'):
if key in {'version', 'native', 'required', 'fallback', 'allow_fallback', 'default_options',
'not_found_message', 'include_type'}:
continue
# All keyword arguments are strings, ints, or lists (or lists of lists)
if isinstance(value, list):
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def get_windows_python_arch() -> T.Optional[str]:
return None
elif pyplat == 'win32':
return '32'
elif pyplat in ('win64', 'win-amd64'):
elif pyplat in {'win64', 'win-amd64'}:
return '64'
mlog.log(f'Unknown Windows Python platform {pyplat!r}')
return None
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _filter_link_args(self, args: T.Sequence[str]) -> T.List[str]:
for f in args:
if self._is_link_arg(f):
result.append(f)
if f in ('-L', '-Xlinker'):
if f in {'-L', '-Xlinker'}:
include_next = True
elif include_next:
include_next = False
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/depfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def parse(lines: T.Iterable[str]) -> T.List[T.Tuple[T.List[str], T.List[str]]]:
if c in {'\\', '$'}:
escape = c
continue
elif c in (' ', '\n'):
elif c in {' ', '\n'}:
if out != '':
if in_deps:
deps.append(out)
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def detect_cpu_family(compilers: CompilersDict) -> str:
trial = 'ppc64'
elif trial.startswith(('powerpc', 'ppc')) or trial in {'macppc', 'power macintosh'}:
trial = 'ppc'
elif trial in ('amd64', 'x64', 'i86pc'):
elif trial in {'amd64', 'x64', 'i86pc'}:
trial = 'x86_64'
elif trial in {'sun4u', 'sun4v'}:
trial = 'sparc64'
Expand Down Expand Up @@ -353,7 +353,7 @@ def detect_cpu(compilers: CompilersDict) -> str:
else:
trial = platform.machine().lower()

if trial in ('amd64', 'x64', 'i86pc'):
if trial in {'amd64', 'x64', 'i86pc'}:
trial = 'x86_64'
if trial == 'x86_64':
# Same check as above for cpu_family
Expand Down
5 changes: 2 additions & 3 deletions mesonbuild/mesonmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def run_help_command(self, options):

def run(self, args):
implicit_setup_command_notice = False
pending_python_deprecation_notice = False
# If first arg is not a known command, assume user wants to run the setup
# command.
known_commands = list(self.commands.keys()) + ['-h', '--help']
Expand All @@ -187,8 +186,8 @@ def run(self, args):
# Bump the version here in order to add a pre-exit warning that we are phasing out
# support for old python. If this is already the oldest supported version, then
# this can never be true and does nothing.
if command in ('setup', 'compile', 'test', 'install') and sys.version_info < (3, 7):
pending_python_deprecation_notice = True
pending_python_deprecation_notice = \
command in {'setup', 'compile', 'test', 'install'} and sys.version_info < (3, 7)

try:
return options.run_func(options)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/minit.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def autodetect_options(options: 'argparse.Namespace', sample: bool = False) -> N
if f.suffix == '.c':
options.language = 'c'
break
if f.suffix in ('.cc', '.cpp'):
if f.suffix in {'.cc', '.cpp'}:
options.language = 'cpp'
break
if f.suffix == '.cs':
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def is_module_library(fname):
if hasattr(fname, 'fname'):
fname = fname.fname
suffix = fname.split('.')[-1]
return suffix in ('gir', 'typelib')
return suffix in {'gir', 'typelib'}


class ModuleReturnValue:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/gnome.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def _get_girtargets_langs_compilers(girtargets: T.Sequence[build.BuildTarget]) -
for girtarget in girtargets:
for lang, compiler in girtarget.compilers.items():
# XXX: Can you use g-i with any other language?
if lang in ('c', 'cpp', 'objc', 'objcpp', 'd'):
if lang in {'c', 'cpp', 'objc', 'objcpp', 'd'}:
ret.append((lang, compiler))
break

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/modules/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _get_windows_python_arch(self) -> T.Optional[str]:
return None
elif self.platform == 'win32':
return '32'
elif self.platform in ('win64', 'win-amd64'):
elif self.platform in {'win64', 'win-amd64'}:
return '64'
mlog.log(f'Unknown Windows Python platform {self.platform!r}')
return None
Expand Down Expand Up @@ -727,7 +727,7 @@ def _find_installation_impl(self, state: 'ModuleState', display_name: str, name_
# on various platforms, let's not give up just yet, if an executable
# named python is available and has a compatible version, let's use
# it
if not python.found() and name_or_path in ['python2', 'python3']:
if not python.found() and name_or_path in {'python2', 'python3'}:
python = PythonExternalProgram('python')

if python.found():
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/munstable_coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ def run(options):
coredata = cdata.load(options.builddir)
backend = coredata.get_option(OptionKey('backend'))
for k, v in sorted(coredata.__dict__.items()):
if k in ('backend_options', 'base_options', 'builtins', 'compiler_options', 'user_options'):
if k in {'backend_options', 'base_options', 'builtins', 'compiler_options', 'user_options'}:
# use `meson configure` to view these
pass
elif k in ['install_guid', 'test_guid', 'regen_guid']:
elif k in {'install_guid', 'test_guid', 'regen_guid'}:
if all_backends or backend.startswith('vs'):
print(k + ': ' + v)
elif k == 'target_guids':
if all_backends or backend.startswith('vs'):
print(k + ':')
dump_guids(v)
elif k in ['lang_guids']:
elif k == 'lang_guids':
if all_backends or backend.startswith('vs') or backend == 'xcode':
print(k + ':')
dump_guids(v)
Expand Down
Loading

0 comments on commit 2d349ea

Please sign in to comment.