Import all (*) from modules better be positioned before explicit local imports #1504
Labels
enhancement
New feature or request
help wanted
Extra attention is needed
idea_for_next_major_release
Milestone
In general, it's recommended to not use "import all" pattern, or at least not without explicit
__all__
defined.However, many code-bases still do so, for good reasons, such as reducing friction in adding new items to a shared library.
Now, assuming we're okay with
import *
in general, and specificallyfrom _<dir-name> import *
forms, the example below demonstrates a problem when havingimport *
statement at the end of the local-imports list, instead of first.Here's the structure of the package
bar
, which has an internal module_foo
, as well as module_bar
that's considered the "main" module of the package.The file contents are like:
bar/__init__.py
:bar/_bar.py
:bar/_foo.py
:In this set up, the class
Any
fromtyping
is overshadowing the classAny
defined inbar/_foo.py
, and re-export explicitly as part of the "public API" of packagebar
.If we change keep
import *
statements at the beginning of local-imports block, we would prevent such implicit overrides from external imports, reducing friction for developers.So,
isort
would modifybar/__init__.py
to look like:I don't know if this has been discussed before (really hard to search for terms "import" and "all" in this repo), but like to suggest updating
isort
to change the behavior here.What do you think?
The text was updated successfully, but these errors were encountered: