Skip to content

Commit 28a6edc

Browse files
authored
refactor: enable stricter ruff lints and broad exception checking (#956)
1 parent af8a4d1 commit 28a6edc

24 files changed

Lines changed: 79 additions & 50 deletions

pyproject.toml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,40 @@ allowed_tags = [
106106
major_tags= ["refactor"]
107107

108108
[tool.ruff]
109-
lint.ignore = ["F403", "E741"]
110-
lint.select=["E", "F", "UP", "I"]
109+
lint.ignore = []
110+
# TODO: Enable ASYNC (flake8-async) in a dedicated follow-up PR once async open() and timeout parameters are refactored.
111+
lint.select = [
112+
"A",
113+
"B",
114+
"BLE",
115+
"C4",
116+
"E",
117+
"F",
118+
"FA",
119+
"I",
120+
"ICN",
121+
"ISC",
122+
"PGH",
123+
"PIE",
124+
"UP",
125+
"W",
126+
"YTT",
127+
]
111128
line-length = 120
112129
extend-exclude = ["roborock/map/proto/*_pb2.py"]
113130

114131
[tool.ruff.lint.per-file-ignores]
115132
"*/__init__.py" = ["F401"]
133+
"roborock/__init__.py" = ["F403"]
134+
"roborock/data/__init__.py" = ["F403"]
135+
"roborock/data/b01_q7/__init__.py" = ["F403"]
136+
"roborock/data/b01_q10/__init__.py" = ["F403"]
137+
"roborock/data/dyad/__init__.py" = ["F403"]
138+
"roborock/data/mower/__init__.py" = ["F403"]
139+
"roborock/data/v1/__init__.py" = ["F403"]
140+
"roborock/data/zeo/__init__.py" = ["F403"]
116141
"roborock/map/proto/*_pb2.py" = ["E501", "I001", "UP009"]
142+
"tests/*" = ["B010"]
117143

118144
[[tool.mypy.overrides]]
119145
module = ["roborock.map.proto.*"]

roborock/broadcast_protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
from asyncio import BaseTransport, Lock
66

7-
from construct import ( # type: ignore
7+
from construct import ( # type: ignore[import-untyped]
88
Bytes,
99
Checksum,
1010
GreedyBytes,
@@ -63,7 +63,7 @@ def datagram_received(self, data: bytes, _):
6363
parsed_message = BroadcastMessage(duid=json_payload["duid"], ip=json_payload["ip"], version=version)
6464
_LOGGER.debug(f"Received broadcast: {parsed_message}")
6565
self.devices_found.append(parsed_message)
66-
except Exception as e:
66+
except Exception as e: # noqa: BLE001
6767
_LOGGER.warning(f"Failed to decode message: {data!r}. Error: {e}")
6868

6969
async def discover(self) -> list[BroadcastMessage]:

roborock/cli.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
```
2323
"""
2424

25+
# ruff: noqa: BLE001
26+
2527
import asyncio
2628
import datetime
2729
import functools
@@ -38,9 +40,9 @@
3840
import click
3941
import click_shell
4042
import yaml
41-
from pyshark import FileCapture # type: ignore
42-
from pyshark.capture.live_capture import LiveCapture, UnknownInterfaceException # type: ignore
43-
from pyshark.packet.packet import Packet # type: ignore
43+
from pyshark import FileCapture # type: ignore[import-untyped]
44+
from pyshark.capture.live_capture import LiveCapture, UnknownInterfaceException # type: ignore[import-untyped]
45+
from pyshark.packet.packet import Packet # type: ignore[import-untyped]
4446
except ImportError as err:
4547
raise SystemExit(
4648
f"The 'roborock' command line tool requires extra dependencies that are not installed ({err.name}).\n"
@@ -307,7 +309,7 @@ async def set(self, value: CacheData) -> None:
307309
@click.pass_context
308310
def cli(ctx, debug: int):
309311
logging_config: dict[str, Any] = {"level": logging.DEBUG if debug > 0 else logging.INFO}
310-
logging.basicConfig(**logging_config) # type: ignore
312+
logging.basicConfig(**logging_config) # type: ignore[call-overload]
311313
ctx.obj = RoborockContext()
312314

313315

@@ -988,26 +990,24 @@ def on_package(packet: Packet):
988990
local_key,
989991
)
990992
print(f"Received request: {f}")
991-
except BaseException as e:
993+
except Exception as e:
992994
print(e)
993-
pass
994995
elif packet.ip.src == device_ip:
995996
try:
996997
f, buffer["data"] = MessageParser.parse(
997998
buffer["data"] + bytes.fromhex(packet.DATA.data),
998999
local_key,
9991000
)
10001001
print(f"Received response: {f}")
1001-
except BaseException as e:
1002+
except Exception as e:
10021003
print(e)
1003-
pass
10041004

10051005
try:
10061006
await capture.packets_from_tshark(on_package, close_tshark=not file_provided)
1007-
except UnknownInterfaceException:
1007+
except UnknownInterfaceException as err:
10081008
raise RoborockException(
10091009
"You need to run 'rvictl -s XXXXXXXX-XXXXXXXXXXXXXXXX' first, with an iPhone connected to usb port"
1010-
)
1010+
) from err
10111011

10121012

10131013
def _parse_diagnostic_file(diagnostic_path: Path) -> dict[str, dict[str, Any]]:
@@ -1319,7 +1319,7 @@ def write_markdown_table(product_features: dict[str, dict[str, any]], all_featur
13191319
]
13201320
# Regular features are the remaining keys, sorted alphabetically
13211321
# We filter out the special rows to avoid duplicating them.
1322-
sorted_features = sorted(list(all_features - set(special_rows)))
1322+
sorted_features = sorted(all_features - set(special_rows))
13231323

13241324
header = ["Feature"] + sorted_products
13251325

roborock/data/code_mappings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _missing_(cls: type[Self], key) -> Self:
3333
if warning not in completed_warnings:
3434
completed_warnings.add(warning)
3535
_LOGGER.warning(warning)
36-
return cls.unknown # type: ignore
36+
return cls.unknown # type: ignore[attr-defined]
3737
default_value = next(item for item in cls)
3838
warning = f"Missing {cls.__name__} code: {key} - defaulting to {default_value}"
3939
if warning not in completed_warnings:

roborock/data/containers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _attr_repr(obj: Any) -> str:
5252
continue
5353
try:
5454
v = getattr(obj, k)
55-
except (RuntimeError, Exception):
55+
except Exception: # noqa: BLE001
5656
continue
5757
if callable(v):
5858
continue
@@ -210,7 +210,7 @@ class Reference(RoborockBase):
210210
r: str | None = None
211211
a: str | None = None
212212
m: str | None = None
213-
l: str | None = None
213+
l: str | None = None # noqa: E741
214214

215215

216216
@dataclass
@@ -379,9 +379,9 @@ class HomeDataSchedule(RoborockBase):
379379
class HomeData(RoborockBase):
380380
id: int
381381
name: str
382-
products: list[HomeDataProduct] = field(default_factory=lambda: [])
383-
devices: list[HomeDataDevice] = field(default_factory=lambda: [])
384-
received_devices: list[HomeDataDevice] = field(default_factory=lambda: [])
382+
products: list[HomeDataProduct] = field(default_factory=list)
383+
devices: list[HomeDataDevice] = field(default_factory=list)
384+
received_devices: list[HomeDataDevice] = field(default_factory=list)
385385
lon: Any | None = None
386386
lat: Any | None = None
387387
geo_name: Any | None = None

roborock/data/v1/v1_code_mappings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class RoborockDssCodes(RoborockEnum):
151151
def _missing_(cls: type[Self], key) -> Self:
152152
# If the calculated value is not provided, then it should be viewed as okay.
153153
# As the math will sometimes result in you getting numbers that don't matter.
154-
return cls.okay # type: ignore
154+
return cls.okay # type: ignore[attr-defined]
155155

156156

157157
class ClearWaterBoxStatus(RoborockDssCodes):

roborock/device_features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def from_feature_flags(
643643
elif (product_features := f.metadata.get("product_features")) is not None:
644644
if product_nickname is not None:
645645
available_features = PRODUCT_FEATURE_MAP.get(product_nickname, [])
646-
if any(feat in available_features for feat in product_features): # type: ignore
646+
if any(feat in available_features for feat in product_features):
647647
kwargs[f.name] = True
648648

649649
# The app combines runtime shake-mop, model shake/spin, and roller-mop

roborock/devices/rpc/b01_q7_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def on_message(response_message: RoborockMessage) -> None:
8484
return
8585
try:
8686
response = response_matcher(response_message)
87-
except Exception as ex:
87+
except Exception as ex: # noqa: BLE001
8888
future.set_exception(ex)
8989
return
9090
if response is not None:

roborock/devices/traits/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
]
2525

2626

27-
class Trait(ABC):
27+
class Trait(ABC): # noqa: B024
2828
"""Base class for all traits."""

roborock/devices/traits/v1/clean_summary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def convert(self, response: common.V1ResponseData) -> CleanRecord:
5656
rec.square_meter_area or 0
5757
)
5858
return final_record
59-
except Exception:
59+
except Exception: # noqa: BLE001
6060
# Return final record when an exception occurred
6161
return final_record
6262
# There are still a few unknown variables in this.

0 commit comments

Comments
 (0)