Skip to content

Commit cad0103

Browse files
garyatwalAIclaude
andcommitted
fix(tracing): resolve Ruff lint and format errors in governance enrichment
Remove unused imports (trace, SecurityThreatTypes, call, patch), move logger assignment below all imports to fix E402, and apply Ruff format. Signed-off-by: Gary Atwal <garyatwalai@users.noreply.github.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 83672ee commit cad0103

File tree

3 files changed

+55
-161
lines changed

3 files changed

+55
-161
lines changed

examples/governance_tracing/governance_tracing_example.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ async def run_interaction(scenario: dict) -> None:
132132
) as root_span:
133133
# --- Run guardrails (NeMo Guardrails creates its own child spans) ---
134134
try:
135-
response = await rails.generate_async(
136-
messages=[{"role": "user", "content": scenario["message"]}]
137-
)
138-
bot_reply = (
139-
response if isinstance(response, str) else response.get("content", "")
140-
)
135+
response = await rails.generate_async(messages=[{"role": "user", "content": scenario["message"]}])
136+
bot_reply = response if isinstance(response, str) else response.get("content", "")
141137
print(f"Bot reply: {bot_reply[:120]}")
142138
except Exception as exc:
143139
bot_reply = ""

nemoguardrails/tracing/governance_enricher.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@
6868
import time
6969
from typing import List, Optional, Sequence
7070

71-
logger = logging.getLogger(__name__)
72-
7371
try:
74-
from opentelemetry import trace # type: ignore
7572
from opentelemetry.trace import Span # type: ignore
7673
except ImportError:
7774
raise ImportError(
@@ -92,9 +89,10 @@
9289
RiskAttributes,
9390
SecurityAttributes,
9491
SecuritySeverity,
95-
SecurityThreatTypes,
9692
)
9793

94+
logger = logging.getLogger(__name__)
95+
9896
# Map severity strings to a numeric weight for comparison
9997
_SEVERITY_ORDER = {
10098
SecuritySeverity.LOW: 1,
@@ -257,9 +255,7 @@ def record_security_threat(
257255
if detection_method is not None:
258256
span.set_attribute(SecurityAttributes.DETECTION_METHOD, detection_method)
259257
if detection_confidence is not None:
260-
span.set_attribute(
261-
SecurityAttributes.DETECTION_CONFIDENCE, detection_confidence
262-
)
258+
span.set_attribute(SecurityAttributes.DETECTION_CONFIDENCE, detection_confidence)
263259

264260
# Detailed event for the trace waterfall
265261
event_attrs: dict = {
@@ -419,9 +415,7 @@ def finalise_compliance_summary(
419415
span.set_attribute(ComplianceAttributes.RAILS_FAILED, rails_failed)
420416

421417
if failed_rail_names:
422-
span.set_attribute(
423-
ComplianceAttributes.FAILED_RAIL_NAMES, ",".join(failed_rail_names)
424-
)
418+
span.set_attribute(ComplianceAttributes.FAILED_RAIL_NAMES, ",".join(failed_rail_names))
425419

426420
# Risk attributes derived from accumulated state
427421
risk_score = _compute_risk_score(
@@ -433,9 +427,7 @@ def finalise_compliance_summary(
433427
span.set_attribute(RiskAttributes.VIOLATION_COUNT, self._violation_count)
434428

435429
if self._highest_severity is not None:
436-
span.set_attribute(
437-
RiskAttributes.VIOLATION_SEVERITY, self._highest_severity
438-
)
430+
span.set_attribute(RiskAttributes.VIOLATION_SEVERITY, self._highest_severity)
439431

440432

441433
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)