Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Use ruff in pre-commit to further limit code complexity #5783

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions haystack/mmh3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion haystack/modeling/data_handler/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion haystack/pipelines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 30 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/*",
Expand Down
4 changes: 3 additions & 1 deletion test/mocks/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]],
Expand All @@ -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 = []
Expand Down