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

Relative import management #1014

Open
noirbizarre opened this issue Dec 3, 2022 · 6 comments
Open

Relative import management #1014

noirbizarre opened this issue Dec 3, 2022 · 6 comments
Labels
plugin Implementing a known but unsupported plugin

Comments

@noirbizarre
Copy link

This topic has always been a bit painful to manage with isort or absolufy-imports and given I am migrating all my project to ruff I would love to be able to tune the relative imports ahndling:

  • basic case (the one I use everyday): force relative imports for siblings and children only (with auto-fix support) (ie. .. starting imports)
  • allow parent imports on a package basis: mark a package to be allowed as a relative-root reference by children packages/modules
  • default: prevent relative

Is it something acceptable ?

Note: I am motivated enough to contribute but not yet skilled enough in Rust to make this happen soon

@charliermarsh
Copy link
Member

Yeah, I think so... Right now, we support the "forbid relative imports of parent modules" rule from flake8-tidy-imports. But adding autofix to that rule would probably mean converting relative to absolute imports (basically points 2 and 3 on your list: prevent relative imports, rewrite them as absolute). So in addition, we'd need a new rule that's basically the inverse: prevent absolute imports in certain cases (with autofix).

Does that sound right?

Is there any tool or combination of tools you use today to enforce this?

@charliermarsh charliermarsh added the question Asking for support or clarification label Dec 3, 2022
@noirbizarre
Copy link
Author

noirbizarre commented Dec 5, 2022

I think the autofix should be both ways for case 1:

  • rewrite relative to absolute imports for the forbidden cases
  • rewrite absolute to relative imports for siblings and children packages

For this case I am using absolufy-import with --never option but it is far from perfect and I have a lot of manual fixes to handle.

For case 3: This one should be the default: forbid relative imports and rewrite them as absolute as autofix. This is basically the deault behavior of absolufy-imports

Case 2 is bonus to me but I have some cases of child-to-parent dependencies I want to keep relative. The idea it to flag a package as relative-root for its children using by-package Ruff config syntax and then autofix should:

  • Apply the same autofix as case 1 for siblings
  • rewrite absolute imports to relative for children referencing this flagged parent (this is the point differing from case 1)
  • keep/rewrite imports as absolute for those not matching this case

I took a look at flake8-tidy-imports: it is very close to the expected behavior but:

  • cannot enforce relative/sibling imports
  • does not autofix

@charliermarsh charliermarsh added plugin Implementing a known but unsupported plugin and removed question Asking for support or clarification labels Dec 31, 2022
@sbrugman
Copy link
Contributor

Note that flake8-tidy-imports now has partial autofix

@lsorber
Copy link

lsorber commented Feb 24, 2023

Which cases are not covered by the flake8-tidy-imports autofix @sbrugman?

@noirbizarre
Copy link
Author

noirbizarre commented Apr 20, 2023

I submitted a PR for my missing case as well as a matching one on flake8-tidy-import. I will adapt the PR depending on the review and feedback on the flake8-tidy-import repository.

@pawamoy
Copy link

pawamoy commented Feb 5, 2024

Sorry if this is not the right place to ask: the docs (https://docs.astral.sh/ruff/rules/relative-imports/) say that

Fix is sometimes available.

When is that autofix available exactly? I'm looking at automatically converting relative imports to absolute ones. I have the relevant rule enabled:

[flake8-tidy-imports]
ban-relative-imports = "all"

Is such an autofix planned/accepted in Ruff?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin Implementing a known but unsupported plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants