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

Switch to ruff #5

Merged
merged 5 commits into from
Oct 28, 2024
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
16 changes: 0 additions & 16 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,3 @@ jobs:
- name: Run Pre-Commit
run: |-
pre-commit run --all-files
pylint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: Upgrade pip
run: |-
python -m pip install -U pip
- name: Install pylint
run: |-
python -m pip install pylint
- name: Run pylint
run: |-
pylint -dfixme raillabel_providerkit || exit $(($? & ~24))
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-format
name: Run Formatter
- id: ruff
name: Run Linter
args: [ --fix ]

- 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
111 changes: 25 additions & 86 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,31 @@ 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
]

[tool.mypy]
check_untyped_defs = true
Expand All @@ -78,82 +91,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",
]
23 changes: 11 additions & 12 deletions raillabel_providerkit/_util/_attribute_type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from enum import Enum

from raillabel_providerkit.exceptions import ValueDoesNotMatchTypeError


class AttributeType(Enum):
"""Enum of all valid RailLabel attribute types."""
Expand All @@ -14,7 +17,7 @@ class AttributeType(Enum):
VEC = "vec"

@classmethod
def from_value(cls, attribute_value_class: t.Type) -> "AttributeType":
def from_value(cls, attribute_value_class: type) -> AttributeType:
"""Return AttributeType based on class of attribute value.

Parameters
Expand All @@ -31,22 +34,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
23 changes: 12 additions & 11 deletions raillabel_providerkit/convert/convert.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

import raillabel

from ..exceptions import UnsupportedFormatError
from raillabel_providerkit.exceptions import UnsupportedFormatError

from . import loader_classes as loader_classes_pkg
from .loader_classes import LoaderABC


def convert(data: dict, loader_class: t.Optional[t.Type[LoaderABC]] = None) -> raillabel.Scene:
def convert(data: dict, loader_class: type[LoaderABC] | None = None) -> raillabel.Scene:
"""Convert annotation data from provider formats into raillabel.

Parameters
Expand All @@ -30,23 +31,23 @@ 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)

return loader_class().load(data)


def _select_loader_class(data: dict) -> t.Type[LoaderABC]:
loader_classes = []
for cls in loader_classes_pkg.__dict__.values():
if isinstance(cls, type) and issubclass(cls, LoaderABC) and cls != LoaderABC:
loader_classes.append(cls)
def _select_loader_class(data: dict) -> type[LoaderABC]:
loader_classes = [
cls
for cls in loader_classes_pkg.__dict__.values()
if isinstance(cls, type) and issubclass(cls, LoaderABC) and cls != LoaderABC
]

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
Loading
Loading