Skip to content

Commit

Permalink
Properly switch the formatter to ruff
Browse files Browse the repository at this point in the history
This avoids having to configure both black and ruff
  • Loading branch information
canton7 committed Feb 1, 2025
1 parent e4ddf3d commit d2a06cd
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 43 deletions.
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"extensions": [
"ms-python.python",
"charliermarsh.ruff",
"ms-python.black-formatter",
"ms-python.vscode-pylance",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
Expand Down
10 changes: 1 addition & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ repos:
- id: end-of-file-fixer
exclude: ^custom_components\/foxess_modbus\/vendor
- id: trailing-whitespace
- repo: local
hooks:
- id: black
name: black
entry: black
language: system
types: [python]
require_serial: true
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.2.1
hooks:
- id: prettier
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.275
rev: v0.9.4
hooks:
- id: ruff
# Temporarily disabled due to crash, probably caused by us having to use HA stubs which are out of date?
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.analysis.extraPaths": [
"./custom_components/foxess_modbus/vendor/pymodbus/pymodbus-3.5.4"
Expand Down
11 changes: 3 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ Pull requests are the best way to propose changes to the codebase.

1. Fork the repo and create your branch from `master`.
2. If you've changed something, update the documentation.
3. Make sure your code lints (using black).
4. Test you contribution.
5. Issue that pull request!
3. Test you contribution.
4. Issue that pull request!

## Any contributions you make will be under the MIT Software License

Expand All @@ -44,11 +43,7 @@ People _love_ thorough bug reports. I'm not even kidding.

## Use a Consistent Coding Style

Use [black](https://github.com/ambv/black) and [prettier](https://prettier.io/)
to make sure the code follows the style.

Or use the `pre-commit` settings implemented in this repository
(see deicated section below).
Use the `pre-commit` settings implemented in this repository (see dedicated section below).

## Test your code modification

Expand Down
4 changes: 2 additions & 2 deletions custom_components/foxess_modbus/client/protocol_pollserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def read(self, size: int = 1) -> bytes:
result == _PollResult.TIMEOUT
or result == _PollResult.ABORT
or timeout.expired()
or (self._inter_byte_timeout is not None and self._inter_byte_timeout > 0)
and not buf
or ((self._inter_byte_timeout is not None and self._inter_byte_timeout > 0)
and not buf)
):
break # early abort on timeout
return bytes(read)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/foxess_modbus/common/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Defines RegisterType"""
"""Defines RegisterType""" # noqa: A005

from enum import Enum
from enum import Flag
Expand Down
2 changes: 1 addition & 1 deletion custom_components/foxess_modbus/entities/base_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class BaseValidator(ABC):
"""Base validator"""

@abstractmethod
def validate(self, data: int | float) -> bool:
def validate(self, data: float) -> bool:
"""Validate a value against a set of rules"""
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def _get_entity_id(self, platform: Platform) -> str:
def _validate(
self,
rules: list[BaseValidator],
processed: float | int,
original: float | int | None = None,
processed: float,
original: float | None = None,
address_override: int | None = None,
) -> bool:
"""Validate against a set of rules"""
Expand Down
8 changes: 4 additions & 4 deletions custom_components/foxess_modbus/entities/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, min_value: float, max_value: float) -> None:
self._min = min_value
self._max = max_value

def validate(self, data: int | float) -> bool:
def validate(self, data: float) -> bool:
"""Validate a value against a set of rules"""

return self._min <= data <= self._max
Expand All @@ -24,7 +24,7 @@ def __init__(self, min_value: float) -> None:
"""Init"""
self._min = min_value

def validate(self, data: int | float) -> bool:
def validate(self, data: float) -> bool:
"""Validate a value against a set of rules"""

return data >= self._min
Expand All @@ -37,7 +37,7 @@ def __init__(self, max_value: float) -> None:
"""Init"""
self._max = max_value

def validate(self, data: int | float) -> bool:
def validate(self, data: float) -> bool:
"""Validate a value against a set of rules"""

return data <= self._max
Expand All @@ -46,6 +46,6 @@ def validate(self, data: int | float) -> bool:
class Time(BaseValidator):
"""Time validator"""

def validate(self, data: int | float) -> bool:
def validate(self, data: float) -> bool:
"""Validate a value against a set of rules"""
return isinstance(data, int) and is_time_value_valid(data)
4 changes: 2 additions & 2 deletions custom_components/foxess_modbus/modbus_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async def write_registers(self, start_address: int, values: list[int]) -> None:
self._notify_update(changed_addresses)
except Exception as ex:
# Failed writes are always bad
_LOGGER.error("Failed to write registers", exc_info=True)
_LOGGER.exception("Failed to write registers")
raise ex

async def _refresh(self, _time: datetime) -> None:
Expand Down Expand Up @@ -538,7 +538,7 @@ async def autodetect(client: ModbusClient, slave: int, adapter_config: dict[str,
_LOGGER.error("Did not recognise inverter model '%s' (%s)", full_model, register_values)
raise UnsupportedInverterError(full_model)
except Exception as ex:
_LOGGER.error("Autodetect: failed to connect to (%s)", client, exc_info=True)
_LOGGER.exceptino("Autodetect: failed to connect to (%s)", client)
raise AutoconnectFailedError(spy_handler.records) from ex
finally:
pymodbus_logger.removeHandler(spy_handler)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/foxess_modbus/select.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Sensor platform for foxess_modbus."""
"""Sensor platform for foxess_modbus.""" # noqa: A005

import logging

Expand Down
13 changes: 4 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,26 @@ module = 'pymodbus.*'
ignore_missing_imports = true

[tool.ruff]
select = ["E", "F", "I", "N", "ANN", "S", "B", "A", "COM", "C4", "DTZ", "ISC", "ICN", "G",
lint.select = ["E", "F", "I", "N", "ANN", "S", "B", "A", "COM", "C4", "DTZ", "ISC", "ICN", "G",
"PIE", "PYI", "RET", "SLF", "SIM", "ARG", "PTH", "PLE", "PLC", "PLW", "FLY", "RUF"]
ignore = [
"ANN101", # missing-type-self
lint.ignore = [
"ANN401", # any-type
"S101", # assert
"SIM108", # if-else-block-instead-of-if-exp
"COM812", # misisng-trailing-comma
"PLW2901", # redefiend-loop-name
"RET504", # unnecessary-assign
]
unfixable = [
lint.unfixable = [
"F841", # unused-variable
]
line-length = 120
extend-exclude = [
'custom_components/foxess_modbus/vendor',
]

[tool.ruff.isort]
[tool.ruff.lint.isort]
force-single-line = true

[tool.black]
line-length = 120
exclude = "scripts"

[tool.pytest.ini_options]
asyncio_mode = "auto"
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ pytest-asyncio

colorlog==6.7.0
pre-commit==3.3.3
black==23.9.0
ruff==0.0.275
ruff==0.9.4
# These are duplicated in .pre-commit-config.yaml
reorder-python-imports==3.10.0
mypy==1.5.1
Expand Down

0 comments on commit d2a06cd

Please sign in to comment.