From 21e9df4191f26091c69e780d89ba874ccbac07ae Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 25 Feb 2025 11:58:17 +0000 Subject: [PATCH 1/5] Added ruff linting There are quite a few cases of unused imports, and even some errors like undefined name. Ruff can be used to catch these going forward. For now, all failing lints are ignored. These can be ratcheted down in the future. --- .pre-commit-config.yaml | 7 +++++++ pyproject.toml | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2f821a42..09f1ae57 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,3 +34,10 @@ repos: rev: 22.3.0 hooks: - id: black + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.9.7 + hooks: + # Run the linter. + - id: ruff + # The formatter could be run in the future + #- id: ruff-format diff --git a/pyproject.toml b/pyproject.toml index 5c80b634..a40659bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,3 +60,26 @@ envlist = py3 deps = pytest commands = pytest """ + +[tool.ruff] +line-length = 88 +indent-width = 4 + +target-version = "py37" + +[tool.ruff.lint] +ignore = [ + "E711", + "E712", + "E713", + "E714", + "E721", + "E722", + "E741", + "E743", + "F401", + "E402", + "F523", + "F841", + "F821", +] From 40497c1aaa4ee7c11b692bf49f0cc539246c26d2 Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 25 Feb 2025 12:05:56 +0000 Subject: [PATCH 2/5] Removed unused arguments from `.format` call Ruff lint F523 --- pyproject.toml | 1 - tests/test_libraries.py | 17 +++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a40659bd..782c8e6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,7 +79,6 @@ ignore = [ "E743", "F401", "E402", - "F523", "F841", "F821", ] diff --git a/tests/test_libraries.py b/tests/test_libraries.py index 9348fa2c..321562cf 100644 --- a/tests/test_libraries.py +++ b/tests/test_libraries.py @@ -7,6 +7,7 @@ import shutil import subprocess import tempfile +from textwrap import dedent from argparse import Namespace from test_common import cache_root, cores_root, library_root @@ -136,14 +137,14 @@ def test_library_add(caplog): vars(args)["sync-version"] = "capi2" args.location = None - expected = """[library.fusesoc-cores] -location = fusesoc_libraries/fusesoc-cores -sync-uri = https://github.com/fusesoc/fusesoc-cores -sync-version = capi2 -sync-type = git -auto-sync = true""".format( - cm._lm.library_root - ) + expected = dedent(""" + [library.fusesoc-cores] + location = fusesoc_libraries/fusesoc-cores + sync-uri = https://github.com/fusesoc/fusesoc-cores + sync-version = capi2 + sync-type = git + auto-sync = true + """) add_library(cm, args) From f7b7813e9be2afd56f20fc90c0891f771346e704 Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 25 Feb 2025 12:09:21 +0000 Subject: [PATCH 3/5] Defined previously undefined variable `tool` Ruff lint F821 --- fusesoc/fusesoc.py | 1 + pyproject.toml | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/fusesoc/fusesoc.py b/fusesoc/fusesoc.py index 5f163fc1..3bfe4ffd 100644 --- a/fusesoc/fusesoc.py +++ b/fusesoc/fusesoc.py @@ -146,6 +146,7 @@ def get_backend(self, core, flags, backendargs=[]): try: backend_class = get_edatool(flags["tool"]) except ImportError: + tool = flags.setdefault("tool", "") raise RuntimeError(f"Backend {tool!r} not found") edalizer = Edalizer( diff --git a/pyproject.toml b/pyproject.toml index 782c8e6c..7c89e5d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -80,5 +80,4 @@ ignore = [ "F401", "E402", "F841", - "F821", ] From 465fd08670e03d1e285fda696089c498fceeeae8 Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 25 Feb 2025 12:37:54 +0000 Subject: [PATCH 4/5] Fixed unused variable lint errors Ruff lint F841 --- fusesoc/capi2/core.py | 1 - fusesoc/coremanager.py | 4 ++-- fusesoc/edalizer.py | 7 +------ fusesoc/fusesoc.py | 2 +- fusesoc/librarymanager.py | 2 +- pyproject.toml | 1 - tests/test_capi2.py | 6 ++---- tests/test_libraries.py | 4 ---- 8 files changed, 7 insertions(+), 20 deletions(-) diff --git a/fusesoc/capi2/core.py b/fusesoc/capi2/core.py index dac0188a..6ce63d90 100644 --- a/fusesoc/capi2/core.py +++ b/fusesoc/capi2/core.py @@ -380,7 +380,6 @@ def _parse_param(flags, name, core_param): # ...or in any of its dependencies elif p in ext_parameters: parameters[p] = ext_parameters[p] - datatype = parameters[p]["datatype"] else: raise SyntaxError( diff --git a/fusesoc/coremanager.py b/fusesoc/coremanager.py index 6eaee2a8..c4bbba1f 100644 --- a/fusesoc/coremanager.py +++ b/fusesoc/coremanager.py @@ -222,7 +222,7 @@ def eq_vln(this, that): transaction = solver.solve(request) except SatisfiabilityError as e: raise DependencyError(top_core.name, msg=e.unsat.to_string(pool)) - except NoPackageFound as e: + except NoPackageFound: raise DependencyError(top_core.name) virtual_selection = {} @@ -376,7 +376,7 @@ def _detect_capi_version(self, core_file) -> int: core_file ) ) - except Exception as error: + except Exception: error_msg = f"Unable to determine CAPI version from core file {core_file}" logger.warning(error_msg) return -1 diff --git a/fusesoc/edalizer.py b/fusesoc/edalizer.py index 30918c4c..2835ee10 100644 --- a/fusesoc/edalizer.py +++ b/fusesoc/edalizer.py @@ -338,7 +338,7 @@ def _build_parser(self, backend_class, edam): default = [ typedict[param["datatype"]]["type"](param["default"]) ] - except KeyError as e: + except KeyError: pass try: param_groups[_paramtype].add_argument( @@ -622,11 +622,6 @@ def is_generator_cacheable(self): def is_cacheable(self): return self.is_input_cacheable() or self.is_generator_cacheable() - def acquire_cache_lock(self): - have_lock = False - # while not have_lock: - # if - def generate(self): """Run a parametrized generator diff --git a/fusesoc/fusesoc.py b/fusesoc/fusesoc.py index 3bfe4ffd..dbe47486 100644 --- a/fusesoc/fusesoc.py +++ b/fusesoc/fusesoc.py @@ -36,7 +36,7 @@ def _register_libraries(self): for library in self.config.libraries + cores_root_libs: try: self.add_library(library) - except (RuntimeError, OSError) as e: + except (RuntimeError, OSError): try: temporary_lm = LibraryManager(self.config.library_root) # try to initialize library diff --git a/fusesoc/librarymanager.py b/fusesoc/librarymanager.py index f1e638b6..5894d8b1 100644 --- a/fusesoc/librarymanager.py +++ b/fusesoc/librarymanager.py @@ -60,7 +60,7 @@ def l(s): logger.info(l(f"{self.location} does not exist. Trying a checkout")) try: provider.init_library(self) - except RuntimeError as e: + except RuntimeError: # Keep old behavior of logging a warning if there is a library # in `fusesoc.conf`, but the directory does not exist for some # reason and it could not be initialized. diff --git a/pyproject.toml b/pyproject.toml index 7c89e5d2..0a1d2b56 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,5 +79,4 @@ ignore = [ "E743", "F401", "E402", - "F841", ] diff --git a/tests/test_capi2.py b/tests/test_capi2.py index 687b994a..a16ebe43 100644 --- a/tests/test_capi2.py +++ b/tests/test_capi2.py @@ -45,7 +45,7 @@ def test_empty_core(): core_file = os.path.join(tests_dir, "capi2_cores", "misc", "empty.core") with pytest.raises(SyntaxError) as excinfo: - core = Core(Core2Parser(), core_file) + Core(Core2Parser(), core_file) assert "Error validating" in str(excinfo.value) @@ -127,8 +127,6 @@ def test_capi2_export_no_overwrite(): "vpifile", ] - result = [] - # Export and check all dst files are equal to src files core.export(export_root) for f in expected: @@ -282,7 +280,7 @@ def test_capi2_type_check(): core_file = os.path.join(tests_dir, "capi2_cores", "misc", "typecheck.core") with pytest.raises(SyntaxError) as excinfo: - core = Core(Core2Parser(), core_file) + Core(Core2Parser(), core_file) assert "Error validating" in str(excinfo.value) diff --git a/tests/test_libraries.py b/tests/test_libraries.py index 321562cf..bb8fd994 100644 --- a/tests/test_libraries.py +++ b/tests/test_libraries.py @@ -178,8 +178,6 @@ def test_library_update(caplog): conf = Config(tcf.name) - args = Namespace() - Fusesoc.init_logging(False, False) fs = Fusesoc(conf) @@ -237,8 +235,6 @@ def test_library_update_with_initialize(caplog): conf = Config(tcf.name) - args = Namespace() - Fusesoc.init_logging(False, False) fs = Fusesoc(conf) From 44a978d64ce0c5c60c2d697352bf83994b61f1a1 Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 25 Feb 2025 12:46:58 +0000 Subject: [PATCH 5/5] Removed unused imports from files Ruff lint F401 --- doc/source/conf.py | 8 +++----- fusesoc/edalizer.py | 1 - fusesoc/filters/splitlib.py | 1 - fusesoc/fusesoc.py | 3 +-- fusesoc/parser/coreparser.py | 1 - fusesoc/provider/git.py | 2 -- fusesoc/provider/opencores.py | 1 - pyproject.toml | 1 - tests/test_capi2.py | 1 - tests/test_libraries.py | 2 -- tests/test_vlnv.py | 2 -- 11 files changed, 4 insertions(+), 19 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index f5691bbc..45d9bf7f 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -12,10 +12,10 @@ import os import sys from datetime import datetime +from importlib import util as importlib_util import jsonschema2md -import fusesoc from fusesoc.capi2.json_schema import capi2_schema from fusesoc.utils import yaml_read @@ -109,11 +109,9 @@ # or # - apt-get install python-sphinx-rtd-theme -try: - import sphinx_rtd_theme - +if importlib_util.find_spec("sphinx_rtd_theme") is not None: html_theme = "sphinx_rtd_theme" -except ImportError: +else: sys.stderr.write( "Warning: The Sphinx 'sphinx_rtd_theme' HTML theme was " + "not found. Make sure you have the theme installed to produce pretty " diff --git a/fusesoc/edalizer.py b/fusesoc/edalizer.py index 2835ee10..76191fd5 100644 --- a/fusesoc/edalizer.py +++ b/fusesoc/edalizer.py @@ -13,7 +13,6 @@ from fusesoc import utils from fusesoc.capi2.coreparser import Core2Parser -from fusesoc.coremanager import DependencyError from fusesoc.utils import merge_dict from fusesoc.vlnv import Vlnv diff --git a/fusesoc/filters/splitlib.py b/fusesoc/filters/splitlib.py index 2ffb7a11..bf95f9c8 100644 --- a/fusesoc/filters/splitlib.py +++ b/fusesoc/filters/splitlib.py @@ -1,5 +1,4 @@ import logging -import os logger = logging.getLogger(__name__) diff --git a/fusesoc/fusesoc.py b/fusesoc/fusesoc.py index dbe47486..da4d1233 100644 --- a/fusesoc/fusesoc.py +++ b/fusesoc/fusesoc.py @@ -6,11 +6,10 @@ import os from importlib import import_module -from fusesoc.config import Config from fusesoc.coremanager import CoreManager, DependencyError from fusesoc.edalizer import Edalizer from fusesoc.librarymanager import Library, LibraryManager -from fusesoc.utils import Launcher, setup_logging, yaml_fread +from fusesoc.utils import setup_logging, yaml_fread from fusesoc.vlnv import Vlnv try: diff --git a/fusesoc/parser/coreparser.py b/fusesoc/parser/coreparser.py index b8c24cc8..60a47086 100644 --- a/fusesoc/parser/coreparser.py +++ b/fusesoc/parser/coreparser.py @@ -3,7 +3,6 @@ # SPDX-License-Identifier: BSD-2-Clause import fastjsonschema -import yaml from fusesoc import utils diff --git a/fusesoc/provider/git.py b/fusesoc/provider/git.py index bb1af4b1..d88e58db 100644 --- a/fusesoc/provider/git.py +++ b/fusesoc/provider/git.py @@ -3,8 +3,6 @@ # SPDX-License-Identifier: BSD-2-Clause import logging -import os.path -import shutil import subprocess from fusesoc.provider.provider import Provider diff --git a/fusesoc/provider/opencores.py b/fusesoc/provider/opencores.py index 2bd60bd6..92b93f67 100644 --- a/fusesoc/provider/opencores.py +++ b/fusesoc/provider/opencores.py @@ -3,7 +3,6 @@ # SPDX-License-Identifier: BSD-2-Clause import logging -import sys from fusesoc.provider.provider import Provider from fusesoc.utils import Launcher, cygpath, is_mingw diff --git a/pyproject.toml b/pyproject.toml index 0a1d2b56..bca10079 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,6 +77,5 @@ ignore = [ "E722", "E741", "E743", - "F401", "E402", ] diff --git a/tests/test_capi2.py b/tests/test_capi2.py index a16ebe43..47b9b9bc 100644 --- a/tests/test_capi2.py +++ b/tests/test_capi2.py @@ -38,7 +38,6 @@ def test_files_out_of_hierarchy(): def test_empty_core(): import os - import tempfile from fusesoc.capi2.coreparser import Core2Parser from fusesoc.core import Core diff --git a/tests/test_libraries.py b/tests/test_libraries.py index bb8fd994..0adf7b64 100644 --- a/tests/test_libraries.py +++ b/tests/test_libraries.py @@ -14,7 +14,6 @@ from fusesoc.config import Config from fusesoc.fusesoc import Fusesoc -from fusesoc.librarymanager import Library build_root = "test_build_root" @@ -62,7 +61,6 @@ def test_library_add(caplog): import tempfile from fusesoc.coremanager import CoreManager - from fusesoc.librarymanager import LibraryManager from fusesoc.main import add_library with tempfile.TemporaryDirectory() as td: diff --git a/tests/test_vlnv.py b/tests/test_vlnv.py index 069ee0d5..329daebe 100644 --- a/tests/test_vlnv.py +++ b/tests/test_vlnv.py @@ -2,8 +2,6 @@ # Licensed under the 2-Clause BSD License, see LICENSE for details. # SPDX-License-Identifier: BSD-2-Clause -import pytest - from fusesoc.vlnv import Vlnv