fix(types): P3 — annotate transport in all template protocol servers; 0 errors in templates/
- asyncio.Protocol (TCP): _transport: asyncio.Transport | None = None + cast() in connection_made; assert guards in every method that directly accesses the field. Files: pop3, smtp, mqtt, postgres, mssql, mongodb, imap, ldap, redis, mysql, sip, vnc. - asyncio.DatagramProtocol (UDP): _transport: asyncio.DatagramTransport | None = None. Files: snmp, tftp, SIPUDPProtocol. - RDP: assert new_transport is not None after start_tls() to narrow Transport | None. - FTP (Twisted): assert self.transport is not None + targeted type: ignore for imprecise Twisted stubs (misc/override/arg-type/attr-defined), IReactorTCP cast for listenTCP. - conpot: proc.stdout is None guard before iteration. - Bonus fixes surfaced by annotation: - smtp: get_payload(decode=True) bytes narrowing (arg-type on sha256) - postgres: rename shadowed `msg` param to `err_msg` in _handle_startup - mongodb: base64.binascii.Error → import binascii; binascii.Error - imap: result: list[int] = [] (var-annotated)
This commit is contained in:
@@ -9,6 +9,7 @@ Logs all requests as JSON.
|
||||
import asyncio
|
||||
import os
|
||||
import struct
|
||||
from typing import cast
|
||||
from syslog_bridge import (
|
||||
encode_secret,
|
||||
forward_syslog,
|
||||
@@ -225,11 +226,13 @@ def _build_response(version: int, community: str, request_id: int, oids: list) -
|
||||
|
||||
|
||||
class SNMPProtocol(asyncio.DatagramProtocol):
|
||||
_transport: asyncio.DatagramTransport | None = None
|
||||
|
||||
def __init__(self):
|
||||
self._transport = None
|
||||
|
||||
def connection_made(self, transport):
|
||||
self._transport = transport
|
||||
def connection_made(self, transport: asyncio.BaseTransport) -> None:
|
||||
self._transport = cast(asyncio.DatagramTransport, transport)
|
||||
|
||||
def datagram_received(self, data, addr):
|
||||
try:
|
||||
@@ -244,7 +247,8 @@ class SNMPProtocol(asyncio.DatagramProtocol):
|
||||
principal=None, secret_kind="snmp_community",
|
||||
**encode_secret(community))
|
||||
response = _build_response(version, community, request_id, oids)
|
||||
self._transport.sendto(response, addr)
|
||||
if self._transport is not None:
|
||||
self._transport.sendto(response, addr)
|
||||
except Exception as e:
|
||||
_log("parse_error", severity=4, src=addr[0], error=str(e), data=data[:64].hex())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user