From 4dc2f147ecf5290e3263b4dfd49453e5097a09fb Mon Sep 17 00:00:00 2001 From: GSRN Date: Mon, 15 Sep 2025 21:16:37 +0200 Subject: [PATCH] fix: Standardize isort command in CI workflows ### Summary of Changes - Updated the `isort` command in both CI workflows to include the `--profile black` option for consistent code formatting. - Refactored function definitions in service adapters to improve readability by consolidating parameters into single lines. ### Expected Results - Enhanced consistency in code formatting checks across CI workflows, ensuring adherence to the Black style guide. - Improved readability and maintainability of function definitions in service adapters. --- .gitea/workflows/ci.yml | 2 +- .gitea/workflows/service-adapters.yml | 2 +- services/service-adapters/main_old.py | 18 +++--------- services/service-adapters/pyproject.toml | 28 +++++++++++++++++++ services/service-adapters/routes/events.py | 6 +--- .../service-adapters/routes/home_assistant.py | 12 ++------ .../tests/test_home_assistant_routes.py | 4 +-- .../service-adapters/tests/test_models.py | 19 ++++--------- 8 files changed, 44 insertions(+), 47 deletions(-) create mode 100644 services/service-adapters/pyproject.toml diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 1d7231b..e46d206 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -103,7 +103,7 @@ jobs: - name: Run code formatting check run: | black --check . - isort --check-only . + isort --check-only --profile black . - name: Run linting run: flake8 . --count --max-complexity=10 --max-line-length=150 diff --git a/.gitea/workflows/service-adapters.yml b/.gitea/workflows/service-adapters.yml index 4abb5bb..1771d0a 100644 --- a/.gitea/workflows/service-adapters.yml +++ b/.gitea/workflows/service-adapters.yml @@ -68,7 +68,7 @@ jobs: - name: Run code formatting check run: | black --check --diff . - isort --check-only --diff . + isort --check-only --diff --profile black . - name: Run linting run: | diff --git a/services/service-adapters/main_old.py b/services/service-adapters/main_old.py index 71095e7..2383da9 100644 --- a/services/service-adapters/main_old.py +++ b/services/service-adapters/main_old.py @@ -217,16 +217,12 @@ async def get_ha_entities(): HAEntity( entity_id="sensor.cpu_usage", state="45.2", - attributes=HAAttributes( - unit_of_measurement="%", friendly_name="CPU Usage" - ), + attributes=HAAttributes(unit_of_measurement="%", friendly_name="CPU Usage"), ), HAEntity( entity_id="sensor.memory_usage", state="2.1", - attributes=HAAttributes( - unit_of_measurement="GB", friendly_name="Memory Usage" - ), + attributes=HAAttributes(unit_of_measurement="GB", friendly_name="Memory Usage"), ), ] ) @@ -340,11 +336,7 @@ async def publish_event(event_data: EventData, background_tasks: BackgroundTasks }, tags=["Events"], ) -async def get_events( - limit: int = Query( - 100, ge=1, le=1000, description="Maximum number of events to retrieve" - ) -): +async def get_events(limit: int = Query(100, ge=1, le=1000, description="Maximum number of events to retrieve")): """Get recent events from the Redis message bus""" try: events = redis_client.lrange("events", 0, limit - 1) @@ -386,9 +378,7 @@ async def get_ha_entity(entity_id: str = Path(..., description="Entity ID")): return HAEntity( entity_id=entity_id, state="unknown", - attributes=HAAttributes( - unit_of_measurement="", friendly_name=f"Entity {entity_id}" - ), + attributes=HAAttributes(unit_of_measurement="", friendly_name=f"Entity {entity_id}"), ) diff --git a/services/service-adapters/pyproject.toml b/services/service-adapters/pyproject.toml new file mode 100644 index 0000000..035baa2 --- /dev/null +++ b/services/service-adapters/pyproject.toml @@ -0,0 +1,28 @@ +[tool.black] +line-length = 150 +target-version = ['py311'] +include = '\.pyi?$' +extend-exclude = ''' +/( + # directories + \.eggs + | \.git + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | build + | dist +)/ +''' + +[tool.isort] +profile = "black" +line_length = 150 +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +ensure_newline_before_comments = true +known_first_party = ["models", "routes", "services"] +known_third_party = ["fastapi", "pytest", "pydantic"] diff --git a/services/service-adapters/routes/events.py b/services/service-adapters/routes/events.py index d3930d4..2a45646 100644 --- a/services/service-adapters/routes/events.py +++ b/services/service-adapters/routes/events.py @@ -49,11 +49,7 @@ async def publish_event(event_data: EventData, background_tasks: BackgroundTasks }, tags=["Events"], ) -async def get_events( - limit: int = Query( - 100, ge=1, le=1000, description="Maximum number of events to retrieve" - ) -): +async def get_events(limit: int = Query(100, ge=1, le=1000, description="Maximum number of events to retrieve")): """Get recent events from the Redis message bus""" try: events = redis_client.lrange("events", 0, limit - 1) diff --git a/services/service-adapters/routes/home_assistant.py b/services/service-adapters/routes/home_assistant.py index e1596af..40badc7 100644 --- a/services/service-adapters/routes/home_assistant.py +++ b/services/service-adapters/routes/home_assistant.py @@ -32,16 +32,12 @@ async def get_ha_entities(): HAEntity( entity_id="sensor.cpu_usage", state="45.2", - attributes=HAAttributes( - unit_of_measurement="%", friendly_name="CPU Usage" - ), + attributes=HAAttributes(unit_of_measurement="%", friendly_name="CPU Usage"), ), HAEntity( entity_id="sensor.memory_usage", state="2.1", - attributes=HAAttributes( - unit_of_measurement="GB", friendly_name="Memory Usage" - ), + attributes=HAAttributes(unit_of_measurement="GB", friendly_name="Memory Usage"), ), ] ) @@ -72,7 +68,5 @@ async def get_ha_entity(entity_id: str = Path(..., description="Entity ID")): return HAEntity( entity_id=entity_id, state="unknown", - attributes=HAAttributes( - unit_of_measurement="", friendly_name=f"Entity {entity_id}" - ), + attributes=HAAttributes(unit_of_measurement="", friendly_name=f"Entity {entity_id}"), ) diff --git a/services/service-adapters/tests/test_home_assistant_routes.py b/services/service-adapters/tests/test_home_assistant_routes.py index 65d0528..6c60248 100644 --- a/services/service-adapters/tests/test_home_assistant_routes.py +++ b/services/service-adapters/tests/test_home_assistant_routes.py @@ -72,9 +72,7 @@ class TestHomeAssistantRoutes: async def test_get_entities_connection_error(self, mock_client_class): """Test handling of connection errors""" # Mock connection error - mock_client_class.return_value.__aenter__.side_effect = Exception( - "Connection failed" - ) + mock_client_class.return_value.__aenter__.side_effect = Exception("Connection failed") response = client.get("/home-assistant/entities") assert response.status_code == 500 diff --git a/services/service-adapters/tests/test_models.py b/services/service-adapters/tests/test_models.py index 0ab02e9..052e516 100644 --- a/services/service-adapters/tests/test_models.py +++ b/services/service-adapters/tests/test_models.py @@ -6,8 +6,7 @@ from datetime import datetime import pytest -from models.schemas import (EventData, FrigateEvent, HAAttributes, HAEntity, - HealthResponse, RootResponse, ServiceStatus) +from models.schemas import EventData, FrigateEvent, HAAttributes, HAEntity, HealthResponse, RootResponse, ServiceStatus class TestServiceStatus: @@ -15,9 +14,7 @@ class TestServiceStatus: def test_service_status_creation(self): """Test creating a ServiceStatus instance""" - service = ServiceStatus( - enabled=True, url="http://example.com", status="healthy" - ) + service = ServiceStatus(enabled=True, url="http://example.com", status="healthy") assert service.enabled is True assert service.url == "http://example.com" assert service.status == "healthy" @@ -25,9 +22,7 @@ class TestServiceStatus: def test_service_status_validation(self): """Test ServiceStatus validation""" # Valid data - service = ServiceStatus( - enabled=False, url="https://api.example.com", status="unhealthy" - ) + service = ServiceStatus(enabled=False, url="https://api.example.com", status="unhealthy") assert service.enabled is False def test_service_status_required_fields(self): @@ -41,9 +36,7 @@ class TestHAAttributes: def test_ha_attributes_creation(self): """Test creating HAAttributes instance""" - attrs = HAAttributes( - unit_of_measurement="°C", friendly_name="Living Room Temperature" - ) + attrs = HAAttributes(unit_of_measurement="°C", friendly_name="Living Room Temperature") assert attrs.unit_of_measurement == "°C" assert attrs.friendly_name == "Living Room Temperature" @@ -60,9 +53,7 @@ class TestHAEntity: def test_ha_entity_creation(self): """Test creating HAEntity instance""" attributes = HAAttributes(unit_of_measurement="°C", friendly_name="Temperature") - entity = HAEntity( - entity_id="sensor.temperature", state="22.5", attributes=attributes - ) + entity = HAEntity(entity_id="sensor.temperature", state="22.5", attributes=attributes) assert entity.entity_id == "sensor.temperature" assert entity.state == "22.5" assert entity.attributes.unit_of_measurement == "°C"