Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[isort] Add support for length-sort settings #8841

Merged
merged 8 commits into from
Nov 28, 2023

Conversation

bluthej
Copy link
Contributor

@bluthej bluthej commented Nov 26, 2023

Summary

Closes #1567.

Add both length-sort and length-sort-straight settings for isort.

Here are a few notable points:

  • The length is determined using the unicode_width crate, i.e. we are talking about displayed length (this is explicitly mentioned in the description of the setting)
  • The dots are taken into account in the length to be compatible with the original isort
  • I had to reorder a few fields of the module key struct for it all to make sense (notably the force_to_top field is now the first one)

Test Plan

I added tests for the following cases:

  • Basic tests for length-sort with ASCII characters only
  • Tests with non-ASCII characters
  • Tests with relative imports
  • Tests for length-sort-straight

The `UnicodeWidthStr` trait yields "displayed width of Unicode strings".

Some tests with non-ASCII characters were added.
- Makes more sense to have force_to_top first
- Allows to place maybe_length after force_to_top but before distance
The name does not contain the dots for relative imports so the number
of dots has to be added
Copy link
Contributor

github-actions bot commented Nov 26, 2023

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+0 -39 violations, +0 -0 fixes in 41 projects)

apache/airflow (+0 -12 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-preview --select ALL

- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:31: ANN001 Missing type annotation for function argument `connection_id`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:46: ANN001 Missing type annotation for function argument `hostname`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:5: ANN201 Missing return type annotation for public function `configure_hive_connection`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:110:10: COM812 [*] Trailing comma missing
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:117:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:52:27: ANN001 Missing type annotation for function argument `table_name`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:52:5: ANN201 Missing return type annotation for public function `create_dynamodb_table`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:72:29: ANN001 Missing type annotation for function argument `table_name`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:72:5: ANN201 Missing return type annotation for public function `get_dynamodb_item_count`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:86:5: T201 `print` found
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:93:27: ANN001 Missing type annotation for function argument `table_name`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:93:5: ANN201 Missing return type annotation for public function `delete_dynamodb_table`

bokeh/bokeh (+0 -27 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-preview --select ALL

- src/bokeh/server/views/metadata_handler.py:25:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:28:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:31:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:32:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:39:5: Q000 [*] Single quotes found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:51:5: D200 One-line docstring should fit on one line
- src/bokeh/server/views/metadata_handler.py:51:5: D210 [*] No whitespaces allowed surrounding docstring text
- src/bokeh/server/views/metadata_handler.py:51:5: D300 Use triple double quotes `"""`
- src/bokeh/server/views/metadata_handler.py:51:5: D400 First line should end with a period
- src/bokeh/server/views/metadata_handler.py:51:5: D415 First line should end with a period, question mark, or exclamation point
- src/bokeh/server/views/metadata_handler.py:51:5: Q002 [*] Single quote docstring found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:56:15: ANN201 Missing return type annotation for public function `get`
- src/bokeh/server/views/metadata_handler.py:56:15: D102 Missing docstring in public method
- src/bokeh/server/views/metadata_handler.py:56:19: ANN101 Missing type annotation for `self` in method
- src/bokeh/server/views/metadata_handler.py:56:26: ANN002 Missing type annotation for `*args`
- src/bokeh/server/views/metadata_handler.py:56:26: ARG002 Unused method argument: `args`
- src/bokeh/server/views/metadata_handler.py:56:34: ANN003 Missing type annotation for `**kwargs`
- src/bokeh/server/views/metadata_handler.py:56:34: ARG002 Unused method argument: `kwargs`
- src/bokeh/server/views/metadata_handler.py:64:20: C408 Unnecessary `dict` call (rewrite as a literal)
- src/bokeh/server/views/metadata_handler.py:66:41: Q000 [*] Single quotes found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:7:1: D205 1 blank line required between summary line and description
- src/bokeh/server/views/metadata_handler.py:7:1: D210 [*] No whitespaces allowed surrounding docstring text
- src/bokeh/server/views/metadata_handler.py:7:1: D300 Use triple double quotes `"""`
- src/bokeh/server/views/metadata_handler.py:7:1: D400 First line should end with a period
- src/bokeh/server/views/metadata_handler.py:7:1: D415 First line should end with a period, question mark, or exclamation point
- src/bokeh/server/views/metadata_handler.py:7:1: Q002 [*] Single quote docstring found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:8:1: D208 [*] Docstring is over-indented

Changes by rule (21 rules affected)

code total + violation - violation + fix - fix
ANN001 5 0 5 0 0
ANN201 5 0 5 0 0
E402 4 0 4 0 0
Q000 2 0 2 0 0
D210 2 0 2 0 0
D300 2 0 2 0 0
D400 2 0 2 0 0
D415 2 0 2 0 0
Q002 2 0 2 0 0
ARG002 2 0 2 0 0
COM812 1 0 1 0 0
DTZ001 1 0 1 0 0
T201 1 0 1 0 0
D200 1 0 1 0 0
D102 1 0 1 0 0
ANN101 1 0 1 0 0
ANN002 1 0 1 0 0
ANN003 1 0 1 0 0
C408 1 0 1 0 0
D205 1 0 1 0 0
D208 1 0 1 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -57 violations, +0 -0 fixes in 41 projects)

apache/airflow (+0 -13 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:31: ANN001 Missing type annotation for function argument `connection_id`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:46: ANN001 Missing type annotation for function argument `hostname`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:103:5: ANN201 Missing return type annotation for public function `configure_hive_connection`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:110:10: COM812 [*] Trailing comma missing
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:117:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:1:1: CPY001 Missing copyright notice at top of file
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:52:27: ANN001 Missing type annotation for function argument `table_name`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:52:5: ANN201 Missing return type annotation for public function `create_dynamodb_table`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:72:29: ANN001 Missing type annotation for function argument `table_name`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:72:5: ANN201 Missing return type annotation for public function `get_dynamodb_item_count`
- tests/system/providers/amazon/aws/example_hive_to_dynamodb.py:86:5: T201 `print` found
... 2 additional changes omitted for project

bokeh/bokeh (+0 -44 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

- src/bokeh/server/views/metadata_handler.py:12:1: E265 Block comment should start with `# `
- src/bokeh/server/views/metadata_handler.py:14:1: E265 Block comment should start with `# `
- src/bokeh/server/views/metadata_handler.py:17:15: E261 [*] Insert at least two spaces before an inline comment
- src/bokeh/server/views/metadata_handler.py:1:1: E265 Block comment should start with `# `
- src/bokeh/server/views/metadata_handler.py:20:1: E265 Block comment should start with `# `
- src/bokeh/server/views/metadata_handler.py:22:1: E265 Block comment should start with `# `
- src/bokeh/server/views/metadata_handler.py:25:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:28:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:31:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:32:1: E402 Module level import not at top of file
- src/bokeh/server/views/metadata_handler.py:34:1: E265 Block comment should start with `# `
... 11 additional changes omitted for rule E265
- src/bokeh/server/views/metadata_handler.py:39:5: Q000 [*] Single quotes found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:51:5: D200 One-line docstring should fit on one line
- src/bokeh/server/views/metadata_handler.py:51:5: D210 [*] No whitespaces allowed surrounding docstring text
- src/bokeh/server/views/metadata_handler.py:51:5: D300 [*] Use triple double quotes `"""`
- src/bokeh/server/views/metadata_handler.py:51:5: D400 First line should end with a period
- src/bokeh/server/views/metadata_handler.py:51:5: D415 First line should end with a period, question mark, or exclamation point
- src/bokeh/server/views/metadata_handler.py:51:5: Q002 [*] Single quote docstring found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:56:15: ANN201 Missing return type annotation for public function `get`
- src/bokeh/server/views/metadata_handler.py:56:15: D102 Missing docstring in public method
- src/bokeh/server/views/metadata_handler.py:56:19: ANN101 Missing type annotation for `self` in method
- src/bokeh/server/views/metadata_handler.py:56:26: ANN002 Missing type annotation for `*args`
- src/bokeh/server/views/metadata_handler.py:56:26: ARG002 Unused method argument: `args`
- src/bokeh/server/views/metadata_handler.py:56:34: ANN003 Missing type annotation for `**kwargs`
- src/bokeh/server/views/metadata_handler.py:56:34: ARG002 Unused method argument: `kwargs`
- src/bokeh/server/views/metadata_handler.py:64:20: C408 Unnecessary `dict` call (rewrite as a literal)
- src/bokeh/server/views/metadata_handler.py:66:41: Q000 [*] Single quotes found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:7:1: D205 1 blank line required between summary line and description
- src/bokeh/server/views/metadata_handler.py:7:1: D210 [*] No whitespaces allowed surrounding docstring text
- src/bokeh/server/views/metadata_handler.py:7:1: D300 [*] Use triple double quotes `"""`
- src/bokeh/server/views/metadata_handler.py:7:1: D400 First line should end with a period
- src/bokeh/server/views/metadata_handler.py:7:1: D415 First line should end with a period, question mark, or exclamation point
- src/bokeh/server/views/metadata_handler.py:7:1: Q002 [*] Single quote docstring found but double quotes preferred
- src/bokeh/server/views/metadata_handler.py:8:1: D208 [*] Docstring is over-indented
... 10 additional changes omitted for project

Changes by rule (24 rules affected)

code total + violation - violation + fix - fix
E265 16 0 16 0 0
ANN001 5 0 5 0 0
ANN201 5 0 5 0 0
E402 4 0 4 0 0
Q000 2 0 2 0 0
D210 2 0 2 0 0
D300 2 0 2 0 0
D400 2 0 2 0 0
D415 2 0 2 0 0
Q002 2 0 2 0 0
ARG002 2 0 2 0 0
COM812 1 0 1 0 0
DTZ001 1 0 1 0 0
CPY001 1 0 1 0 0
T201 1 0 1 0 0
E261 1 0 1 0 0
D200 1 0 1 0 0
D102 1 0 1 0 0
ANN101 1 0 1 0 0
ANN002 1 0 1 0 0
ANN003 1 0 1 0 0
C408 1 0 1 0 0
D205 1 0 1 0 0
D208 1 0 1 0 0

@charliermarsh charliermarsh added the isort Related to import sorting label Nov 27, 2023
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@charliermarsh charliermarsh enabled auto-merge (squash) November 28, 2023 05:50
@charliermarsh charliermarsh changed the title [isort] Length sort settings [isort] Add support for length-sort settings Nov 28, 2023
@charliermarsh charliermarsh merged commit 578ddf1 into astral-sh:main Nov 28, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isort Related to import sorting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add Length-Sort config to isort
2 participants