import-linter icon indicating copy to clipboard operation
import-linter copied to clipboard

Forbid self imports

Open sobolevn opened this issue 6 years ago • 2 comments

There's a very useful feature in pylint called self-import. What it does?

It disallows to write code as this one:

# ex.py
import ex  # self import
from ex import *  # self import

print('init!')

I guess it might be a good idea to forbid this kind of imports by import-linter.

I have tried to implement it inside wemake-python-styleguide, but I am not sure that it is the best place to do it. Original: https://github.com/wemake-services/wemake-python-styleguide/issues/699

What do you think?

sobolevn avatar Jul 31 '19 20:07 sobolevn

Hi Nikita,

Thanks, as always, for the suggestion!

I'm unclear about whether or not self imports are a real problem. How often does this kind of thing actually happen? I'd be interested to hear more on this.

In terms of whether it's right for Import Linter, I feel the main focus should be on architecture, focusing on the dependency relationships between modules. The import statements themselves are more of a proxy for those relationships. I'm leaning towards not including linting for style, or even straightforward mistakes like this. I reckon that kind of thing could probably be done by a different kind of tool, that doesn't need the overhead of building an entire dependency graph.

Happy to hear more about it though.

seddonym avatar Aug 03 '19 14:08 seddonym

My guess is that import-linter is about imports 🙂 However, I like the focus of this library. It is unique and useful.

Considering self-imports: I have never seen one in a real-life code. But, the ability to do it seems like a possibility for all kinds of mistakes (including possible ones in the import graph, because they create self-cycles).

I guess that some sanity-checking is a good thing. Like:

  1. self imports
  2. explicit import cycles

sobolevn avatar Aug 03 '19 17:08 sobolevn

I'm going to close this ticket as it's quite old, and I think that it's best to keep import linter architecture-focused. I think it is probably possible to support this via a custom contract type, which could always be released as a separate package.

seddonym avatar Aug 15 '22 09:08 seddonym