Selaa lähdekoodia

Apply the locals rule: inline single-use aliases and no-op guards

Six sites where a local only re-aliased an expression: the respond-rooms
value in _refresh_rooms, the attachments guard list in
_dispatch_message_posted, the handoff seed text, chat_id in
_event_room_and_message_id, the urlsplit intermediate in
_materialise_image - plus the dead 'or ""' guards on MessageAttachment
fields that parse() already guarantees as str. Locals that narrow,
transform or are reused stay.
Paul Klumpp 1 viikko sitten
vanhempi
commit
d7579865f0
1 muutettua tiedostoa jossa 14 lisäystä ja 12 poistoa
  1. 14 12
      adapter.py

+ 14 - 12
adapter.py

@@ -852,8 +852,8 @@ class ChattoAdapter(BasePlatformAdapter):
 
         for att in attachments:
             url = att.asset_url.url if att.asset_url else ""
-            filename = att.filename or ""
-            content_type = att.content_type or ""
+            filename = att.filename
+            content_type = att.content_type
             if not url:
                 # Videos are announced before transcoding finishes, so the
                 # signed URL can legitimately be missing on arrival.
@@ -941,8 +941,7 @@ class ChattoAdapter(BasePlatformAdapter):
 
         # A message carrying only an image/PDF has an empty body — dropping it
         # here is what made attachments sent to Hermes disappear silently.
-        attachments = list(message.attachments or [])
-        if not message_body and not attachments:
+        if not message_body and not message.attachments:
             return
 
         event = await self._admit_and_build(
@@ -1479,7 +1478,6 @@ class ChattoAdapter(BasePlatformAdapter):
 
         try:
             rooms_list = await client.list_rooms()
-            respond_rooms = self.chatto_config.respond_rooms.value
             member_ids: set[str] = set()
             new_room_ids: list[str] = []
 
@@ -1517,7 +1515,11 @@ class ChattoAdapter(BasePlatformAdapter):
                     stale_room_ids,
                 )
 
-            unjoined_listed = [rid for rid in respond_rooms if rid not in member_ids]
+            unjoined_listed = [
+                rid
+                for rid in self.chatto_config.respond_rooms.value
+                if rid not in member_ids
+            ]
             if unjoined_listed:
                 logger.warning(
                     "Chatto WS: CHATTO_RESPOND_ROOMS lists room(s) we are not a"
@@ -1850,9 +1852,11 @@ class ChattoAdapter(BasePlatformAdapter):
             logger.warning("Chatto: handoff thread — client unavailable")
             return None
 
-        seed_text = f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**"
         try:
-            msg = await client.post_message(room_id=str(parent_chat_id), body=seed_text)
+            msg = await client.post_message(
+                room_id=str(parent_chat_id),
+                body=f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**",
+            )
         except Exception as e:
             logger.warning(
                 "Chatto: handoff thread seed-post failed for room %s: %s",
@@ -2081,8 +2085,7 @@ class ChattoAdapter(BasePlatformAdapter):
     def _event_room_and_message_id(self, event: MessageEvent) -> tuple[str, str]:
         """Extract room_id and message_id from a MessageEvent."""
         message_id = event.message_id or ""
-        chat_id = event.source.chat_id
-        return chat_id, message_id
+        return event.source.chat_id, message_id
 
     async def on_processing_start(self, event: MessageEvent) -> None:
         """Record the turn as open, then add an 👀 (eyes) reaction.
@@ -2458,8 +2461,7 @@ class ChattoAdapter(BasePlatformAdapter):
         when ``is_temp``.          ``(None, False)`` means the entry is unusable.
         """
         if image_url.startswith(("http://", "https://")):
-            parsed = urlsplit(image_url)
-            ext = os.path.splitext(parsed.path)[1] or ".png"
+            ext = os.path.splitext(urlsplit(image_url).path)[1] or ".png"
             tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
             os.close(tmp_fd)
             try: