From 2d6b9fa464558be143ee29f60bb58e88c28d5cb6 Mon Sep 17 00:00:00 2001 From: docker24017 <350791397+docker24017@users.noreply.github.com> Date: Wed, 4 Nov 2026 22:16:31 +0000 Subject: [PATCH] =?UTF-8?q?fix(#37):=20@atraced=20sur=20les=20neuf=20corou?= =?UTF-8?q?tines=20d=C3=A9cor=C3=A9es=20par=20@traced=20+=20test=20AST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pystashtemplate/indexer.py | 10 +++++----- src/pystashtemplate/outputs/syslog.py | 6 +++--- src/pystashtemplate/service.py | 4 ++-- tests/test_tracing.py | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/pystashtemplate/indexer.py b/src/pystashtemplate/indexer.py index 72a189d..fd4514b 100644 --- a/src/pystashtemplate/indexer.py +++ b/src/pystashtemplate/indexer.py @@ -35,7 +35,7 @@ from pystashtemplate.outputs.elasticsearch_bulk import SendOutcome as _SortieOutcome from pystashtemplate.outputs.opensearch import OpenSearchOutput from pystashtemplate.templates import CompiledTemplate, compile_template, render -from pystashtemplate.tracing import traced +from pystashtemplate.tracing import atraced, traced OPENSEARCH_TIMEOUT = 3.0 # secondes, timeout socket du client OpenSearch @@ -190,7 +190,7 @@ def __init__( options["ssl_key"] = client_key self.sortie = OpenSearchOutput(options, dlq=dlq) - @traced + @atraced async def health(self) -> dict[str, Any]: """Interroge le cluster (GET /) : sa version, ou lève l'erreur. @@ -229,7 +229,7 @@ def _document_id(self, doc: dict) -> str | None: rendered = render(self._document_id_template, doc, on_missing="empty") return rendered or None - @traced + @atraced async def _client(self): """Client httpx paresseux de la sortie (couture : template d'index). @@ -239,7 +239,7 @@ async def _client(self): """ return self.sortie._client_http() - @traced + @atraced async def send( self, doc: dict, @@ -308,7 +308,7 @@ async def send( return SendOutcome.INDEXED return SendOutcome.RETRYABLE - @traced + @atraced async def close(self) -> None: """Ferme proprement la sortie (et son client HTTP).""" await self.sortie.close() diff --git a/src/pystashtemplate/outputs/syslog.py b/src/pystashtemplate/outputs/syslog.py index f94db41..81ff841 100644 --- a/src/pystashtemplate/outputs/syslog.py +++ b/src/pystashtemplate/outputs/syslog.py @@ -491,7 +491,7 @@ def __init__(self, hote: str, port: int, protocole: str, framing: str, contexte_ self._udp: Any = None logger.debug("_TransportReseau.__init__ | sortie") - @traced + @atraced async def ouvrir_udp(self) -> Any: """Ouvre un socket UDP vers la destination.""" transport, _ = await asyncio.get_event_loop().create_datagram_endpoint( @@ -500,7 +500,7 @@ async def ouvrir_udp(self) -> Any: ) return transport - @traced + @atraced async def ecrire(self, trames: list[bytes]) -> None: """Écrit les trames : datagrammes UDP, ou flux TCP reconnecté.""" if self._protocole == "udp": @@ -521,7 +521,7 @@ async def ecrire(self, trames: list[bytes]) -> None: self._writer.write(trame) await self._writer.drain() - @traced + @atraced async def fermer(self) -> None: """Ferme la connexion TCP ouverte.""" if self._writer is not None: diff --git a/src/pystashtemplate/service.py b/src/pystashtemplate/service.py index f216fb6..f95eb03 100644 --- a/src/pystashtemplate/service.py +++ b/src/pystashtemplate/service.py @@ -149,7 +149,7 @@ def analyser_syslog(trame: bytes) -> dict[str, Any] | None: return None -@traced +@atraced async def _lire_trame(lecteur: asyncio.StreamReader) -> bytes | None: """Lit UNE trame syslog TCP (RFC 6587) : octet-counting ou LF. @@ -181,7 +181,7 @@ async def _lire_trame(lecteur: asyncio.StreamReader) -> bytes | None: tampon += octet -@traced +@atraced async def _lire_requete(lecteur: asyncio.StreamReader) -> tuple[list[bytes] | None, bytes]: """Lit UNE requête HTTP POST ; rend les documents du corps. diff --git a/tests/test_tracing.py b/tests/test_tracing.py index 67c2b7c..d053755 100644 --- a/tests/test_tracing.py +++ b/tests/test_tracing.py @@ -95,3 +95,29 @@ async def secret(mot_de_passe: str) -> str: finally: logger.remove(sink) assert not any("s3cret" in m or "S3CRET" in m for m in messages) + + +class TestCoroutinesDecorees: + """Aucune coroutine du paquet n'est décorée par `@traced` (issue #37). + + `@traced` sur un `async def` journalise la « sortie » dès la création de + la coroutine, avant le premier `await` : la trace ment. Analyse AST de + `src/`, sans import. + """ + + def test_aucun_traced_sur_async_def(self) -> None: + import ast + from pathlib import Path + + racine = Path(__file__).resolve().parent.parent / "src" + fautifs: list[str] = [] + for fichier in sorted(racine.rglob("*.py")): + arbre = ast.parse(fichier.read_text(encoding="utf-8")) + for noeud in ast.walk(arbre): + if not isinstance(noeud, ast.AsyncFunctionDef): + continue + for decorateur in noeud.decorator_list: + nom = decorateur.id if isinstance(decorateur, ast.Name) else getattr(decorateur, "attr", None) + if nom == "traced": + fautifs.append(f"{fichier.relative_to(racine)}:{noeud.lineno} {noeud.name}") + assert fautifs == [], f"@traced sur des coroutines (utiliser @atraced) : {fautifs}"