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

Ruff rule to standardise import x.y.z as alias and from x.y import z as alias #15639

Open
epenet opened this issue Jan 21, 2025 · 7 comments
Open
Labels
rule Implementing or modifying a lint rule

Comments

@epenet
Copy link
Contributor

epenet commented Jan 21, 2025

Hi,

The current Home Assistant code base has a mix of import x.y.z as alias and from x.y import z as alias

This leads to funny aesthetics, for example:

from homeassistant.helpers import device_registry as dr
import homeassistant.helpers.issue_registry as ir

or

from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

It would be nice to have a rule to be able to enforce from x.y import z as alias, which will also unlock the grouping

from homeassistant.helpers import device_registry as dr, issue_registry as ir

or

from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

Current isort configuration:

[tool.ruff.lint.isort]
force-sort-within-sections = true
known-first-party = [
    "homeassistant",
]
combine-as-imports = true
split-on-trailing-comma = false
@epenet
Copy link
Contributor Author

epenet commented Jan 21, 2025

Note: these are linked PRs on HA code base

@MichaReiser
Copy link
Member

I think that overall makes sense but I'm not sure if it should go into isort or any of the other import-related linters.

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Jan 21, 2025
@AlexWaygood
Copy link
Member

Is this a duplicate of #14070?

@dhruvmanila
Copy link
Member

I think #14070 is different than what's being asked here in that the linked issue is requesting to maintain consistency between import ... and from ... style imports i.e., strictly use one or the other (IIUC reading the issue description).

@MichaReiser
Copy link
Member

requesting to maintain consistency between import ... and from ... style imports i.e., strictly use one or the other (IIUC reading the issue description

But isn't this the same as is requested in this issue 😆?

@dhruvmanila
Copy link
Member

I think @epenet might be able to provide more context here. Is the request here strictly about converting a import x.y as z to from x import y as z or does the conversion from import x.y to from x import y is also applicable here.

While writing this, I realized that considering both might be more useful and thus this can be marked as duplicate of #14070

@epenet
Copy link
Contributor Author

epenet commented Jan 22, 2025

I was not aware of #14070, and it is definitely related.
However, I think that #14070 is too broad as not all imports are equivalent, and you cannot always migrate in both directions between mixed <=> option 1 <=> option 2.

I'll try to add a comment there but maybe keep this open a little while longer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

4 participants