Skip to content

Python: Fix tuple parsing for parenthesized element with trailing comma#6811

Merged
knutwannheden merged 1 commit intomainfrom
oddly-bear
Feb 25, 2026
Merged

Python: Fix tuple parsing for parenthesized element with trailing comma#6811
knutwannheden merged 1 commit intomainfrom
oddly-bear

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Summary

  • Fixes parser crash ('NoneType' object has no attribute 'value_source') when parsing files containing single-element tuples where parentheses wrap the element and the trailing comma is outside, e.g. ("a"),
  • The parser incorrectly identified ( as the tuple's opening paren in such expressions, causing the token index to progressively fall behind the AST position, eventually crashing when encountering string literals in type annotations like Literal["..."]
  • Adds token-level analysis for single-element tuples: checks whether ) is followed by , with no , inside the parens, distinguishing ("a"), (grouping parens + implicit tuple) from ("a",) (tuple parens)

Test plan

  • Added test_literal_string_type_hint_with_assignment — minimal reproduction of the original crash
  • Added test_single_element_tuple_with_trailing_comma_outside_parens — inline case
  • Added test_single_element_tuple_with_trailing_comma_outside_parens_multiline — multi-line string concatenation case
  • All 833 existing Python parser tests pass (422 tree tests, 411 others)
  • Verified the original failing file (preview_long_strings.py) parses successfully

…ma outside

The parser incorrectly identified `(` as the tuple's opening paren in
expressions like `("a"),` where the parentheses wrap the element and the
trailing comma outside creates an implicit single-element tuple. This caused
the token index to fall behind the AST position, eventually crashing with
`'NoneType' object has no attribute 'value_source'` when parsing string
literals in type annotations like `Literal["..."]` later in the file.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Feb 25, 2026
@knutwannheden knutwannheden merged commit 35854f8 into main Feb 25, 2026
1 check passed
@knutwannheden knutwannheden deleted the oddly-bear branch February 25, 2026 08:42
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Feb 25, 2026
macsux pushed a commit that referenced this pull request Feb 27, 2026
…ma outside (#6811)

The parser incorrectly identified `(` as the tuple's opening paren in
expressions like `("a"),` where the parentheses wrap the element and the
trailing comma outside creates an implicit single-element tuple. This caused
the token index to fall behind the AST position, eventually crashing with
`'NoneType' object has no attribute 'value_source'` when parsing string
literals in type annotations like `Literal["..."]` later in the file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant