Skip to content

Commit

Permalink
Add ruff as the main linter (#936)
Browse files Browse the repository at this point in the history
* Remove isort, pylint, pydocstyle and pyupgrade from pre-commit

* Update dev requirements

* Updated pre-commit

* Format based on the new blackversion

* Remove redundant packages from dev.txt
  • Loading branch information
samet-akcay authored Mar 1, 2023
1 parent b7cb94a commit 1407cfc
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 123 deletions.
60 changes: 12 additions & 48 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
# list of supported hooks: https://pre-commit.com/hooks.html
- id: trailing-whitespace
Expand All @@ -15,80 +15,44 @@ repos:

# python code formatting
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 23.1.0
hooks:
- id: black

# python import sorting
- repo: https://github.com/PyCQA/isort
rev: 5.11.5
# Ruff version.
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.253"
hooks:
- id: isort

# python code analysis
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
args: ["--max-line-length=120", "--ignore=E203,W503"]
- id: ruff
exclude: "tests"

# python linting
- repo: https://github.com/PyCQA/pylint
rev: v2.14.5
hooks:
- id: pylint
name: pylint
entry: pylint --score=no
language: system
types: [python]
exclude: "tests|docs"

# python static type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.971"
rev: "v1.0.1"
hooks:
- id: mypy
additional_dependencies: [types-PyYAML]
exclude: "tests"

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
additional_dependencies: [toml]
name: pydocstyle
entry: pydocstyle
language: python
types: [python]
exclude: "tests|docs"

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade

# notebooks.
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.4.0
rev: 1.6.3
hooks:
- id: nbqa-black
- id: nbqa-isort
- id: nbqa-flake8
- id: nbqa-pylint
- id: nbqa-ruff

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
rev: v3.0.0-alpha.4
hooks:
- id: prettier

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.32.2
rev: v0.33.0
hooks:
- id: markdownlint

- repo: https://github.com/AleksaC/hadolint-py
rev: v2.10.0
rev: v2.12.0.2
hooks:
- id: hadolint
name: Lint Dockerfiles
Expand Down
1 change: 0 additions & 1 deletion anomalib/data/utils/augmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __init__(
p_anomalous: float = 0.5,
beta: float | tuple[float, float] = (0.2, 1.0),
):

self.p_anomalous = p_anomalous
self.beta = beta

Expand Down
1 change: 0 additions & 1 deletion anomalib/deploy/inferencers/torch_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def __init__(
meta_data_path: str | Path | None = None,
device: str = "auto",
) -> None:

self.device = self._get_device(device)

# Check and load the configuration
Expand Down
1 change: 0 additions & 1 deletion anomalib/models/csflow/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ def __init__(
clamp: int = 3,
num_channels: int = 3,
) -> None:

super().__init__()
self.input_dims = (num_channels, *input_size)
self.clamp = clamp
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/fastflow/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def forward(self, hidden_variables: list[Tensor], jacobians: list[Tensor]) -> Te
Tensor: Fastflow loss computed based on the hidden variables and the log of the Jacobians.
"""
loss = torch.tensor(0.0, device=hidden_variables[0].device) # pylint: disable=not-callable
for (hidden_variable, jacobian) in zip(hidden_variables, jacobians):
for hidden_variable, jacobian in zip(hidden_variables, jacobians):
loss += torch.mean(0.5 * torch.sum(hidden_variable**2, dim=(1, 2, 3)) - jacobian)
return loss
1 change: 0 additions & 1 deletion anomalib/models/ganomaly/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ class GanomalyLightning(Ganomaly):
"""

def __init__(self, hparams: DictConfig | ListConfig) -> None:

super().__init__(
batch_size=hparams.dataset.train_batch_size,
input_size=hparams.model.input_size,
Expand Down
1 change: 0 additions & 1 deletion anomalib/models/rkde/region_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def post_process_box_predictions(self, pred_boxes: Tensor, pred_scores: Tensor)

processed_boxes: list[Tensor] = []
for boxes, scores in zip(pred_boxes, pred_scores):

# remove small boxes
keep = box_ops.remove_small_boxes(boxes, min_size=self.min_size)
boxes, scores = boxes[keep], scores[keep]
Expand Down
1 change: 0 additions & 1 deletion anomalib/pre_processing/tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def __init__(
mode: str = "padding",
tile_count: int = 4,
) -> None:

self.tile_size_h, self.tile_size_w = self.__validate_size_type(tile_size)
self.tile_count = tile_count

Expand Down
2 changes: 1 addition & 1 deletion anomalib/utils/callbacks/nncf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_target(self, _):


def wrap_nncf_model(
model: nn.Module, config: dict, dataloader: DataLoader = None, init_state_dict: dict = None
model: nn.Module, config: dict, dataloader: DataLoader, init_state_dict: dict
) -> tuple[CompressionAlgorithmController, NNCFNetwork]:
"""Wrap model by NNCF.
Expand Down
2 changes: 1 addition & 1 deletion anomalib/utils/metrics/aupro.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(
compute_on_step: bool = True,
dist_sync_on_step: bool = False,
process_group: Any | None = None,
dist_sync_fn: Callable = None,
dist_sync_fn: Callable | None = None,
fpr_limit: float = 0.3,
) -> None:
super().__init__(
Expand Down
98 changes: 42 additions & 56 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,59 @@ requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ISORT CONFIGURATION. #
[tool.isort]
profile = "black"
known_first_party = "wandb"
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER']


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# BLACK CONFIGURATION #
[tool.black]
line-length = 120

[tool.flake8]
ignore = ["E203", "W503"]
max-line-length = 120


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# PYLINT CONFIGURATION #
[tool.pylint.main]
extension-pkg-whitelist = "cv2"
ignore = ["tests", "docs"]
ignored-modules = "cv2"

[tool.pylint.messages_control]
disable = [
"duplicate-code",
"arguments-differ",
"fixme",
"import-error",
"too-many-arguments",
"too-many-branches",
"too-many-instance-attributes",
"too-many-locals",
"too-few-public-methods",
# RUFF CONFIGURATION #
[tool.ruff]
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
select = ["E", "F"]
ignore = []

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["A", "B", "C", "D", "E", "F"]
unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

[tool.pylint.typecheck]
generated-members = ["numpy.*", "torch.*"]

[tool.pylint.basic]
good-names = ["e", "i", "id"]
# Same as Black.
line-length = 120

[tool.pylint.format]
max-line-length = 120
# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.pylint.design]
max-parents = 15
# Assume Python 3.8.
target-version = "py38"

[tool.pylint.similarities]
min-similarity-lines = 5
[tool.ruff.mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Expand All @@ -83,23 +84,8 @@ exclude_lines = [
"raise ValueError",
]

[tool.pydocstyle]
inherit = false
ignore = [
"D107", # Missing docstring in __init__
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
"D401", # First line should be in imperative mood; try rephrasing
"D404", # First word of the docstring should not be This
"D406", # Section name should end with a newline
"D407", # Missing dashed underline after section
"D413", # Missing blank line after last section
]


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# NBQA CONFIGURATION #
[tool.nbqa.addopts]
pylint = ["--disable=C0103,C0114,C0116,C0413,E0401,R0801,W0106"]
flake8 = ["--ignore=E203,W503,E402", "--max-line-length=120"]
ruff = ["--ignore=E402"]
9 changes: 2 additions & 7 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
black==22.6.0
isort==5.11.5
pylint>=2.14.5
flake8>=4.0.1
pre-commit
pytest
pre-commit>=2.15.0
tox>=3.24.3
nbmake>=1.3.5
tox
1 change: 0 additions & 1 deletion tests/helpers/aupro_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def collect_anomaly_scores(anomaly_maps, ground_truth_maps):
# Collect anomaly scores within each ground truth region and for all potential fp pixels.
ok_index = 0
for gt_map, prediction in zip(ground_truth_maps, anomaly_maps):

# Compute the connected components in the ground truth map.
labeled, n_components = label(gt_map, structure)

Expand Down
4 changes: 2 additions & 2 deletions tools/inference/gradio_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def get_args() -> Namespace:
Example:
Example for Torch Inference.
>>> python tools/inference/gradio_inference.py \ ─╯
>>> python tools/inference/gradio_inference.py \
... --config ./anomalib/models/padim/config.yaml \
... --weights ./results/padim/mvtec/bottle/weights/model.ckpt # noqa: E501 #pylint: disable=line-too-long
... --weights ./results/padim/mvtec/bottle/weights/model.ckpt
Returns:
Namespace: List of arguments.
Expand Down

0 comments on commit 1407cfc

Please sign in to comment.