Skip to content

Commit

Permalink
project: Enable SIM ruff rules and fixes
Browse files Browse the repository at this point in the history
WIP

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
  • Loading branch information
pdgendt committed Nov 26, 2024
1 parent 2b18ae4 commit 9d81a23
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ namespaces = false
line-length = 100

[tool.ruff.lint]
extend-select = [
"I", # isort
select = [
"B", # flake8-bugbear
"E", # pycodestyle errors
"F", # Pyflakes
"I", # isort
"SIM", # flake8-simplify
"UP", # pyupgrade
"W", # pycodestyle warnings
]
ignore = [
"UP027", # deprecated pyupgrade rule
"SIM108", # Allow if-else blocks instead of forcing ternary operator
"SIM300", # Allow Yoda conditions
"UP027", # deprecated pyupgrade rule
]

[tool.ruff.format]
Expand Down
11 changes: 4 additions & 7 deletions src/west/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'''

import argparse
import contextlib
import logging
import os
import platform
Expand Down Expand Up @@ -253,10 +254,8 @@ def run(self, argv):
# See if we're in a workspace. It's fine if we're not.
# Note that this falls back on searching from ZEPHYR_BASE
# if the current directory isn't inside a west workspace.
try:
with contextlib.suppress(WestNotFound):
self.topdir = west_topdir()
except WestNotFound:
pass

# Read the configuration files. We need this to get
# manifest.path to parse the manifest, etc.
Expand Down Expand Up @@ -785,11 +784,9 @@ def set_zephyr_base(self, args):
projects = manifest.get_projects(['zephyr'],
allow_paths=False)
except ValueError:
try:
with contextlib.suppress(ValueError):
projects = manifest.get_projects([Path(topdir) /
'zephyr'])
except ValueError:
pass
if projects:
zephyr = projects[0]
config.set('zephyr.base', zephyr.path)
Expand Down Expand Up @@ -1104,7 +1101,7 @@ def add_argument(self, *args, **kwargs):
# Track information we want for formatting help. The argparse
# module calls kwargs.pop(), so can't call super first without
# losing data.
optional = {'options': [], 'metavar': kwargs.get('metavar', None)}
optional = {'options': [], 'metavar': kwargs.get('metavar')}
need_metavar = (optional['metavar'] is None and
kwargs.get('action') in (None, 'store'))
for arg in args:
Expand Down
5 changes: 2 additions & 3 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'''West project commands'''

import argparse
import contextlib
import logging
import os
import shlex
Expand Down Expand Up @@ -128,10 +129,8 @@ def has_output():
# are no notable changes, so any output at all
# means we should run 'git status' on the project.
stdout, stderr = None, None
try:
with contextlib.suppress(subprocess.TimeoutExpired):
stdout, stderr = popen.communicate(timeout=0.1)
except subprocess.TimeoutExpired:
pass
return stdout or stderr

while True:
Expand Down
7 changes: 3 additions & 4 deletions src/west/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'''

import configparser
import contextlib
import os
import platform
import warnings
Expand Down Expand Up @@ -601,7 +602,7 @@ def _location(cfg: ConfigFile, topdir: Optional[PathType] = None,
#
# See https://github.com/zephyrproject-rtos/west/issues/300
# for details.
pd = PureWindowsPath(os.environ['ProgramData'])
pd = PureWindowsPath(os.environ['ProgramData']) # noqa: SIM112
return os.fspath(pd / 'west' / 'config')

raise ValueError('unsupported platform ' + plat)
Expand Down Expand Up @@ -638,10 +639,8 @@ def _gather_configs(cfg: ConfigFile, topdir: Optional[PathType]) -> list[str]:
if cfg == ConfigFile.ALL or cfg == ConfigFile.GLOBAL:
ret.append(_location(ConfigFile.GLOBAL, topdir=topdir))
if cfg == ConfigFile.ALL or cfg == ConfigFile.LOCAL:
try:
with contextlib.suppress(WestNotFound):
ret.append(_location(ConfigFile.LOCAL, topdir=topdir))
except WestNotFound:
pass

return ret

Expand Down
7 changes: 3 additions & 4 deletions src/west/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2268,11 +2268,10 @@ def _load_defaults(self, defaults: dict[str, Any],
# md = manifest defaults (dictionary with values parsed from
# the manifest)
mdrem: Optional[str] = defaults.get('remote')
if mdrem:
if mdrem and mdrem not in url_bases:
# The default remote name, if provided, must refer to a
# well-defined remote.
if mdrem not in url_bases:
self._malformed(f'default remote {mdrem} is not defined')
self._malformed(f'default remote {mdrem} is not defined')
return _defaults(mdrem, defaults.get('revision', _DEFAULT_REV))

def _load_projects(self, manifest: dict[str, Any],
Expand Down Expand Up @@ -2364,7 +2363,7 @@ def _load_project(self, pd: dict, url_bases: dict[str, str],
# regardless of self._ctx.import_flags. The 'ignore' type flags
# just mean ignore the imported data. The path-prefix in this
# manifest affects the project no matter what.
imp = pd.get('import', None)
imp = pd.get('import')
if isinstance(imp, dict):
pfx = self._load_imap(imp, f'project {name}').path_prefix
else:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# it's particularly inconvenient to test something without a real git
# repository, go ahead and make one in a temporary directory.

import contextlib
import logging
import os
import platform
Expand Down Expand Up @@ -1354,11 +1355,9 @@ def clean_up_config_files():
for configfile in [ConfigFile.SYSTEM,
ConfigFile.GLOBAL,
ConfigFile.LOCAL]:
try:
with contextlib.suppress(KeyError):
config.delete('manifest.project-filter',
configfile=configfile)
except KeyError:
pass

def check_error(project_filter, expected_err_contains):
for configfile in [ConfigFile.SYSTEM,
Expand Down

0 comments on commit 9d81a23

Please sign in to comment.