Skip to content

Fix f-string debug format parsing for Python 3.8+ ({expr=})#118

Open
aryanyk wants to merge 1 commit intogoogle:masterfrom
aryanyk:fix-fstring-equal-sign
Open

Fix f-string debug format parsing for Python 3.8+ ({expr=})#118
aryanyk wants to merge 1 commit intogoogle:masterfrom
aryanyk:fix-fstring-equal-sign

Conversation

@aryanyk
Copy link

@aryanyk aryanyk commented Feb 4, 2026

Summary

This PR fixes Issue #115, where Python 3.8+ f-string debug format ({expr=}) fails to parse correctly and raises:

AnnotationError: Expected '!' but found '='

The fix is scoped to Python 3.8–3.11, which are within pasta’s supported range.


Background

Python 3.8 introduced debug f-strings:

f"{a=}"

Unlike regular f-strings, {expr=}:

  • Leaves '=' in the token stream
  • Implicitly sets conversion=repr
  • Does not include an explicit '!' in the source

The current logic in visit_FormattedValue assumes that conversion must be introduced by '!', which causes parsing to fail.


Root Cause

When parsing {expr=}:

  1. The expression tokens are consumed
  2. The parser expects '!' for conversion
  3. '=' is encountered instead
  4. An error is raised

Fix

The fix updates visit_FormattedValue to:

  1. Explicitly consume the debug specifier '='
  2. Make conversion parsing conditional on the presence of '!'
  3. Only parse the conversion character when '!' is present

This matches CPython’s behavior.


Tests

Added test_fstring_debug_format covering:

  • f"{a=}"
  • f"{a=!r}"
  • f"{a=:d}"
  • f"{a=!r:10}"
  • f"prefix {x=} suffix"

Verification

Tests pass on:

  • Python 3.10
  • Python 3.11

Notes

Python 3.12+ introduces PEP 701 (PEG-based f-string parsing), which is a separate compatibility issue and not addressed here.

@google-cla
Copy link

google-cla bot commented Feb 4, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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