Skip to content

Skip f-string inspection for spans and logs that logfire opens itself - #2775

Merged
alexmojaki merged 2 commits into
pydantic:mainfrom
ska2704:skip-fstring-magic-inside-logfire
Nov 10, 2026
Merged

alexmojaki merged 2 commits into
pydantic:mainfrom
ska2704:skip-fstring-magic-inside-logfire

Conversation

@ska2704

@ska2704 ska2704 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2771

With inspect_arguments=True, Logfire._span and Logfire.log hand the caller's frame to logfire_format_with_magic, which runs executing on the call site to see whether the message is an f-string. When the caller is logfire itself (Variable.get(), integrations), that parses the logfire module's source on the first call, even though the message is never a user f-string.

Following the suggestion in the issue, logfire_format_with_magic now drops the frame when the call site is inside the logfire package, the same as inspect_arguments=False. Doing it there covers _span and log and every internal caller at once. For the nice-to-have: when PYTEST_CURRENT_TEST is set (the same signal log_internal_error uses), the magic still runs for these calls and asserts the template is unchanged, so an internal call with an f-string fails the tests instead of silently producing a span name per value.

With the reproduction from the issue, the first get() went from 9.3 ms to 1.4 ms (macOS arm64, Python 3.13), and executing no longer parses variables/variable.py.

test_logfire_calls_skip_fstring_magic fails on main because variable.py gets inspected. test_logfire_fstring_call_fails_in_tests covers the test-time check.

View guided diff

Spans and logs opened by logfire itself, such as Variable.get(), never pass a
user f-string, but with inspect_arguments enabled the call site still went
through executing, which parses the calling module on the first call.

In tests, the magic still runs for these calls and asserts the template is
unchanged, so an internal f-string call fails the suite.

Closes pydantic#2771
@veria-ai

veria-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal
  formatter.py
Project Total  

This report was generated by python-coverage-comment-action

An error while re-evaluating an internal f-string's expression was caught
along with everything else, so the check passed without reaching the
assertion. KnownFormattingError only comes from formatting a plain template,
so catch just that and let anything else fail the test.
@alexmojaki
alexmojaki added this pull request to the merge queue Nov 10, 2026
Merged via the queue into pydantic:main with commit e3dca28 Nov 10, 2026
32 checks passed
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.

Variable.get() parses variables/variable.py source on its first call when inspect_arguments is enabled

2 participants