Add Ruff refurb rules (#121701)

This commit is contained in:
Sid 2024-07-10 21:47:40 +02:00 committed by GitHub
parent a9c9963f0f
commit 3142f52a79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 16 additions and 15 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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",

View File

@ -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

View File

@ -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)

View File

@ -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.

View File

@ -725,6 +725,7 @@ select = [
"E", # pycodestyle
"F", # pyflakes/autoflake
"FLY", # flynt
"FURB", # refurb
"G", # flake8-logging-format
"I", # isort
"INP", # flake8-no-pep420

View File

@ -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