8000 Add flake8-logging (LOG) and flake8-logging-format (G) for ruff (#1104) · msz-coder/python-kasa@3e43781 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e43781

Browse files
authored
Add flake8-logging (LOG) and flake8-logging-format (G) for ruff (python-kasa#1104)
Enables rules LOG (flake8-logging) and G (flake8-logging-format) for ruff. This will catch eager log message formatting, among other similar issues.
1 parent 2706e9a commit 3e43781

File tree

11 files changed

+33
-26
lines changed

11 files changed

+33
-26
lines changed

kasa/aestransport.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ async def _generate_key_pair_payload(self) -> AsyncGenerator:
278278
+ "\n-----END PUBLIC KEY-----\n"
279279
)
280280
handshake_params = {"key": pub_key}
281-
_LOGGER.debug(f"Handshake params: {handshake_params}")
282281
request_body = {"method": "handshake", "params": handshake_params}
283-
_LOGGER.debug(f"Request {request_body}")
282+
_LOGGER.debug("Handshake request: %s", request_body)
284283
yield json_dumps(request_body).encode()
285284

286285
async def perform_handshake(self) -> None:

kasa/device_factory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ def _perf_log(has_params, perf_type):
8484
if debug_enabled:
8585
end_time = time.perf_counter()
8686
_LOGGER.debug(
87-
f"Device {config.host} with connection params {has_params} "
88-
+ f"took {end_time - start_time:.2f} seconds to {perf_type}",
87+
"Device %s with connection params %s took %.2f seconds to %s",
88+
config.host,
89+
has_params,
90+
end_time - start_time,
91+
perf_type,
8992
)
9093
start_time = time.perf_counter()
9194

kasa/discover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def datagram_received(self, data, addr) -> None:
262262
self._handle_discovered_event()
263263
return
264264
except KasaException as ex:
265-
_LOGGER.debug(f"[DISCOVERY] Unable to find device type for {ip}: {ex}")
265+
_LOGGER.debug("[DISCOVERY] Unable to find device type for %s: %s", ip, ex)
266266
self.invalid_device_exceptions[ip] = ex
267267
self._handle_discovered_event()
268268
return

kasa/emeterstatus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ def __getitem__(self, item):
8787
):
8888
return value / 1000
8989

90-
_LOGGER.debug(f"Unable to find value for '{item}'")
90+
_LOGGER.debug("Unable to find value for '%s'", item)
9191
return None

kasa/iot/iotdevice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def modules(self) -> ModuleMapping[IotModule]:
198198
def add_module(self, name: str | ModuleName[Module], module: IotModule):
199199
"""Register a module."""
200200
if name in self._modules:
201-
_LOGGER.debug("Module %s already registered, ignoring..." % name)
201+
_LOGGER.debug("Module %s already registered, ignoring...", name)
202202
return
203203

204204
_LOGGER.debug("Adding module %s", module)

kasa/klaptransport.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
153153

154154
if _LOGGER.isEnabledFor(logging.DEBUG):
155155
_LOGGER.debug(
156-
"Handshake1 posted at %s. Host is %s, Response"
157-
+ "status is %s, Request was %s",
156+
"Handshake1 posted at %s. Host is %s, "
157+
"Response status is %s, Request was %s",
158158
datetime.datetime.now(),
159159
self._host,
160160
response_status,
@@ -179,7 +179,7 @@ async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
179179
if _LOGGER.isEnabledFor(logging.DEBUG):
180180
_LOGGER.debug(
181181
"Handshake1 success at %s. Host is %s, "
182-
+ "Server remote_seed is: %s, server hash is: %s",
182+
"Server remote_seed is: %s, server hash is: %s",
183183
datetime.datetime.now(),
184184
self._host,
185185
remote_seed.hex(),
@@ -211,9 +211,10 @@ async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
211211

212212
if default_credentials_seed_auth_hash == server_hash:
213213
_LOGGER.debug(
214-
"Server response doesn't match our expected hash on ip %s"
215-
+ f" but an authentication with {key} default credentials matched",
214+
"Server response doesn't match our expected hash on ip %s, "
215+
"but an authentication with %s default credentials matched",
216216
self._host,
217+
key,
217218
)
218219
return local_seed, remote_seed, self._default_credentials_auth_hash[key] # type: ignore
219220

@@ -231,8 +232,8 @@ async def perform_handshake1(self) -> tuple[bytes, bytes, bytes]:
231232

232233
if blank_seed_auth_hash == server_hash:
233234
_LOGGER.debug(
234-
"Server response doesn't match our expected hash on ip %s"
235-
+ " but an authentication with blank credentials matched",
235+
"Server response doesn't match our expected hash on ip %s, "
236+
"but an authentication with blank credentials matched",
236237
self._host,
237238
)
238239
return local_seed, remote_seed, self._blank_auth_hash # type: ignore
@@ -260,8 +261,8 @@ async def perform_handshake2(
260261

261262
if _LOGGER.isEnabledFor(logging.DEBUG):
262263
_LOGGER.debug(
263-
"Handshake2 posted %s. Host is %s, Response status is %s, "
264-
+ "Request was %s",
264+
"Handshake2 posted %s. Host is %s, "
265+
"Response status is %s, Request was %s",
265266
datetime.datetime.now(),
266267
self._host,
267268
response_status,
@@ -338,18 +339,17 @@ async def send(self, request: str):
338339
+ f"Response status is {response_status}, Request was {request}"
339340
)
340341
if response_status != 200:
341-
_LOGGER.error("Query failed after successful authentication " + msg)
342+
_LOGGER.error("Query failed after successful authentication: %s", msg)
342343
# If we failed with a security error, force a new handshake next time.
343344
if response_status == 403:
344345
self._handshake_done = False
345346
raise _RetryableError(
346-
f"Got a security error from {self._host} after handshake "
347-
+ "completed"
347+
"Got a security error from %s after handshake completed", self._host
348348
)
349349
else:
350350
raise KasaException(
351-
f"Device {self._host} responded with {response_status} to"
352-
+ f"request with seq {seq}"
351+
f"Device {self._host} responded with {response_status} to "
352+
f"request with seq {seq}"
353353
)
354354
else:
355355
_LOGGER.debug("Device %s query posted %s", self._host, msg)

kasa/smart/modules/firmware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ async def update(
181181
)
182182
continue
183183

184-
_LOGGER.debug("Update state: %s" % state)
184+
_LOGGER.debug("Update state: %s", state)
185185
if progress_cb is not None:
186186
asyncio.create_task(progress_cb(state))
187187

kasa/smart/smartmodule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, device: SmartDevice, module: str):
7777

7878
def __init_subclass__(cls, **kwargs):
7979
name = getattr(cls, "NAME", cls.__name__)
80-
_LOGGER.debug("Registering %s" % cls)
80+
_LOGGER.debug("Registering %s", cls)
8181
cls.REGISTERED_MODULES[name] = cls
8282

8383
def _set_error(self, err: Exception | None):

kasa/smartprotocol.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ async def _handle_response_lists(
309309
# In case the device returns empty lists avoid infinite looping
310310
if not next_batch[response_list_name]:
311311
_LOGGER.error(
312-
f"Device {self._host} returned empty "
313-
+ f"results list for method {method}"
312+
"Device %s returned empty results list for method %s",
313+
self._host,
314+
method,
314315
)
315316
break
316317
response_result[response_list_name].extend(next_batch[response_list_name])

kasa/tests/fakeprotocol_iot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ def get_response_for_command(cmd):
425425
return error(msg=f"command {cmd} not found")
426426

427427
params = request[target][cmd]
428-
_LOGGER.debug(f"Going to execute {target}.{cmd} (params: {params}).. ")
428+
_LOGGER.debug(
429+
"Going to execute %s.%s (params: %s).. ", target, cmd, params
430+
)
429431

430432
if callable(proto[target][cmd]):
431433
res = proto[target][cmd](self, params, child_ids)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ select = [
117117
"FA", # flake8-future-annotations
118118
"I", # isort
119119
"S", # bandit
120+
"LOG", # flake8-logging
121+
"G", # flake8-logging-format
120122
]
121123
ignore = [
122124
"D105", # Missing docstring in magic method

0 commit comments

Comments
 (0)
0