From 3142f52a794935484293c419eacdad6c01732167 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Wed, 10 Jul 2024 21:47:40 +0200 Subject: [PATCH] Add Ruff refurb rules (#121701) --- homeassistant/components/conversation/util.py | 2 +- homeassistant/components/esphome/light.py | 4 ++-- homeassistant/components/refoss/sensor.py | 2 +- homeassistant/components/sensor/recorder.py | 2 +- homeassistant/helpers/template.py | 8 ++++---- homeassistant/util/color.py | 2 +- pyproject.toml | 1 + script/licenses.py | 10 +++++----- 8 files changed, 16 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/conversation/util.py b/homeassistant/components/conversation/util.py index b4ff2511ca1..4326c95cb66 100644 --- a/homeassistant/components/conversation/util.py +++ b/homeassistant/components/conversation/util.py @@ -34,4 +34,4 @@ def create_matcher(utterance: str) -> re.Pattern[str]: pattern.append(rf"(?:{optional_match.groups()[0]} *)?") pattern.append("$") - return re.compile("".join(pattern), re.I) + return re.compile("".join(pattern), re.IGNORECASE) diff --git a/homeassistant/components/esphome/light.py b/homeassistant/components/esphome/light.py index 295f9365cd0..52f999afe4f 100644 --- a/homeassistant/components/esphome/light.py +++ b/homeassistant/components/esphome/light.py @@ -122,7 +122,7 @@ def _color_mode_to_ha(mode: int) -> str: return ColorMode.UNKNOWN # choose the color mode with the most bits set - candidates.sort(key=lambda key: bin(key[1]).count("1")) + candidates.sort(key=lambda key: key[1].bit_count()) return candidates[-1][0] @@ -146,7 +146,7 @@ def _least_complex_color_mode(color_modes: tuple[int, ...]) -> int: # popcount with bin() function because it appears # to be the best way: https://stackoverflow.com/a/9831671 color_modes_list = list(color_modes) - color_modes_list.sort(key=lambda mode: bin(mode).count("1")) + color_modes_list.sort(key=lambda mode: (mode).bit_count()) return color_modes_list[0] diff --git a/homeassistant/components/refoss/sensor.py b/homeassistant/components/refoss/sensor.py index 9f5ee5d898a..f65724ddd77 100644 --- a/homeassistant/components/refoss/sensor.py +++ b/homeassistant/components/refoss/sensor.py @@ -91,7 +91,7 @@ SENSORS: dict[str, tuple[RefossSensorEntityDescription, ...]] = { native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_display_precision=2, subkey="mConsume", - fn=lambda x: x if x > 0 else 0, + fn=lambda x: max(0, x), ), RefossSensorEntityDescription( key="energy_returned", diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 940592d7b08..c02c3ce7b7a 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -109,7 +109,7 @@ def _time_weighted_average( for fstate, state in fstates: # The recorder will give us the last known state, which may be well # before the requested start time for the statistics - start_time = start if state.last_updated < start else state.last_updated + start_time = max(state.last_updated, start) if old_start_time is None: # Adjust start time, if there was no last known state start = start_time diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 9ab3f353dea..c21523baa38 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -2327,7 +2327,7 @@ def regex_match(value, find="", ignorecase=False): """Match value using regex.""" if not isinstance(value, str): value = str(value) - flags = re.I if ignorecase else 0 + flags = re.IGNORECASE if ignorecase else 0 return bool(_regex_cache(find, flags).match(value)) @@ -2338,7 +2338,7 @@ def regex_replace(value="", find="", replace="", ignorecase=False): """Replace using regex.""" if not isinstance(value, str): value = str(value) - flags = re.I if ignorecase else 0 + flags = re.IGNORECASE if ignorecase else 0 return _regex_cache(find, flags).sub(replace, value) @@ -2346,7 +2346,7 @@ def regex_search(value, find="", ignorecase=False): """Search using regex.""" if not isinstance(value, str): value = str(value) - flags = re.I if ignorecase else 0 + flags = re.IGNORECASE if ignorecase else 0 return bool(_regex_cache(find, flags).search(value)) @@ -2359,7 +2359,7 @@ def regex_findall(value, find="", ignorecase=False): """Find all matches using regex.""" if not isinstance(value, str): value = str(value) - flags = re.I if ignorecase else 0 + flags = re.IGNORECASE if ignorecase else 0 return _regex_cache(find, flags).findall(value) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index ab5c4037f9b..0745bc96dfb 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -244,7 +244,7 @@ def color_RGB_to_xy_brightness( y = Y / (X + Y + Z) # Brightness - Y = 1 if Y > 1 else Y + Y = min(Y, 1) brightness = round(Y * 255) # Check if the given xy value is within the color-reach of the lamp. diff --git a/pyproject.toml b/pyproject.toml index 7c79cf7b5a5..6eaa26e5793 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -725,6 +725,7 @@ select = [ "E", # pycodestyle "F", # pyflakes/autoflake "FLY", # flynt + "FURB", # refurb "G", # flake8-logging-format "I", # isort "INP", # flake8-no-pep420 diff --git a/script/licenses.py b/script/licenses.py index b560d709d33..54774bbe2f7 100644 --- a/script/licenses.py +++ b/script/licenses.py @@ -237,20 +237,20 @@ def main() -> int: f"{package.name}@{package.version}: {package.license}" ) print("Please remove the package from the TODO list.") - print("") + print() else: print( "We could not detect an OSI-approved license for " f"{package.name}@{package.version}: {package.license}" ) - print("") + print() exit_code = 1 elif not approved and package.name not in EXCEPTIONS: print( "We could not detect an OSI-approved license for" f"{package.name}@{package.version}: {package.license}" ) - print("") + print() exit_code = 1 elif approved and package.name in EXCEPTIONS: print( @@ -258,7 +258,7 @@ def main() -> int: f"{package.name}@{package.version}: {package.license}" ) print(f"Please remove the package from the EXCEPTIONS list: {package.name}") - print("") + print() exit_code = 1 current_packages = {package.name for package in package_definitions} for package in [*TODO.keys(), *EXCEPTIONS]: @@ -267,7 +267,7 @@ def main() -> int: f"Package {package} is tracked, but not used. Please remove from the licenses.py" "file." ) - print("") + print() exit_code = 1 return exit_code -- GitLab