import-linter
import-linter copied to clipboard
Limit imports only to symbols in __init__.py
Awesome package. I was about to build something until I found this! Congrats
I have a something that looks like this
- __init__.py
- a.py
- b.py
- c.py
- inner
- __init__.py
- inner1
- __init__.py
- a1.py
- b1.py
- c1.py
- inner2
- __init__.py
- a2.py
- b2.py
- c2py
and I am trying to restrict imports from inner1 and inner2 only to those imported in their respective __init__.py How can I achieve this?
Thanks for raising the issue!
I'm not totally clear on what you are trying to restrict, could you give an example?
As python doesn't have public/private modifiers, I want to use a linter to promote a certain type of organization. Defining a subpackage as any folder that has an __init__.py inside, I would like a contract that states that modules in a subpackage can only import:
- from modules in the same subpackage
- from the
__init__.pyof child subpackges.
In the example above:
__init__.py,a,b,cshould only import from each other and frominner/__init__.pyinner/__init__.pyshould only import frominner/inner1/__init__.pyandinner/inner2/__init__.pyinner/inner1/__init__.pyshould be able to import from each otherinner/inner2/__init__.py(as if it wasinner/inner1__init__.pyandinner/inner2__init__.py)
There should also be a way to exclude certain source and target files/folders from the rule (for example, from the testsuite I want to be able to import everything and maybe there is a compat module in the root that should be accesible to all.
Interesting! I think there is a lot of mileage in the idea of private modules - in fact we use a (slightly different) convention at work where modules prefixed with an underscore are deemed 'private', and can only be imported by their parent __init__.py file. At some point I plan to make a built in contract type for this.
I think the best way forward with your requirements is to create a custom contract. Let me know if anything in the documentation isn't clear.
I'm going to close this in favour of these issues:
- https://github.com/seddonym/import-linter/issues/159
- https://github.com/seddonym/import-linter/issues/158
Thanks! , I will take a look!