Skip to content

test(pydantic_ai): Remove usage of internal functions from tests - #7622

Closed
DawnofGenX wants to merge 1 commit into
getsentry:masterfrom
DawnofGenX:fix/issue-5637-pydantic-ai-public-api
Closed

DawnofGenX wants to merge 1 commit into
getsentry:masterfrom
DawnofGenX:fix/issue-5637-pydantic-ai-public-api

Conversation

@DawnofGenX

Copy link
Copy Markdown

Summary

Refactors tests/integrations/pydantic_ai/test_pydantic_ai.py so it no longer imports or calls internal (_-prefixed) SDK functions, per CONTRIBUTING ("Don't call private functions") and the review note in #5629. Fixes #5637.

What changed (test-only; no sentry_sdk/ changes):

  • Removed imports/calls of _set_input_messages, _set_usage_data, _set_output_data, _set_available_tools, _get_model_name from the test module. Tests that exercised those helpers now drive the public integration surface: ai_client_span() / update_ai_client_span() (with real pydantic_ai message/part objects — UserPromptPart, SystemPromptPart, ModelResponse, RequestUsage — instead of MagicMock messages), and read back the recorded data from the emitted chat span.
  • Several of these tests were previously vacuous (_set_input_messages(span, msgs) + assert transaction is not None — no assertion on captured data). They now assert the actual span attributes (gen_ai.request.messages, gen_ai.system.instructions, gen_ai.request.model, gen_ai.usage.*, gen_ai.request.available_tools, absence of messages when include_prompts=False/PII off), so coverage is strictly increased.
  • Agent-context tests: sentry_init-scoped assertions now use the integration's public helpers get_current_agent()/push_agent()/pop_agent() from sentry_sdk.integrations.pydantic_ai.utils instead of poking scope._contexts["pydantic_ai_agent"]. Note the SDK no longer stores agent state in Scope._contexts at all (it is a private contextvar now), so every one of those _contexts assertions was vacuously true; they now verify the real mechanism. The two tests that injected fake streaming/agent state into scope._contexts were rewritten to exercise ai_client_span() through push_agent()/streaming runs instead of injecting into a private dict.
  • BLOB_DATA_SUBSTITUTE is now asserted as the literal wire value ("[Blob substitute]") with a comment, instead of importing the internal constant from sentry_sdk._types (removes the last from sentry_sdk._types import ... in the file).
  • Renamed two tests whose names referenced internal functions (test_get_model_name_*test_model_name_*, test_set_usage_data_with_cache_tokenstest_usage_data_with_cache_tokens).

Test plan

Full integration suite green on both supported pydantic-ai pins after refactor:

$ TESTPATH=tests/integrations/pydantic_ai uv run tox -e py3.14-pydantic_ai-latest
====================== 331 passed, 69 warnings in 12.57s =======================
  py3.14-pydantic_ai-latest: OK

$ TESTPATH=tests/integrations/pydantic_ai uv run tox -e py3.10-pydantic_ai-v1.0.18
====================== 331 passed, 68 warnings in 35.07s =======================
  py3.10-pydantic_ai-v1.0.18: OK

No private-underscore usages remain (grep is empty; exit 1 = no match):

$ grep -nE "_set_|_should_send_prompts|_get_model_name|_get_context|_get_tool_def|_contexts|from sentry_sdk\._|import _" tests/integrations/pydantic_ai/test_pydantic_ai.py
GREP_EXIT=1

Lint:

$ uv run ruff check tests
All checks passed!
$ uv run ruff format tests
<file> reformatted (only)

Fixes #5637

@DawnofGenX
DawnofGenX requested a review from a team as a code owner September 22, 2026 20:46
@alexander-alderman-webb

Copy link
Copy Markdown
Contributor

Going to close this because we don't have capacity to review the 5 PRs you've opened.
If you'd like to contribute dedicate your time towards making one good PR. Thanks.

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.

Refactor test_pydantic_ai to remove usage of internal functions within tests

2 participants