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

Improve auto-import sorting rules to be better compatible with isort #1675

Closed
cdce8p opened this issue Aug 15, 2021 · 5 comments
Closed

Improve auto-import sorting rules to be better compatible with isort #1675

cdce8p opened this issue Aug 15, 2021 · 5 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@cdce8p
Copy link

cdce8p commented Aug 15, 2021

Pylance: v2021.8.1

--
It would be great if Auto-Import could use the same default sorting rules as isort when adding a new import. That way the Sort Imports command wouldn't reorder them. In particular full support for the order-by-type option, i.e.

  1. CONSTANT_VARIABLE
  2. CamelCaseClass
  3. variable_or_function

https://pycqa.github.io/isort/docs/configuration/options.html#order-by-type

Current Example

# lib.py

DEVICE_CLASS_BATTERY = "battery"
DEVICE_CLASS_MOTION = "motion"

def a_func(): ...
def a_func2(): ...

class BinarySensorEntity: ...
class BinarySensorEntityDescription: ...
# sensor.py

from lib import (
    DEVICE_CLASS_BATTERY,
)

DEVICE_CLASS_BATTERY
# using Auto-Import for the following symbols

DEVICE_CLASS_MOTION
BinarySensorEntity
BinarySensorEntityDescription
a_func
# results in

from lib import (
    BinarySensorEntity,
    BinarySensorEntityDescription,
    DEVICE_CLASS_BATTERY,
    DEVICE_CLASS_MOTION,
    a_func,
)

Here BinarySensorEntity and BinarySensorEntityDescription should be put after DEVICE_CLASS_MOTION.

@jakebailey
Copy link
Member

Are you looking for us to read an isort configuration, or just change our behavior to more closely match isort's defaults? In general, we don't read configurations of other tools.

Note that our current behavior is similar to TypeScript's, where it inserts in a specific location, but then it's up to a formatter/linter to change it on save.

@jakebailey jakebailey added the waiting for user response Requires more information from user label Aug 16, 2021
@github-actions github-actions bot removed the triage label Aug 16, 2021
@erictraut
Copy link
Contributor

I would guess that most developers use the default settings for isort, so I think it's reasonable that we should adopt that behavior. That was my intent when I wrote the logic that determines where pylance inserts an auto-import symbol, but I didn't realize at that time that isort treated all-caps symbols differently. It would be an easy fix.

@jakebailey
Copy link
Member

If that's the default in isort, then yeah, I think we should make it match. The docs seem to imply that it's on by default.

@cdce8p
Copy link
Author

cdce8p commented Aug 16, 2021

Obviously reading the config would be great, but that is not really necessary.
I think just matching the default would be great for most devs.

@jakebailey jakebailey added enhancement New feature or request and removed waiting for user response Requires more information from user labels Aug 16, 2021
@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 17, 2021
@heejaechang
Copy link
Contributor

it should be fixed in 2021.8.2 released today (2021.8.19)

@heejaechang heejaechang removed the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 19, 2021
@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants