Skip to content

Mangle private names when evaluating f-string values inside a class - #2861

Merged
alexmojaki merged 3 commits into
pydantic:mainfrom
caiotheodoro:fix/fstring-private-names
Nov 23, 2026
Merged

alexmojaki merged 3 commits into
pydantic:mainfrom
caiotheodoro:fix/fstring-private-names

Conversation

@caiotheodoro

@caiotheodoro caiotheodoro commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2860.

When the f-string sits inside a class body, compile_formatted_value now applies CPython's private-name mangling to the names in the compiled value and format spec. It uses the innermost enclosing class, found through the parent links executing sets. F-strings outside a class are unchanged. Zero-arg super() inside an f-string still fails, but that has a different cause and isn't touched here.

test_private_names_in_fstring covers a private attribute, a private class attribute and a private local in a format spec, and fails on main with the issue's AttributeError. Full suite (3687 passed), make lint and make typecheck are green.

Drafted with Devin (Claude Opus 5.5, SWE-2).

Inside a class body the compiler rewrites `self.__x` to `self._ClassName__x`.
The f-string magic compiles each value on its own, so the second evaluation
looked up the unmangled name, raised AttributeError or NameError, and was
reported as an internal error (re-raised under pytest). Apply the same
rewrite to the names in the compiled value and format spec.
@veria-ai

veria-ai Bot commented Nov 22, 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

Comment thread tests/test_formatter.py Resolved
@github-actions

github-actions Bot commented Nov 22, 2026 •

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

@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 1 file (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Re-trigger cubic

Comment thread tests/test_formatter.py Resolved
@alexmojaki
alexmojaki added this pull request to the merge queue Nov 23, 2026
Merged via the queue into pydantic:main with commit 6247e9a Nov 23, 2026
34 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.

f-string magic raises an internal error for private attributes inside a class

2 participants