Skip to content

Python: from app import x links to a project variable named app, not the app package #2584

Closed

Description

@inth3shadows

Summary

In Python, from app import email creates an imports edge from the importing file to any project variable named app, such as the usual FastAPI/Flask app = FastAPI() in app/main.py. The edge is exact-match at 0.9. The package itself (app/__init__.py) gets no edge unless the file also has a separate import app. Every test or module that imports from a package named like a variable is then reported as a dependent of that variable's file.

Reproduction (verified)

On main at 8fe3156, built from source, Linux (WSL2), Node 22.23.2, WASM extractor (CODEGRAPH_KERNEL=0). Four files:

# app/__init__.py
(empty)
# app/email.py
def send():
    return 1
# app/main.py
app = object()
# tests/test_email.py
from app import email


def test_send():
    email.send()

codegraph init -i, then the imports / calls edges (target kind included):

tests/test_email.py -imports-> variable app [app/main.py:1] @1 exact-match/0.9
tests/test_email.py -imports-> file app/email.py [app/email.py:1] @1 import/0.9
test_send -calls-> function send [app/email.py:1] @5 import/0.85

Expected: no edge to the variable app in app/main.py. tests/test_email.py imports the package app, so if the module-name ref links anywhere it should be app/__init__.py.

Variants, same build:

Change Edge for the from app module name
as above -> variable app [app/main.py:1], exact-match/0.9
app/main.py is x = object() instead none
variable moved to server.py at the root (app = object()) -> variable app [server.py:1], exact-match/0.9
import app added above the from app import email line -> file app/__init__.py from both lines, import/0.9

The email edge and the send call are correct in every row.

Likely cause (inferred from reading the code)

A ref that names the module of a from X import statement is a module path, so it could resolve to a file or package or not at all, and never by symbol name.

Related

Not checked

  • Dotted module names (from app.api import routes).
  • Relative imports (from . import x, from .. import x).
  • A function or class named like the package, instead of a variable.
  • The native kernel extractor (only CODEGRAPH_KERNEL=0 was run).
  • Windows and macOS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions