pytestarch icon indicating copy to clipboard operation
pytestarch copied to clipboard

Checking for external import raises NetworkXError

Open GitRon opened this issue 8 months ago • 3 comments

Hi there!

I'm really super excited about this package but I'm struggling a little with the usage 😅

I've created this rule (for my Django project):

from django.conf import settings
from pytestarch import Rule
from pytestarch.pytestarch import get_evaluable_architecture

evaluable = get_evaluable_architecture(settings.BASE_DIR.parent, settings.BASE_DIR, exclude_external_libraries=False)


def test_views_must_not_import_django_db():
    Rule().modules_that().are_named("apps.common.views.*").should_not().import_modules_that().are_named(
        "django.db.*"
    ).assert_applies(evaluable)

I get this result:

networkx.exception.NetworkXError: The node django.db.* is not in the digraph.

I double-checked the docs and the issue tracker here but I couldn't find a solution why this error is being raised. Especially, since I'm explicitly enabling external deps via exclude_external_libraries.

Thanks and best from Cologne Ronny

GitRon avatar Apr 15 '25 11:04 GitRon

Hi @GitRon, apologies for the very! delayed response.

If it's still relevant, I believe the problem lies in the rule definition: When you use are_named, then the tool searches for a module literally named "django.db.*". Which doesn't exist of course, because of the wildcard.

You could try simple "django.db", that includes submodules. If you do want to use regex-patterns, replace are_named with have_name_matching. But in this case it might not be necessary, as mentioned.

zyskarch avatar Aug 08 '25 12:08 zyskarch

Hi @zyskarch!

Thanks for the clarification! Maybe worth making the error message a little bit clearer? 🤔

Best regards Ronny

GitRon avatar Aug 14 '25 08:08 GitRon

Hi @GitRon,

totally agree, I will add one/change it! Thanks for the suggestion 👍

zyskarch avatar Aug 22 '25 05:08 zyskarch