Skip to content

Conversation

@Harish-Nesrekar
Copy link

Description

Fix crash in string-processing preview for unassigned one-item tuples of string literals with a trailing comma.

  • Problem: Black crashed when formatting code like:
"A very long string that exceeds line length and has a trailing comma",

because it tried to split the string but did not wrap it in parentheses, producing code that is not AST-equivalent.

  • Cause: _prefer_paren_wrap_match() decides whether to wrap a literal in parentheses. Previously, syms.testlist (one-item tuples) was missing, so parentheses weren’t added.

  • Fix: Add syms.testlist to _prefer_paren_wrap_match():

matching_nodes = [
    syms.listmaker,
    syms.dictsetmaker,
    syms.testlist_gexp,
    syms.testlist,  # added to handle one-item tuples
]
  • This is a minimal, safe fix that preserves AST semantics.

  • Added regression test:
    tests/data/preview/string_processing_trailing_comma.py

  • Verified with:

black --preview --enable-unstable-feature=string_processing tests/data/preview/string_processing_trailing_comma.py

Checklist - did you ...

  • Implement code changes under --preview style?
  • Add / update tests?
  • Add an entry in CHANGES.md if necessary?
  • Add / update documentation if necessary?
Screenshot 2025-12-25 015852

@cobaltt7
Copy link
Collaborator

The test cases don't seem to be picked up by our tests runner. Please put them in tests/data/cases (not preview). The three test cases can be put in the same file.
Is this bug only reproducible with string_processing? If so, please add this comment to the beginning of the tests file so it's correctly run on the preview style: # flags: --unstable (The test runner will make sure it doesn't crash on preview regardless, but it's good to check any change in formatting is intentional)

Additionally, can you add a changelog entry to CHANGES.md, and please run Black on the codebase (python src/black .)

Thanks for working on this!

@github-actions
Copy link

github-actions bot commented Dec 24, 2025

diff-shades reports zero changes comparing this PR (88abdab) to main (c3cc5a9).


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

@Harish-Nesrekar
Copy link
Author

Hi @cobaltt7

Thanks for the review!

I’ve moved the test cases to tests/data/cases/string_trailing_comma.py and combined all three related cases into a single file.

Added the # flags: --unstable comment at the top so it runs correctly under preview mode.

Added a changelog entry in CHANGES.md.

Ran Black on the codebase for consistent formatting.

All changes are now pushed to the string-processing branch.

Please let me know if there’s anything else needed!

Thanks again for the guidance.

@cobaltt7
Copy link
Collaborator

I'm inclined to believe that this PR is AI-generated. While using AI in itself isn't forbidden, we do expect you to look over what the AI did, that you've reviewed the code it writes, and that submitted PRs are up to our code standards and in line with the project expectations and requirements. Unfortunately, your contribution does not meet this bar.

Please review the docs on writing test cases; it's still not correct. https://black.readthedocs.io/en/stable/contributing/the_basics.html#testing
Please also correct the changelog entry.

@Harish-Nesrekar
Copy link
Author

Harish-Nesrekar commented Dec 25, 2025 via email

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.

2 participants