Skip to content

Conversation

@TaKO8Ki
Copy link
Contributor

@TaKO8Ki TaKO8Ki commented Sep 21, 2025

Summary

Fixes #19226

Fixes a panic in binary expression formatting caused by TextRange assertion failures when comments are positioned between unary operators (like not) and their operands.

The issue occurred because expression.start() points to the unary operator while comment.end() can be positioned after it, creating invalid TextRange where start > end. The fix uses operand.start() for unary operations to ensure proper ordering.

Test Plan

I have added new test cases to crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/unary.py.

@github-actions
Copy link
Contributor

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

Thank you. I need to think about whether this is the intended comment placement as it means that a comment can now move "forward" (it moves from after not to before not)

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Sep 23, 2025
@TaKO8Ki
Copy link
Contributor Author

TaKO8Ki commented Sep 23, 2025

@MichaReiser Ok. If it's not intentional, I can make has_lparen_between_comment_and_expression to skip unary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic assertion failed: start.raw <= end.raw in ruff_python_formatter/src/expression/binary_like.rs

2 participants