Python: fix AutoFormat crash on import ... as ...#7412
Merged
knutwannheden merged 1 commit intomainfrom Apr 17, 2026
Merged
Conversation
`SpacesVisitor.visit_import` wrote a raw `Identifier` into `Import._alias` (typed `JLeftPadded[Identifier]`) because `replace_if_changed` maps public kwarg names to private fields without type-checking the value. The second spacing adjustment then read `imp.padding.alias` back as a bare `Identifier` and crashed with `AttributeError: 'Identifier' object has no attribute 'before'`. Apply both the before-`as` and before-identifier spacing to the `JLeftPadded` wrapper before writing it back.
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.
Motivation
AutoFormatcrashed on any Python file containingimport X as Yorfrom X import Y as Z, surfacing asAttributeError: 'Identifier' object has no attribute 'before'deep inSpacesVisitor. Discovered via flagship recipe alerts while runningUnwrapElseAfterReturnon real sources.Root cause is a subtle mismatch between public and private fields on padded LST nodes.
Import._aliasis typedOptional[JLeftPadded[Identifier]], while the publicaliasproperty unwraps to the bareIdentifier. The previous code did:replace_if_changedmaps the public kwargaliasto the private field_aliaswithout type-checking the value, so the first line clobbered theJLeftPaddedwrapper with a rawIdentifier. The second line then readimp.padding.aliasback as a bareIdentifierand crashed on.before.Summary
SpacesVisitor.visit_importnow applies both spacing adjustments to theJLeftPaddedwrapper (usingspace_before_left_paddedfor the space beforeas, andspace_before_left_padded_elementfor the space before the alias identifier) before a singlepadding.replace.import X as Yandfrom X import Y as Zwith non-normalized spacing.Test plan
test_import_with_alias_normalizes_spacingfails on trunk with the reportedAttributeErrorand passes after the fixpytest tests/python/all/format/ tests/python/all/tree/import_test.py tests/python/format/— 181 passedpytest tests/ --ignore=tests/rpc— 1157 passed, 6 skipped