Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f'"{var:}"' adds redundant empty string literal in AST in 3.12 #106396

Closed
sobolevn opened this issue Jul 4, 2023 · 2 comments
Closed

f'"{var:}"' adds redundant empty string literal in AST in 3.12 #106396

sobolevn opened this issue Jul 4, 2023 · 2 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jul 4, 2023

3.11:

>>> import ast
>>> print(ast.dump(ast.parse("""f'"{var:}"'"""), indent=2))
Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          Constant(value='"'),
          FormattedValue(
            value=Name(id='var', ctx=Load()),
            conversion=-1,
            format_spec=JoinedStr(values=[])),
          Constant(value='"')]))],
  type_ignores=[])

3.12:

>>> import ast
>>> print(ast.dump(ast.parse("""f'"{var:}"'"""), indent=2))
Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          Constant(value='"'),
          FormattedValue(
            value=Name(id='var', ctx=Load()),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                Constant(value='')])),
          Constant(value='"')]))],
  type_ignores=[])

This affects several linters:

  1. Fix parse test case on Python 3.12 mypy#15577
  2. B907 crashes when bugbear is run on Python 3.12 PyCQA/flake8-bugbear#393

Is this a bug? Or is it a feature?

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.12 bugs and security fixes labels Jul 4, 2023
@AlexWaygood AlexWaygood added the 3.13 bugs and security fixes label Jul 4, 2023
@sunmy2019
Copy link
Member

First of all, it is not wrong. We do have an empty format spec string here.

Fixing it for compatibility reasons is also acceptable to me. I am 60-40 towards fixing it.

@lysnikolaou
Copy link
Contributor

I'd say let's fix it both for compatibility and for consistency with things like an empty f-string, which is also a JoinedStr with an empty values array.

cpython on main via C v14.0.3-clang via 🐍 pyenv 3.11.3 
❯ ./python.exe           
Python 3.13.0a0 (heads/main:6c60684bf5, Jul  4 2023, 11:09:56) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> print(ast.dump(ast.parse("""f''"""), indent=2))
Module(
  body=[
    Expr(
      value=JoinedStr(values=[]))],
  type_ignores=[])
>>> print(ast.dump(ast.parse("""f'"{var:}"'"""), indent=2))
Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          Constant(value='"'),
          FormattedValue(
            value=Name(id='var', ctx=Load()),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                Constant(value='')])),
          Constant(value='"')]))],
  type_ignores=[])

lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Jul 4, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 4, 2023
…r node (pythonGH-106401)

(cherry picked from commit dfe4de2)

Co-authored-by: Lysandros Nikolaou <[email protected]>
lysnikolaou added a commit that referenced this issue Jul 4, 2023
…tr node (GH-106401) (#106416)

(cherry picked from commit dfe4de2)

Co-authored-by: Lysandros Nikolaou <[email protected]>
carljm added a commit to carljm/cpython that referenced this issue Jul 5, 2023
* main: (39 commits)
  pythongh-102542 Remove unused bytes object and bytes slicing (python#106433)
  Clarify state of CancelledError in doc (python#106453)
  pythongh-64595: Fix regression in file write logic in Argument Clinic (python#106449)
  pythongh-104683: Rename Lib/test/clinic.test as Lib/test/clinic.test.c (python#106443)
  tp_flags docs: fix indentation (python#106420)
  pythongh-104050: Partially annotate Argument Clinic CLanguage class (python#106437)
  pythongh-106368: Add tests for formatting helpers in Argument Clinic (python#106415)
  pythongh-104050: Annotate Argument Clinic parameter permutation helpers (python#106431)
  pythongh-104050: Annotate toplevel functions in clinic.py (python#106435)
  pythongh-106320: Fix specialize.c compilation by including pycore_pylifecycle.h (python#106434)
  Add some codeowners for `Tools/clinic/` (python#106430)
  pythongh-106217: Truncate the issue body size of `new-bugs-announce-notifier` (python#106423)
  pythongh-61215: Rename `wait_until_any_call` to `wait_until_any_call_with` (python#106414)
  pythongh-106162: array: suppress warning in test_array (python#106404)
  pythongh-106320: Remove _PyInterpreterState_HasFeature() (python#106425)
  pythonGH-106360: Support very basic superblock introspection (python#106422)
  pythongh-106406: Fix _Py_IsInterpreterFinalizing() in _winapi.c (python#106408)
  pythongh-106396: Special-case empty format spec to gen empty JoinedStr node (python#106401)
  pythongh-106368: Add tests for permutation helpers in Argument Clinic (python#106407)
  pythonGH-106008: Fix refleak when peepholing `None` comparisons (python#106367)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants