[python] cycle import style #28
Open
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.
Python import statements can be written in a few different styles. Initially, I wanted the equivalent of
toggle_multiline()forimport_from_statementsbetween "inline" and "expand" (seeFormats), but certain style guides prefer a "single" style (one import per line). There are other preferences, like whether a multi-line statement is wrapped in parentheses or uses\for line continuation, and it also needs to account for a max line_length. So I wrote up something custom to cycle between them, and made it configurable.Formats
import_statementimport_from_statementSiblings
cycle_import()will look for and include siblings that are adjacent to the node performing the action. Siblings have the same:type()and import from the same module/package. This is what enables the "single" format to participate in cycling. For example, going from "single" to "inline":isn't possible without being able to combine* the two.
*Implementation Note:
There is no appropriate parent to act as a
targetnode that could provide the correctnode:range()forinit.lua:replace_node(), so in this case, it creates a table inmake_target_node()that responds to:range()with the correct start/end positions (and more, see comments).