From 223fefbbfacc813cd1a9ffde9499b96302c76da7 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Sat, 13 Apr 2024 09:56:33 +0200 Subject: [PATCH] Enable Ruff RUF018 (#115485) --- homeassistant/components/api/__init__.py | 3 ++- homeassistant/components/light/__init__.py | 12 ++++++------ .../components/mqtt_statestream/__init__.py | 3 ++- homeassistant/components/ring/camera.py | 3 ++- homeassistant/components/zwave_js/diagnostics.py | 3 ++- pyproject.toml | 1 + tests/ruff.toml | 1 + 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/api/__init__.py b/homeassistant/components/api/__init__.py index 496b6fa5fb1..73751daa6cb 100644 --- a/homeassistant/components/api/__init__.py +++ b/homeassistant/components/api/__init__.py @@ -284,7 +284,8 @@ class APIEntityStateView(HomeAssistantView): # Read the state back for our response status_code = HTTPStatus.CREATED if is_new_state else HTTPStatus.OK - assert (state := hass.states.get(entity_id)) + state = hass.states.get(entity_id) + assert state resp = self.json(state.as_dict(), status_code) resp.headers.add("Location", f"/api/states/{entity_id}") diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 332d701148e..b3b1330b3a1 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -517,13 +517,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: params[ATTR_COLOR_TEMP_KELVIN] ) elif ATTR_RGB_COLOR in params and ColorMode.RGB not in supported_color_modes: - assert (rgb_color := params.pop(ATTR_RGB_COLOR)) is not None + rgb_color = params.pop(ATTR_RGB_COLOR) + assert rgb_color is not None if ColorMode.RGBW in supported_color_modes: params[ATTR_RGBW_COLOR] = color_util.color_rgb_to_rgbw(*rgb_color) elif ColorMode.RGBWW in supported_color_modes: - # https://github.com/python/mypy/issues/13673 params[ATTR_RGBWW_COLOR] = color_util.color_rgb_to_rgbww( - *rgb_color, # type: ignore[call-arg] + *rgb_color, light.min_color_temp_kelvin, light.max_color_temp_kelvin, ) @@ -584,9 +584,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: elif ( ATTR_RGBWW_COLOR in params and ColorMode.RGBWW not in supported_color_modes ): - assert (rgbww_color := params.pop(ATTR_RGBWW_COLOR)) is not None - # https://github.com/python/mypy/issues/13673 - rgb_color = color_util.color_rgbww_to_rgb( # type: ignore[call-arg] + rgbww_color = params.pop(ATTR_RGBWW_COLOR) + assert rgbww_color is not None + rgb_color = color_util.color_rgbww_to_rgb( *rgbww_color, light.min_color_temp_kelvin, light.max_color_temp_kelvin ) if ColorMode.RGB in supported_color_modes: diff --git a/homeassistant/components/mqtt_statestream/__init__.py b/homeassistant/components/mqtt_statestream/__init__.py index 6a1a791d7ac..3a0953a0158 100644 --- a/homeassistant/components/mqtt_statestream/__init__.py +++ b/homeassistant/components/mqtt_statestream/__init__.py @@ -57,7 +57,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _state_publisher(evt: Event[EventStateChangedData]) -> None: entity_id = evt.data["entity_id"] - assert (new_state := evt.data["new_state"]) + new_state = evt.data["new_state"] + assert new_state payload = new_state.state diff --git a/homeassistant/components/ring/camera.py b/homeassistant/components/ring/camera.py index 282f9816c4c..a5144777eaa 100644 --- a/homeassistant/components/ring/camera.py +++ b/homeassistant/components/ring/camera.py @@ -167,7 +167,8 @@ class RingCam(RingEntity[RingDoorBell], Camera): def _get_video(self) -> str | None: if self._last_event is None: return None - assert (event_id := self._last_event.get("id")) and isinstance(event_id, int) + event_id = self._last_event.get("id") + assert event_id and isinstance(event_id, int) return self._device.recording_url(event_id) @exception_wrap diff --git a/homeassistant/components/zwave_js/diagnostics.py b/homeassistant/components/zwave_js/diagnostics.py index 777d45efddb..3d61699472d 100644 --- a/homeassistant/components/zwave_js/diagnostics.py +++ b/homeassistant/components/zwave_js/diagnostics.py @@ -151,7 +151,8 @@ async def async_get_device_diagnostics( client: Client = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] identifiers = get_home_and_node_id_from_device_entry(device) node_id = identifiers[1] if identifiers else None - assert (driver := client.driver) + driver = client.driver + assert driver if node_id is None or node_id not in driver.controller.nodes: raise ValueError(f"Node for device {device.id} can't be found") node = driver.controller.nodes[node_id] diff --git a/pyproject.toml b/pyproject.toml index 6b61766d4b8..b9111f505c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -701,6 +701,7 @@ select = [ "RUF005", # Consider iterable unpacking instead of concatenation "RUF006", # Store a reference to the return value of asyncio.create_task "RUF013", # PEP 484 prohibits implicit Optional + "RUF018", # Avoid assignment expressions in assert statements # "RUF100", # Unused `noqa` directive; temporarily every now and then to clean them up "S102", # Use of exec detected "S103", # bad-file-permissions diff --git a/tests/ruff.toml b/tests/ruff.toml index 5455e211762..87725160751 100644 --- a/tests/ruff.toml +++ b/tests/ruff.toml @@ -6,6 +6,7 @@ extend = "../pyproject.toml" extend-ignore = [ "B904", # Use raise from to specify exception cause "N815", # Variable {name} in class scope should not be mixedCase + "RUF018", # Avoid assignment expressions in assert statements ] [lint.isort]