Skip to content

Raw f-string format spec undocumented behavior change between 3.11 and 3.12 #137314

Closed

Description

@MeGaGiGaGon

Bug report

Bug description:

I found this while working on astral-sh/ruff#19546 .

Between 3.11 and 3.12, the behavior of raw f-string format specifiers changed.

On 3.11 and earlier, the rawness of the f-string is respected in the format specifier:

PS ~>uvx python@3.11 -c @'
class UnchangedFormat:
    def __format__(self, format):
        return format

print("Non-raw output:", repr(f"{UnchangedFormat():\xFF}"))
print("Raw output:", repr(rf"{UnchangedFormat():\xFF}"))
'@
Non-raw output: 'ÿ'
Raw output: '\\xFF'

On 3.12 and later, the rawness of the f-string is not respected:

PS ~>uvx python@3.12 -c @'
class UnchangedFormat:
    def __format__(self, format):
        return format

print("Non-raw output:", repr(f"{UnchangedFormat():\xFF}"))
print("Raw output:", repr(rf"{UnchangedFormat():\xFF}"))
'@
Non-raw output: 'ÿ'
Raw output: 'ÿ'

This new behavior looks to have been carried to t-strings on 3.14 and later:

PS ~>uvx python@3.14 -c @'
class UnchangedFormat:
    def __format__(self, format):
        return format

print("Non-raw output:", repr(t"{UnchangedFormat():\xFF}"))
print("Raw output:", repr(rt"{UnchangedFormat():\xFF}"))
'@
Non-raw output: Template(strings=('', ''), interpolations=(Interpolation(<__main__.UnchangedFormat object at 0x00000183F9904EC0>, 'UnchangedFormat()', None, 'ÿ'),))
Raw output: Template(strings=('', ''), interpolations=(Interpolation(<__main__.UnchangedFormat object at 0x00000183F9908A50>, 'UnchangedFormat()', None, 'ÿ'),))

I could not find any information/documentation about how the rawness of f-strings affect their format specifiers, despite looking in:

CPython versions tested on:

3.12, 3.11, 3.14

Operating systems tested on:

Windows

Linked PRs

Activity

  1. picnixz commented on Aug 1, 2025

    @picnixz
    Member

    Unfortunately, 3.11 and 3.12 are both security-only and I don't think this is a security issue. AFAIK, there isn't an exceptional rule for such bugs, so even if this is a regression, we won't patch it :( However, we may fix it in 3.13+ (maybe?)

    cc @pablogsal

  2. ericvsmith commented on Aug 2, 2025

    @ericvsmith
    Member

    This looks like a bug to me. I think it should be fixed in 3.14 and backported to 3.13.

  3. pablogsal commented on Aug 2, 2025

    @pablogsal
    Member

    This looks like a bug to me. I think it should be fixed in 3.14 and backported to 3.13.

    Agreed 👍

  4. added a commit that references this issue on Aug 3, 2025
  5. added 2 commits that reference this issue on Aug 3, 2025
  6. added 3 commits that reference this issue on Aug 3, 2025
  7. MeGaGiGaGon commented on Aug 3, 2025

    @MeGaGiGaGon
    ContributorAuthor

    Looks like the PRs have gone through, wow that was fast :)

    The only remaining questions I have are on documentation:

    • Should something be added to the docs somewhere explicitly stating that format specs are affected by f-string rawness?
    • What about 3.12? I assume these changes won't be backported to 3.12 since it's not a security issue, so should somewhere in the docs mention how 3.12 behaves differently?
  8. pablogsal commented on Aug 3, 2025

    @pablogsal
    Member

    Looks like the PRs have gone through, wow that was fast :)

    The only remaining questions I have are on documentation:

    • Should something be added to the docs somewhere explicitly stating that format specs are affected by f-string rawness?

    Yeah. Would you like to do the PR to the docs?

    • What about 3.12? I assume these changes won't be backported to 3.12 since it's not a security issue, so should somewhere in the docs mention how 3.12 behaves differently?

    It depends on @Yhg1s (see #137328 (comment)) but likely it won't be back ported

  9. MeGaGiGaGon commented on Aug 3, 2025

    @MeGaGiGaGon
    ContributorAuthor

    Looks like the PRs have gone through, wow that was fast :)
    The only remaining questions I have are on documentation:

    • Should something be added to the docs somewhere explicitly stating that format specs are affected by f-string rawness?

    Yeah. Would you like to do the PR to the docs?

    Not particularly, I'm both not sure on the wording and on where it should go.

  10. Yhg1s commented on Aug 4, 2025

    @Yhg1s
    Member
    • What about 3.12? I assume these changes won't be backported to 3.12 since it's not a security issue, so should somewhere in the docs mention how 3.12 behaves differently?

    It depends on @Yhg1s (see #137328 (comment)) but likely it won't be back ported

    Indeed, it's not really the kind of thing we'd backport at this stage. Documenting the difference might be worth the effort, although given that it took this long for people to report it... maybe?

  11. pablogsal commented on Aug 4, 2025

    @pablogsal
    Member

    that it took this long for people to report it... maybe?

    yeah to me this looks fundamentally low risk, likely only affecting linters in their test suite :)

  12. ericvsmith commented on Aug 6, 2025

    @ericvsmith
    Member

    I agree we don't want to backport it: there's only been one bug report in the 2+ years it's been a problem.

  13. reopened this on Aug 7, 2025
  14. picnixz commented on Aug 7, 2025

    @picnixz
    Member

    Sorry I closed it but I think we want to add a doc note in which case the issue is still needed (?)

  15. ericvsmith commented on Aug 7, 2025

    @ericvsmith
    Member

    I'm not sure we need any documentation (it's just how raw strings are expected to work).

  16. MeGaGiGaGon commented on Aug 7, 2025

    @MeGaGiGaGon
    ContributorAuthor

    Given all the discussion above this sounds good to me too, thank you all for your help on this

  17. added a commit that references this issue on Aug 19, 2025
  18. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions