Skip to content

Formatter panic when range formatting certain clause headers with semicolons #21065

@dylwil3

Description

@dylwil3

Summary

Given ex.py with content:

x =7
try:
    a;
finally:
    b

We get:

ruff format --range=2 --diff ex.py
error: Failed to format tarmin.py: syntax error: Expected the keyword token but found another token instead.

This only happens with range formatting, since it comes from this call of self.range:

// Write a source map entry for the colon for range formatting to support formatting the clause header without
// the clause body. Avoid computing `self.header.range()` otherwise because it's somewhat involved.
let clause_end = if f.options().source_map_generation().is_enabled() {
Some(source_position(
self.header.range(f.context().source())?.end(),
))

The SimpleTokenizer finding the keyword range is expecting to find Finally but instead it finds the (unnecessary) semi-colon.

Version

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions