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

Query about forbidden contracts and direct imports #193

Open
seddonym opened this issue Sep 5, 2023 · 1 comment
Open

Query about forbidden contracts and direct imports #193

seddonym opened this issue Sep 5, 2023 · 1 comment

Comments

@seddonym
Copy link
Owner

seddonym commented Sep 5, 2023

Opening on behalf of @pwalsh (originally posted in different ticket).

Original post

With (snippets from pyproject.toml):

[tool.importlinter]
root_packages = ["survey", "api", "django"]
include_external_packages = true

[[tool.importlinter.contracts]]
name = "Dont use cache dependencies directly"
type = "forbidden"
source_modules = ["survey", "api"]
forbidden_modules = ["django.core.cache"]

I would expect django.core.cache to be forbidden, but, I get a huge list of import errors as any import under django.* is listed as an error.

If I change root_packages to either

root_packages = ["survey", "api", "django.core"]
#
root_packages = ["survey", "api", "django.core.cache"]

then no linting errors related to the Django package are detected.

I'd like to, for example, prevent usage of anything under django.core.cache by raising lint errors. is this possible some other way?

@seddonym
Copy link
Owner Author

seddonym commented Sep 5, 2023

I get a huge list of import errors as any import under django.* is listed as an error.

Is it possible that the problem might be because a lot of django modules indirectly depend on django.core.cache?

For example, on a sample project I set up, I included an import of django.db.models in survey.main. I get this contract failure:

----------------
Broken contracts
----------------

Dont use cache dependencies directly
------------------------------------

survey is not allowed to import django.core.cache:

-   survey.main -> django.db.models (l.1)
    django.db.models -> django.db.models.fields (l.39, l.38)
    django.db.models.fields -> django.core.checks (l.14)
    django.core.checks -> django.core.checks.caches (l.18)
    django.core.checks.caches -> django.core.cache (l.4)

-   survey.main -> django.db.models (l.1)
    django.db.models -> django.db.models.fields (l.39, l.38)
    django.db.models.fields -> django.core.checks (l.14)
    django.core.checks -> django.core.checks.caches (l.18)
    django.core.checks.caches -> django.core.cache.backends.filebased (l.5)

If you don't mind indirect imports, you could add allow_indirect_imports = true to the contract, i.e.

[[tool.importlinter.contracts]]
name = "Dont use cache dependencies directly"
type = "forbidden"
source_modules = ["survey", "api"]
forbidden_modules = ["django.core.cache"]
allow_indirect_imports = true

Does that solve your problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant