Skip to content

Conversation

@Ankitsinghsisodya
Copy link

@Ankitsinghsisodya Ankitsinghsisodya commented Dec 30, 2025

Description

This PR fixes issue #4912 where string_processing would crash with an INTERNAL ERROR
when processing an unassigned long string literal with a trailing comma (creating a
one-item tuple).

The Problem: When a line contains only a long string literal followed by a trailing
comma (making it a one-item tuple) and is not inside brackets, StringSplitter would
split the string into multiple implicitly concatenated strings. This created multiple
expressions where only the last has the comma, breaking AST equivalence:

# Input
"A long string that exceeds line length...",

# Incorrect output (before fix)
'A long string that exceeds line length'
'continuation of the string...',  # Only this line has the comma!

The Fix:

  1. StringSplitter.do_splitter_match() now detects the STRING + COMMA pattern (bare
    one-item tuple not inside brackets) and returns an error, deferring to
    StringParenWrapper
  2. StringParenWrapper._trailing_comma_tuple_match() is a new method that matches this
    specific pattern
  3. The string is wrapped in parentheses before splitting, preserving the tuple
    semantics:
# Correct output (after fix)
(
    "A long string that exceeds line length"
    " continuation of the string..."
),

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the
    stability policy? > Yes, string_processing is an unstable feature enabled via
    --preview --enable-unstable-feature=string_processing

  • Add an entry in CHANGES.md if necessary? > Added under "Preview style" in the
    Unreleased section

  • Add / update tests if necessary? > Added test case in
    tests/data/cases/preview_long_strings.py

  • Add new / update outdated documentation? > No documentation changes needed - this
    is a bugfix for existing functionality

Fixes #4912
closes #4917

Ankitsinghsisodya and others added 2 commits December 30, 2025 07:14
…h trailing commas

When a line contains only a long string literal followed by a trailing comma
(making it a one-item tuple) and is not inside brackets, StringSplitter would
split the string, creating multiple expressions where only the last has the
comma. This broke AST equivalence.

The fix:
1. StringSplitter now detects this pattern and defers to StringParenWrapper
2. StringParenWrapper wraps the string in parentheses before splitting

Fixes psf#4912
@github-actions
Copy link

diff-shades reports zero changes comparing this PR (2fcfba1) to main (c3cc5a9).


What is this? | Workflow run | diff-shades documentation

@cobaltt7
Copy link
Collaborator

cobaltt7 commented Jan 1, 2026

Everything looks good here, thanks! Will merge after the 2026 style is stabilized.

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.

string_processing: error with unassigned long string literal with trailing comma

2 participants