Skip to content

Commit

Permalink
add RUF and TCH rules
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Oct 17, 2023
1 parent 955d1aa commit a49e1ee
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
python -m pip install -e .[dev]
- name: Code Format Check with Black
run: |
black --check --verbose .
black --check --verbose ./src
build:
name: Build artifacts
Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ version = {attr = "pycanape.__version__"}
"py.typed",
]

[tool.black]
include = '^/((docs|src)/.*|setup)\.pyi?$'

[tool.mypy]
show_error_codes = true
ignore_missing_imports = false
disallow_untyped_defs = false
disallow_incomplete_defs = false
disallow_incomplete_defs = true
check_untyped_defs = true
warn_unused_ignores = false
warn_unused_ignores = true
warn_redundant_casts = true
disallow_any_generics = true
disallow_subclassing_any = true
Expand Down Expand Up @@ -90,13 +87,16 @@ select = [
"ISC", # flake8-implicit-str-concat
"PIE", # flake8-pie
"RSE", # flake8-raise
"RUF", # Ruff-specific rules
"S", # flake8-bandit
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = [
"E501", # line too long
"RUF012", # mutable-class-default
]

[tool.ruff.isort]
Expand Down
6 changes: 3 additions & 3 deletions src/pycanape/calibration_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return (
f"{self.__class__.__name__}("
f"asap3_handle={repr(self._asap3_handle)}, "
f"module_handle={repr(self._module_handle)}, "
f"name={repr(self._name)}, "
f"asap3_handle={self._asap3_handle!r}, "
f"module_handle={self._module_handle!r}, "
f"name={self._name!r}, "
f"object_info)"
)

Expand Down
3 changes: 2 additions & 1 deletion src/pycanape/cnp_api/cnp_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import ctypes
from ctypes import wintypes
from typing import ClassVar

from . import cnp_constants

Expand Down Expand Up @@ -39,7 +40,7 @@ class TTaskInfo(ctypes.Structure):
"""

_pack_ = 1
_fields_ = [
_fields_: ClassVar = [
("description", ctypes.c_char_p),
("taskId", ctypes.c_ushort),
("taskCycle", ctypes.c_ulong),
Expand Down
2 changes: 1 addition & 1 deletion src/pycanape/cnp_api/cnp_prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_last_error(result, function, args):

if error_code > 0:
error_msg = (
f"{cnp_constants.ErrorCodes(error_code).name}: " # type: ignore[union-attr]
f"{cnp_constants.ErrorCodes(error_code).name}: "
f"{ptr.contents.value.decode('ascii')}" # type: ignore[union-attr]
)
raise CANapeError(error_code, error_msg, function.__name__)
Expand Down
13 changes: 10 additions & 3 deletions src/pycanape/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
import ctypes
import logging
import winreg
from collections.abc import Callable
from ctypes.util import find_library
from threading import Lock
from typing import Any, ParamSpec, TypeVar

import psutil

LOG = logging.getLogger(__name__)
LOCK = Lock()

P1 = ParamSpec("P1")
T1 = TypeVar("T1")

def _synchronization_wrapper(func, func_name: str):

def _synchronization_wrapper(
func: Callable[P1, T1], func_name: str
) -> Callable[P1, T1]:
"""Use locks to assure thread safety.
Without synchronization it is possible that Asap3GetLastError
Expand Down Expand Up @@ -46,7 +53,7 @@ def __init__(self, library_or_path: str) -> None:
def function_type(self):
return ctypes.WINFUNCTYPE

def map_symbol(self, func_name: str, restype=None, argtypes=(), errcheck=None):
def map_symbol(self, func_name: str, restype=None, argtypes=(), errcheck=None) -> Callable[..., Any]: # type: ignore[no-untyped-def]
"""
Map and return a symbol (function) from a C library. A reference to the
mapped symbol is also held in the instance
Expand Down Expand Up @@ -81,7 +88,7 @@ def map_symbol(self, func_name: str, restype=None, argtypes=(), errcheck=None):


class CANapeError(Exception):
def __init__(self, error_code, error_string, function) -> None:
def __init__(self, error_code: int, error_string: str, function: str) -> None:
#: The error code according to :class:`~pycanape.cnp_api.cnp_constants.ErrorCodes`
self.error_code = error_code
super().__init__(f"{function} failed ({error_string})")
Expand Down

0 comments on commit a49e1ee

Please sign in to comment.