isort icon indicating copy to clipboard operation
isort copied to clipboard

Possibility to locally turn off `remove_redundent_aliases`

Open bluenote10 opened this issue 3 years ago • 2 comments

In general remove_redundent_aliases is a nice setting, and it makes sense to have it enabled across a large code base in general.

However, type checkers use redundant module or redundant symbol aliases (see second bullet point) to communicate information about whether a symbol should be considered private or public by the type checker. This feature is quite convenient, because it can avoid having to double-maintain the good old __all__ list.

This means that turning off remove_redundent_aliases is valuable for package-level __init__.py that deal with symbol forwarding to the outside. But turning of remove_redundent_aliases globally just for the usages in __init__.py's would allow truely redundant aliases to sneak in where they aren't meaningful. Alternatively, using # isort: off ... # isort: on wrapping fails to leverage isort for the its main purpose of sorting all the imports. So for a large __init__.py turning off isort entirely is also not ideal.

For these reasons it would be great if the redundant alias feature could be turned off locally via an action, e.g.:

# isort: remove-redundent-aliases-off
from . import PublicThingA as PublicThingA
from . import PublicThingB as PublicThingB
from . import PrivateThing
# isort: remove-redundent-aliases-on

bluenote10 avatar Oct 19 '23 14:10 bluenote10

I’m working on adding local action comments to preserve redundant aliases while keeping import sorting enabled, with regression tests and documentation.

anshurajbisoyi98-ctrl avatar Oct 03 '26 07:10 anshurajbisoyi98-ctrl

__all__ is what the community has moved towards. I can understand why you'd want this, but considering the code necessary in https://github.com/PyCQA/isort/pull/2706/ I don't think this is worth the effort. Sorry!

DanielNoord avatar Oct 15 '26 19:10 DanielNoord