Python: Fix auto_format adding extra indentation on sub-trees#6842
Merged
knutwannheden merged 1 commit intomainfrom Feb 27, 2026
Merged
Python: Fix auto_format adding extra indentation on sub-trees#6842knutwannheden merged 1 commit intomainfrom
auto_format adding extra indentation on sub-trees#6842knutwannheden merged 1 commit intomainfrom
Conversation
When auto_format is called on a sub-tree (e.g., an If node inside a method body) with a cursor, TabsAndIndentsVisitor.visit() called pre_visit() on the root element during setup. This set indent_type=INDENT on the cursor, causing visit_space() to add an extra indent_size to the node's own prefix — doubling the indentation. The pre_visit setup was intended to establish context for children, but it incorrectly affected the root element itself. In normal full-tree traversal, pre_visit is called as the visitor descends, so the node's own prefix uses the parent's indent_type (not its own). Remove the setup pre_visit call. The normal traversal pre_visit calls handle child indentation correctly without it.
auto_format adding extra indentation on sub-trees
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TabsAndIndentsVisitor.visit()adding an extra indentation level whenauto_formatis called on a sub-tree with a cursorpre_visit()call incorrectly setindent_type=INDENTon the root element's cursor, causingvisit_space()to double the indentation (e.g., 4-space indent became 8-space)auto_formaton anIfnode inside a method, a nested class method, and at top levelContext
Discovered while developing Python cleanup recipes that call
auto_format(result, p, cursor=self.cursor)after restructuring if/else chains. TheSwapIfElseBranchesrecipe'sauto_formatcall shifted a 4-space-indentedifto 8-space indentation inside a method body.Test plan
auto_format_subtree_test.pyverify the fix at different nesting levels