feat(Python): Enable commutation of class attributes, with docstrings #590

Merged
wetneb merged 4 commits from 589-python-docstrings-bundle into main 2025-09-17 12:34:50 +02:00
Owner

Closes #589.

I am not very happy with the overall approach, but it feels like it's the best I can do.

Ideally, the bundling of the comments should be done in the grammar, similarly to what we do in Rust, since the attachment is done by Python itself, regardless of the amount of whitespace surrounding it. But I haven't been able to update the grammar accordingly so far.

We could also decide to define the additional comments via a tree-sitter query, but as things stand it would still not remove the need to fork the grammar because the assignment nodes are wrapped inside an expression_statement node, making it impossible to define the commutative children groups as things stand (except if we also migrate those to support TS queries…)
That being said, there are good signs that the tree-sitter-python maintainer will accept a move to hide those expression_statement nodes in the upstream grammar. When that gets published, we could then be able to switch back to upstream, assuming we change the comment_nodes to use a tree-sitter query instead. But that also has downsides, since that's going to add another query to execute at the beginning of the parsing, complicating the AstNode construction a bit further.

In the interest of meeting Stainless' needs, I think it would still be good to ship this (or some variant of it…)

Closes #589. I am not very happy with the overall approach, but it feels like it's the best I can do. Ideally, the bundling of the comments should be done in the grammar, similarly to what we do in Rust, since the attachment is done by Python itself, regardless of the amount of whitespace surrounding it. But I haven't been able to update the grammar accordingly so far. We could also decide to define the additional comments via a tree-sitter query, but as things stand it would still not remove the need to fork the grammar because the `assignment` nodes are wrapped inside an `expression_statement` node, making it impossible to define the commutative children groups as things stand (except if we also migrate those to support TS queries…) That being said, there are [good signs that the tree-sitter-python maintainer will accept a move to hide those `expression_statement` nodes](https://github.com/tree-sitter/tree-sitter-python/pull/317) in the upstream grammar. When that gets published, we could then be able to switch back to upstream, assuming we change the `comment_nodes` to use a tree-sitter query instead. But that also has downsides, since that's going to add another query to execute at the beginning of the parsing, complicating the `AstNode` construction a bit further. In the interest of meeting Stainless' needs, I think it would still be good to ship this (or some variant of it…)
wetneb changed title from 589-python-docstrings-bundle to feat(Python): Enable commutation of class attributes, with docstrings 2025-09-15 23:17:20 +02:00
@ -0,0 +3,4 @@
y = 2
z = 3
Author
Owner

This example is crying for being spelt out without blank lines between arguments, but merging class attributes with docstrings requires us to still insert a blank line between the merged attributes, otherwise the docstrings end up being at equal distance between two consecutive attributes, which prevents them from being bundled correctly when re-parsing the merge result. This is calling for #373.

This example is crying for being spelt out without blank lines between arguments, but merging class attributes with docstrings requires us to still insert a blank line between the merged attributes, otherwise the docstrings end up being at equal distance between two consecutive attributes, which prevents them from being bundled correctly when re-parsing the merge result. This is calling for #373.
ada4a approved these changes 2025-09-17 09:16:53 +02:00
wetneb force-pushed 589-python-docstrings-bundle from 94dc6eea1a to 4368a54cd1 2025-09-17 11:29:46 +02:00 Compare
wetneb merged commit 952e8c6661 into main 2025-09-17 12:34:50 +02:00
ada4a deleted branch 589-python-docstrings-bundle 2025-09-17 13:51:29 +02:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: mergiraf/mergiraf#590
No description provided.