From 9d81a23964b138165c8b4a15bd2c5fdaa03d27f5 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Tue, 26 Nov 2024 16:20:31 +0100 Subject: [PATCH] project: Enable SIM ruff rules and fixes WIP Signed-off-by: Pieter De Gendt --- pyproject.toml | 9 ++++++--- src/west/app/main.py | 11 ++++------- src/west/app/project.py | 5 ++--- src/west/configuration.py | 7 +++---- src/west/manifest.py | 7 +++---- tests/test_manifest.py | 5 ++--- 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 10f1dddf..4a46ab9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/src/west/app/main.py b/src/west/app/main.py index bd4b6090..a8d86804 100755 --- a/src/west/app/main.py +++ b/src/west/app/main.py @@ -12,6 +12,7 @@ ''' import argparse +import contextlib import logging import os import platform @@ -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. @@ -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) @@ -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: diff --git a/src/west/app/project.py b/src/west/app/project.py index d5f4c705..40cce75d 100644 --- a/src/west/app/project.py +++ b/src/west/app/project.py @@ -6,6 +6,7 @@ '''West project commands''' import argparse +import contextlib import logging import os import shlex @@ -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: diff --git a/src/west/configuration.py b/src/west/configuration.py index 426c9b1c..540e8855 100644 --- a/src/west/configuration.py +++ b/src/west/configuration.py @@ -36,6 +36,7 @@ ''' import configparser +import contextlib import os import platform import warnings @@ -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) @@ -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 diff --git a/src/west/manifest.py b/src/west/manifest.py index da557d35..da19e1a0 100644 --- a/src/west/manifest.py +++ b/src/west/manifest.py @@ -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], @@ -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: diff --git a/tests/test_manifest.py b/tests/test_manifest.py index 274820a6..1fb0913e 100644 --- a/tests/test_manifest.py +++ b/tests/test_manifest.py @@ -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 @@ -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,