Skip to content

Commit

Permalink
lint: change linter to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Oct 28, 2024
1 parent 8f036f4 commit 852fefe
Show file tree
Hide file tree
Showing 69 changed files with 885 additions and 865 deletions.
30 changes: 10 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,16 @@ repos:
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
additional_dependencies:
- docformatter[tomli]
- repo: https://github.com/PyCQA/pydocstyle
rev: 6.3.0

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
hooks:
- id: pydocstyle
exclude: '^tests/'
additional_dependencies:
- pydocstyle[toml]
- id: ruff
name: Run Linter
args: [ --fix ]
- id: ruff-format
name: Run Formatter

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
Expand Down Expand Up @@ -89,6 +78,7 @@ repos:
- LICENSES/.license_header.txt
- --comment-style
- '..| |'

- repo: https://github.com/fsfe/reuse-tool
rev: v4.0.3
hooks:
Expand Down
6 changes: 2 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@

# The full version, including alpha/beta/rc tags.
version = raillabel_providerkit.__version__
rst_epilog = """
rst_epilog = f"""
.. |Project| replace:: {project}
.. |Version| replace:: {version}
""".format(
project=project, version=version
)
"""


# -- Options for copy-button -------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions git-conventional-commits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"convention" : {
"commitTypes": [
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"merge",
"perf",
"refactor",
"revert",
"test",

"lint"
],
"commitScopes": []
}
}
2 changes: 2 additions & 0 deletions git-conventional-commits.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Copyright DB InfraGO AG and the raillabel contributors
SPDX-License-Identifier: Apache-2.0
116 changes: 30 additions & 86 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,36 @@ test = [
"json5"
]

[tool.black]
line-length = 100
target-version = ["py38"]
force-exclude = "tests/"
[tool.ruff]
line-length = 101

[tool.docformatter]
wrap-descriptions = 72
wrap-summaries = 79
[tool.ruff.lint]
exclude = ["tests/*", "docs/*"]
select = ["ALL"]
ignore = [
"COM812", # conflicts with ruff formatter

[tool.isort]
profile = 'black'
line_length = 100
"D100", # imo no docstrings are necessary in public modules
"D107", # __init__ docstrings are not necessary
"D203", # incompatible with D211
"D213", # incompatible with D212

"FBT001", # flags in functions are not bad practice
"FBT002", # flags in functions are not bad practice

"ISC001", # conflicts with ruff formatter

"TCH001", # adds hard to understand compexity without providing a benefit for smaller projects
"TCH002", # same as TCH001
"TCH003", # same as TCH001

"SIM103", # less readable in some cases imo

# to be removed later
"TID252",
"FA100",
"PERF401",
]

[tool.mypy]
check_untyped_defs = true
Expand All @@ -78,82 +96,8 @@ warn_redundant_casts = true
warn_unreachable = true
python_version = "3.12"
ignore_missing_imports = true

[tool.pydocstyle]
convention = "numpy"
add-select = [
"D212", # Multi-line docstring summary should start at the first line
"D402", # First line should not be the functions "signature"
"D417", # Missing argument descriptions in the docstring
]
add-ignore = [
"D100", # Missing docstring in public module
"D201", # No blank lines allowed before function docstring # auto-formatting
"D202", # No blank lines allowed after function docstring # auto-formatting
"D203", # 1 blank line required before class docstring # auto-formatting
"D204", # 1 blank line required after class docstring # auto-formatting
"D209", # Multi-line docstring closing quotes should be on a separate line
"D211", # No blank lines allowed before class docstring # auto-formatting
"D213", # Multi-line docstring summary should start at the second line
]

[tool.pylint.master]
max-line-length = 100

[tool.pylint.messages_control]
disable = [
"arguments-renamed",
"global-statement",
"invalid-name",
"no-else-return", # using else returns is more readible imo
"protected-access", # class comparisons raised as false positive
"redefined-builtin", # the domain is full of builtin-names (object, type, format, ...)
"too-few-public-methods", # does not contribute to code quality imo
"too-many-arguments", # 6 as a limit is too low
"too-many-instance-attributes", # classes mirror OpenLABEL, therefore the number of fields is set
"unidiomatic-typecheck", # type() is necessary in some cases
"unspecified-encoding", # default encoding is sufficient in all cases
"unsupported-membership-test", # raise false positives for dicts
"global-variable-not-assigned", # raises false positive when global variable is a dict and items are assigned

# Auto-formatting
"bad-indentation",
"inconsistent-quotes",
"missing-final-newline",
"missing-class-docstring",
"missing-function-docstring",
"missing-module-docstring",
"mixed-line-endings",
"multiple-imports",
"multiple-statements",
"trailing-newlines",
"trailing-whitespace",
"unexpected-line-ending-format",
"ungrouped-imports",
"wrong-import-order",
"wrong-import-position",

# Handled by mypy
"arguments-differ",
"assignment-from-no-return",
"import-error",
"missing-kwoa",
"no-member",
"no-value-for-parameter",
"redundant-keyword-arg",
"signature-differs",
"syntax-error",
"too-many-function-args",
"unbalanced-tuple-unpacking",
"undefined-variable",
"unexpected-keyword-arg",
]
enable = [
"c-extension-no-member",
"deprecated-pragma",
"use-symbolic-message-instead",
"useless-suppression",
]
disable_error_code = ["call-arg"]
exclude = ["tests/*"]

[tool.pytest.ini_options]
addopts = """
Expand Down
8 changes: 8 additions & 0 deletions raillabel_providerkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0
"""A library for annotation providers of raillabel-formatted data."""

from importlib import metadata

from . import format
Expand All @@ -13,3 +14,10 @@
except metadata.PackageNotFoundError:
__version__ = "0.0.0+unknown"
del metadata

__all__ = [
"format",
"loader_classes",
"convert",
"validate",
]
18 changes: 8 additions & 10 deletions raillabel_providerkit/_util/_attribute_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import typing as t
from enum import Enum

from ..exceptions import ValueDoesNotMatchTypeError


class AttributeType(Enum):
"""Enum of all valid RailLabel attribute types."""
Expand Down Expand Up @@ -31,22 +33,18 @@ def from_value(cls, attribute_value_class: t.Type) -> "AttributeType":
------
ValueError
if attribute value class does not correspond to an Attribute Type.
"""
if attribute_value_class == str:
"""
if attribute_value_class is str:
return AttributeType.TEXT

elif attribute_value_class in [float, int]:
if attribute_value_class in [float, int]:
return AttributeType.NUM

elif attribute_value_class == bool:
if attribute_value_class is bool:
return AttributeType.BOOLEAN

elif attribute_value_class in [list, tuple]:
if attribute_value_class in [list, tuple]:
return AttributeType.VEC

else:
raise ValueError(
f"Type {attribute_value_class} does not correspond to a valid RailLabel attribute "
+ "type. Supported types are str, float, int, bool, list, tuple."
)
raise ValueDoesNotMatchTypeError(attribute_value_class)
15 changes: 11 additions & 4 deletions raillabel_providerkit/_util/_warning.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import logging
import typing as t
from io import StringIO
from types import TracebackType


class _WarningsLogger:
warnings: t.ClassVar[list[str]] = []

warnings: t.List[str] = []

def __enter__(self) -> "_WarningsLogger":
def __enter__(self) -> None:
logger = logging.getLogger("loader_warnings")
warnings_stream = StringIO()
handler = logging.StreamHandler(warnings_stream)
Expand All @@ -19,7 +21,12 @@ def __enter__(self) -> "_WarningsLogger":

return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(
self,
typ: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
logger = logging.getLogger("loader_warnings")
stream = logger.handlers[-1].stream
stream.seek(0)
Expand Down
5 changes: 2 additions & 3 deletions raillabel_providerkit/convert/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def convert(data: dict, loader_class: t.Optional[t.Type[LoaderABC]] = None) -> r
------
raillabel.UnsupportedFormatError
if the annotation file does not match any loaders.
"""
"""
if loader_class is None:
loader_class = _select_loader_class(data)

Expand All @@ -45,8 +45,7 @@ def _select_loader_class(data: dict) -> t.Type[LoaderABC]:
loader_classes.append(cls)

for loader_class in loader_classes:

if loader_class().supports(data):
return loader_class

raise UnsupportedFormatError("No loader could be found, that supported the provided data.")
raise UnsupportedFormatError
3 changes: 2 additions & 1 deletion raillabel_providerkit/convert/loader_classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

from ._loader_abc import LoaderABC

__all__ = ["LoaderABC"]

# iterate through the modules in the current package
package_dir = str(Path(__file__).resolve().parent)
for _, module_name, _ in iter_modules([package_dir]):

# import the module and iterate through its attributes
module = import_module(f"{__name__}.{module_name}")
for attribute_name in dir(module):
Expand Down
3 changes: 3 additions & 0 deletions raillabel_providerkit/convert/loader_classes/_loader_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LoaderABC(ABC):
List of warning strings, that have been found during the execution of load().
SCHEMA_PATH: Path
Absolute path to the JSON schema.
"""

scene: raillabel.Scene
Expand All @@ -47,6 +48,7 @@ def load(self, data: dict, validate: bool = True) -> raillabel.Scene:
-------
scene: raillabel.Scene
The loaded scene with the data.
"""
raise NotImplementedError

Expand All @@ -66,5 +68,6 @@ def supports(self, data: dict) -> bool:
-------
bool:
If True, the Loader class is suitable for the data.
"""
raise NotImplementedError
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LoaderUnderstandAi(LoaderABC):
Loaded raillabel.format.understand_ai.Scene with the data.
warnings: t.List[str]
List of warning strings, that have been found during the execution of load().
"""

scene: uai_format.Scene
Expand All @@ -47,8 +48,8 @@ def load(self, data: dict, validate_schema: bool = False) -> uai_format.Scene:
-------
scene: raillabel.format.understand_ai.UAIScene
The loaded scene with the data.
"""
"""
if validate_schema:
self.validate_schema(data)

Expand All @@ -74,8 +75,8 @@ def supports(self, data: dict) -> bool:
-------
bool:
If True, the Loader class is suitable for the data.
"""
"""
return (
"metadata" in data
and "project_id" in data["metadata"]
Expand Down
Loading

0 comments on commit 852fefe

Please sign in to comment.