Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unexpected whitespace handling in f-string !r #93283

Closed
Labels
3.10only security fixes3.11only security fixes3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@dimaqq

Description

@dimaqq

Bug report

The handling of whitespace around the argument in literal string interpolation (f-string) is unexpected when a conversion specifier (e.g. !r) is applied.

Consider

f"{None}"# OKf"{None}"# OKf"{None!r}"# OKf"{None!r}"# SyntaxError# detailsf"{None!r}# OKf"{None !r}# OKf"{None!r}# SyntaxError

PEP-498 states:

Leading and trailing whitespace in expressions is ignored
For ease of readability, leading and trailing whitespace in expressions is ignored. This is a by-product of enclosing the expression in parentheses before evaluation.

I suppose that the text above may be technically accurate, but is misleading to common folk:
In the example above,None is enclosed in parentheses(None) but notNone!r.

There are no mentions of whitespace in the rest of the PEP.

Formal reproducer:

> python3.11 -c'print(f"{ None!r }")'  File"<string>", line 1    print(f"{ None!r }")                       ^SyntaxError: f-string: expecting'}'

Your environment

  • CPython versions tested on: 3.8, 3.9, 3.10, 3.11a2, 3.11a7
  • Operating system and architecture: Linux, MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixes3.11only security fixes3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp