From 0caf4f7cd31c69692c0c77a6ced12c82062e554a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 12 Sep 2023 18:09:53 +0200 Subject: [PATCH 1/2] ci: Use ruff in pre-commit to further limit complexity --- .pre-commit-config.yaml | 5 +++ haystack/mmh3.py | 7 ++-- haystack/modeling/data_handler/processor.py | 2 +- haystack/pipelines/base.py | 2 +- pyproject.toml | 33 +++++++++++++++++-- releasenotes/notes/ruff-4d2504d362035166.yaml | 4 +++ test/mocks/pinecone.py | 4 ++- 7 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/ruff-4d2504d362035166.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a9cf58e910..228f5ec094 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,11 @@ repos: - id: trailing-whitespace # trims trailing whitespace args: [--markdown-linebreak-ext=md] +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.289 + hooks: + - id: ruff + - repo: https://github.com/psf/black rev: 23.9.1 hooks: diff --git a/haystack/mmh3.py b/haystack/mmh3.py index 7dc482dedf..c9b08286a4 100644 --- a/haystack/mmh3.py +++ b/haystack/mmh3.py @@ -22,8 +22,10 @@ def xencode(x): del _sys -def hash128(key, seed=0x0, x64arch=True): +def hash128(key, seed=0x0, x64arch=True): # noqa: C901,PLR0915 """Implements 128bit murmur3 hash.""" + # This function has a very high McCabe cyclomatic complexity score of 44 + # (recommended is 10) and contains 212 statements (recommended is 50). def hash128_x64(key, seed): """Implements 128bit murmur3 hash for x64.""" @@ -153,8 +155,9 @@ def fmix(k): return h2 << 64 | h1 - def hash128_x86(key, seed): + def hash128_x86(key, seed): # noqa: PLR0915 """Implements 128bit murmur3 hash for x86.""" + # This function contains 125 statements (recommended is 50). def fmix(h): h ^= h >> 16 diff --git a/haystack/modeling/data_handler/processor.py b/haystack/modeling/data_handler/processor.py index b2ea292b85..710a690466 100644 --- a/haystack/modeling/data_handler/processor.py +++ b/haystack/modeling/data_handler/processor.py @@ -474,7 +474,7 @@ def dataset_from_dicts( max_answers = ( self.max_answers if self.max_answers is not None - else max(max(len(basket.raw["answers"]) for basket in baskets), 1) + else max(*(len(basket.raw["answers"]) for basket in baskets), 1) ) # Convert answers from string to token space, skip this step for inference diff --git a/haystack/pipelines/base.py b/haystack/pipelines/base.py index 8d4ea8a515..ea81f3e554 100644 --- a/haystack/pipelines/base.py +++ b/haystack/pipelines/base.py @@ -607,7 +607,7 @@ def run( # type: ignore return node_output - def run_batch( # type: ignore + def run_batch( # noqa: C901,PLR0912 type: ignore self, queries: Optional[List[str]] = None, file_paths: Optional[List[str]] = None, diff --git a/pyproject.toml b/pyproject.toml index b3cc532e48..ace4a6de3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -332,9 +332,11 @@ disable = [ [tool.pylint.'DESIGN'] max-args = 37 # Default is 5 max-attributes = 27 # Default is 7 -max-branches = 33 # Default is 12 -max-locals = 44 # Default is 15 -max-statements = 205 # Default is 50 +max-branches = 34 # Default is 12 +max-locals = 45 # Default is 15 +max-module-lines = 2468 # Default is 1000 +max-nested-blocks = 7 # Default is 5 +max-statements = 206 # Default is 50 [tool.pylint.'SIMILARITIES'] min-similarity-lines=6 @@ -370,6 +372,31 @@ plugins = [ "pydantic.mypy", ] +[tool.ruff] +ignore = [ + "PLR1714", # repeated-equality-comparison + "PLR5501", # collapsible-else-if + "PLW0603", # global-statement + "PLW1510", # subprocess-run-without-check + "PLW2901", # redefined-loop-name + "W605", # invalid-escape-sequence +] +select = [ + "C90", # McCabe cyclomatic complexity + "PL", # Pylint + "W", # Pycodestyle warnings +] + +[tool.ruff.mccabe] +max-complexity = 28 + +[tool.ruff.pylint] +allow-magic-value-types = ["float", "int", "str"] +max-args = 38 # Default is 5 +max-branches = 32 # Default is 12 +max-returns = 9 # Default is 6 +max-statements = 105 # Default is 50 + [tool.coverage.run] omit = [ "haystack/testing/*", diff --git a/releasenotes/notes/ruff-4d2504d362035166.yaml b/releasenotes/notes/ruff-4d2504d362035166.yaml new file mode 100644 index 0000000000..15fd9c4534 --- /dev/null +++ b/releasenotes/notes/ruff-4d2504d362035166.yaml @@ -0,0 +1,4 @@ +--- +enhancements: + - | + Use ruff in pre-commit to further limit code complexity. diff --git a/test/mocks/pinecone.py b/test/mocks/pinecone.py index 2c247dbf45..60a2518f03 100644 --- a/test/mocks/pinecone.py +++ b/test/mocks/pinecone.py @@ -148,7 +148,7 @@ def fetch(self, ids: List[str], namespace: str = ""): } return response - def _filter( + def _filter( # noqa: C901,PLR0912 self, metadata: dict, filters: Optional[Union[FilterType, List[Optional[FilterType]]]], @@ -158,6 +158,8 @@ def _filter( """ Mock filtering function """ + # This function has a very high McCabe cyclomatic complexity score of 38 + # (recommended is 10) and contains 55 branches (recommended is 12). bools = [] if type(filters) is list: list_bools = [] From 0477c2170fd87910ea7d22979225e4b7fe54ecf1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 13 Sep 2023 13:21:40 +0200 Subject: [PATCH 2/2] Delete releasenotes/notes/ruff-4d2504d362035166.yaml --- releasenotes/notes/ruff-4d2504d362035166.yaml | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 releasenotes/notes/ruff-4d2504d362035166.yaml diff --git a/releasenotes/notes/ruff-4d2504d362035166.yaml b/releasenotes/notes/ruff-4d2504d362035166.yaml deleted file mode 100644 index 15fd9c4534..0000000000 --- a/releasenotes/notes/ruff-4d2504d362035166.yaml +++ /dev/null @@ -1,4 +0,0 @@ ---- -enhancements: - - | - Use ruff in pre-commit to further limit code complexity.