[AAASM-3347] ♻️ (python-sdk): Reduce SonarCloud code smells under 20#144
Conversation
The pre-commit mypy hook was red on master: pydantic models with Field(default=...) defaults reported spurious 'Missing named argument' call-arg errors because the pydantic mypy plugin was not enabled. Enable it so defaulted fields are correctly treated as optional at construction. refs AAASM-3347
The pre-commit mypy hook flagged runtime.socket / runtime.subprocess as non-exported attributes and a Popen-vs-str comparison-overlap. Patch via string targets (mypy does not check string monkeypatch paths) and widen the handle annotation to object. No behaviour change; tests still pass. refs AAASM-3347
SonarCloud S5799 flagged two adjacent f-string literals in the blocked message; merge each into one. S112 flagged the generic Exception return of _build_blocked_error; narrow it to MCPToolBlockedError. No behaviour change. refs AAASM-3347
SonarCloud S5799 flagged three adjacent f-string literals in adapter validation messages; merge each into one. S108 flagged the empty `if TYPE_CHECKING: pass` block (no longer guarding any import); remove it and the now-unused TYPE_CHECKING import. No behaviour change. refs AAASM-3347
SonarCloud S112 flagged four `raise` sites whose builders returned the generic Exception type. Narrow _build_denied_error / _build_pending_rejected_error return annotations to PolicyViolationError (added under TYPE_CHECKING). No behaviour change. refs AAASM-3347
SonarCloud S112 flagged two `raise` sites whose builders returned the generic Exception type. Narrow _build_denied_error / _build_pending_rejected_error return annotations to PolicyViolationError (added under TYPE_CHECKING). No behaviour change. refs AAASM-3347
…2/S7504) SonarCloud S1172 flagged the unused node_name parameter on _wrap_tool_node_subgraphs and _make_subgraph_spawn_wrapper; remove it and update all call sites (incl. tests). S7504 flagged the unnecessary list() around tools_by_name.items() — the loop mutates tool.func, not the dict, so iterate directly. No behaviour change. refs AAASM-3347
SonarCloud S3776 flagged _normalize_decision at complexity 18 (limit 15). Collapse the three per-status if-chains in both the str and Mapping branches into a single _coerce_known_status lookup against a status set. Behaviour is identical; unknown verdicts still fail closed under enforce. refs AAASM-3347
SonarCloud S3776 flagged _normalize_decision at complexity 18 (limit 15). Collapse the three per-status if-chains in both the str and Mapping branches into a single _coerce_known_status lookup against a status set. Behaviour is identical; unknown verdicts still fail closed under enforce. refs AAASM-3347
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
🤖 Claude Code — PR ReviewDisclosure: Authored by a sub-agent I orchestrated under the AAASM-3347 fan-out (Epic AAASM-3345). Reviewing critically. 1. CI verdict14 green / 6 skipped / 1 failure — 2. Scope vs AAASM-3347 — met
19 fixed across 6 rules (S5799 string-concat, S112 exception-narrowing, S1172 unused-params, S108 empty-block, S7504 redundant-list, S3776 complexity) — behaviour-preserving. 3. Quality observations
4. Verdict🟢 APPROVED. Smells 22→3 (target met), residuals are legitimate FP/breaking-to-fix, the prep commits are a justified gate-unblock (not Generated by Claude Code (Opus 4.8, 1M ctx) · scope: CI triage (gate=duplication, acceptance) + AAASM-3347 verification + prep-commit scrutiny + FP-justification review |


Description
Reduces SonarCloud code smells for the
AI-agent-assembly_python-sdkproject from 22 to a projected 3 (target was < 20). All fixes are behaviour-preserving; the full test suite (572 passed) andmypy agent_assemblyremain green.Per-rule fixes:
cli/adapter_validator.py(3) andadapters/mcp/patch.py(2).-> Exceptionreturn annotation on the_build_*_errorhelpers to the concreteMCPToolBlockedError/PolicyViolationErrorthey return —mcp(1),pydantic_ai(4 call sites via 2 builders),google_adk(2 call sites via 2 builders).node_nameparameter from_wrap_tool_node_subgraphsand_make_subgraph_spawn_wrapperinadapters/langgraph/patch.pyand updated all call sites + tests.if TYPE_CHECKING: passblock (and unused import) incli/adapter_validator.py.list(), x1 of 2): dropped the redundantlist()aroundtools_by_name.items()(the loop mutatestool.func, not the dict).ifchains in both_normalize_decisionfunctions (crewai,langchain) into a single status-set lookup helper.Not fixed (flagged for operator review):
client/gateway.pylines 99/133/196):register_agent,check_policy_compliance,dispatch_toolareasyncbut use noawait. Removingasyncis a breaking public-API change (every caller and test doesawait client.<method>()). Left as-is — recommend Won't Fix or a separate, deliberate API change.langgraph/patch.pyline 390):list(items_method())is a false positive — the loop mutatesnode_map[node_name]during iteration, so the defensive copy is required. Recommend Won't Fix.Two preparatory commits unblock the pre-commit
mypyhook, which was red onmasterfor reasons unrelated to this change (CI does not run mypy as a merge gate): enabling the pydantic mypy plugin (fixes spurious modelcall-argerrors) and makingtest_runtime.pymonkeypatch targets mypy-clean. No--no-verify/ no hook bypass was used.Type of Change
Breaking Changes
Related Issues
Testing
pytest test/→ 572 passed, 13 env-skipped;mypy→ clean; pre-commit hooks green)Checklist
🤖 Generated with Claude Code