[New] order
: allow controlling intragroup spacing of type-only imports via newlines-between-types
#3127
+678
−38
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.
Depends on #3104
This PR implements in
import/order
: a new option analogous tonewlines-between
but specifically for controlling newlines between type-only imports.A demo package containing this feature is temporarily available for easy testing:
Allow controlling intragroup spacing of type-only imports
This is implemented via
newlines-between-types
. The proposed documentation corresponding to this new feature can be previewed here.Example
Given this code:
And the following settings, the rule check will fail:
With
--fix
yielding:However, with the following settings, the rule check will succeed instead:
{ "groups": ["type", "builtin", "parent", "sibling", "index"], "sortTypesAmongThemselves": true, "newlines-between": "always", + "newlines-between-types": "ignore" }
sortTypesAmongThemselves
allows sorting type-only and normal imports separately. By default,newlines-between
will govern all newlines between import statements like normal.I generally want my type-only imports to be sorted for ease of reference but never have newlines between them (save space) while I want my normal imports (which I tend to visually peruse more often) to be aesthetically pleasing, grouped, and sorted.
newlines-between
is too coarse-grained for this, so this PR introducesnewlines-between-types
, a setting identical tonewlines-between
except it only applies to type-only imports, and only whensortTypesAmongThemselves
is enabled (i.e. it is backward-compatible).When
newlines-between
andnewlines-between-types
conflict,newlines-between-types
takes precedence for type-only imports. For normal imports,newlines-between-types
is ignored entirely.One issue that might warrant further discussion is which setting governs the newline separating type-only imports from normal imports. Right now, I have it so
newlines-between-types
controls this space, but perhaps it should be its own setting.