Skip to content

[AAASM-4691] ✅ (python-sdk): Fix pytest.raises single-statement smells (S5778)#264

Merged
Chisanan232 merged 9 commits into
masterfrom
v0.1.0/AAASM-4691/fix/sonar_pytest_raises
Jul 16, 2026
Merged

[AAASM-4691] ✅ (python-sdk): Fix pytest.raises single-statement smells (S5778)#264
Chisanan232 merged 9 commits into
masterfrom
v0.1.0/AAASM-4691/fix/sonar_pytest_raises

Conversation

@Chisanan232

@Chisanan232 Chisanan232 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Description

Resolves all 28 SonarCloud python:S5778 code smells ("the pytest.raises /
assertRaises block should contain a single simple statement") across the
test/ suite. For each flagged block, the setup calls that produce arguments or
instances (constructors, uuid4(), MagicMock(), SimpleNamespace(...),
_ArgsModel(...), str(...)) were hoisted before the with pytest.raises(...)
block, leaving only the single call expected to raise inside it.

The one combined-context-manager case
(test_spawn_lineage_integration.py::test_exception_in_scope_still_resets_ctx,
which had both pytest.raises and spawn_context_scope(ctx) plus two body
statements) was refactored to move the scope + raise into a nested helper, so
the RuntimeError still propagates through spawn_context_scope.__exit__
(exercising the reset-on-exception path) while pytest.raises wraps a single
call. No assertions were weakened or removed.

Type of Change

  • ♻️ Refactoring (test-only; no production code touched)

Breaking Changes

  • No

Related Issues

  • Related JIRA ticket: AAASM-4691 (Epic AAASM-4686)

Testing

  • Unit tests added/updated (existing tests refactored, behavior unchanged)
  • Integration tests added/updated
  • Verified locally:
    • .venv/bin/python -m pytest test/800 passed, 15 skipped
    • ruff check + ruff format --check on all changed files → clean
    • pre-commit hooks (isort, autoflake, black, mypy) → pass on every commit

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • All tests passing

🤖 Generated with Claude Code

S5778: keep only the single raising call inside the with-block by
constructing the adapter (and its argument) beforehand.
S5778: hoist run_id=uuid4() and the recorder construction out of the
pytest.raises blocks so each wraps a single call.
S5778: extract the uuid4() call before each with pytest.raises so only
handler.on_tool_start remains inside.
S5778: construct tool/ctx/args before each pytest.raises block so only
the awaited _run/run call remains inside.
S5778: bind the tool instance before each pytest.raises so only the
awaited invoke() call remains inside.
S5778: bind the SimpleNamespace ctx / MagicMock args before each
pytest.raises so only the awaited invoke/run call remains inside.
S5778: bind the session before pytest.raises so only the awaited
call_tool() remains inside.
S5778: the combined pytest.raises/spawn_context_scope with-block held
two statements. Move the scope and raise into a nested helper so the
RuntimeError still propagates through spawn_context_scope.__exit__
(exercising the reset-on-exception path) while pytest.raises wraps a
single call.
S5778: bind the path before pytest.raises so only
load_adapter_class_from_path() remains inside.
@sonarqubecloud

Copy link
Copy Markdown

@Chisanan232

Copy link
Copy Markdown
Contributor Author

🤖 Claude Code review

  • CI: Green — 17 success + 4 skipped (the four e2e jobs skip by design). Unit, integration, CodeQL, SonarCloud, pip-audit, LangChain contract test all pass.
  • Scope: Fully covered. All 14 changed files are under test/; exactly 28 pytest.raises blocks refactored (S5778). Each hoists setup (object construction) before the block and keeps a single raising statement inside — same exception type still asserted, all match= regexes preserved, no post-block assertions dropped. Hoisted values are identical to what the raising call previously received.
  • Side-effects: None. No non-test files touched; no behavior change to shipped code. Duplication (4.5%) intentionally left unaddressed per ticket; coverage unaffected (no production code changed).
  • Special case: test_spawn_lineage_integration.py combined-context-manager refactor moves spawn_context_scope(ctx) into a nested helper — RuntimeError still propagates through __exit__, so the reset-on-exception path stays exercised (assert _SPAWN_CTX.get() is None after the block confirms it).
  • Verdict: ✅ Approve — clean, mechanical, test-intent-preserving refactor matching the intended scope.

Automated review by Claude Code.

@Chisanan232 Chisanan232 merged commit 4fd30ca into master Jul 16, 2026
21 checks passed
@Chisanan232 Chisanan232 deleted the v0.1.0/AAASM-4691/fix/sonar_pytest_raises branch July 16, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant