Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 27, 2025

Fixes #104 - Functions with module prefix are not mapped correctly when using imports like from infrastructure import http followed by calls like http.request().

Problem

When analyzing Python code with nested module imports, code2flow was not properly connecting function calls that use module prefixes. For example:

# infrastructure/http.py
def request(method, url):
    pass

# repository/user_repository.py  
from infrastructure import http
from infrastructure.http import request

def get_users():
    http.request("GET", "https://api.example.com/users")  # This call was not mapped

def get_users2():
    request("GET", "https://api.example.com/users")      # This call worked fine

Before the fix:

  • Variables showed: http->UNKNOWN_MODULE
  • Only direct imports were connected
  • Missing edges for module-prefixed calls like http.request()

Root Cause

The issue was in the _resolve_str_variable() function in model.py. When processing from infrastructure import http, it creates a variable with:

  • token = "http"
  • points_to = "infrastructure.http"

But the file infrastructure/http.py has import_tokens = ["http"]. The function only checked for exact matches between "infrastructure.http" and ["http"], which failed.

Solution

Enhanced _resolve_str_variable() to include a fallback check for module path matching:

# Handle module paths like "infrastructure.http" matching file with import_tokens ["http"]
for file_group in file_groups:
    for node in file_group.all_nodes():
        if any(variable.points_to.endswith('.' + ot) for ot in node.import_tokens):
            return node
    for group in file_group.all_groups():
        if any(variable.points_to.endswith('.' + ot) for ot in group.import_tokens):
            return group

This allows "infrastructure.http" to match files with import token "http" when the variable path ends with ".http".

Results

After the fix:

  • Variables now show: http->http (properly resolved)
  • All module-prefixed calls are correctly mapped
  • Complete call graph includes previously missing edges
  • Existing functionality remains unchanged

Testing

  • Added comprehensive test case module_prefix_imports to prevent regression
  • Verified existing tests still pass (two_file_simple, import_paths, etc.)
  • Tested edge cases to ensure no false positive matches
  • Full verification with the exact scenario from issue Functions with module prefix are not mapped (Python) #104

The fix is minimal, targeted, and maintains backward compatibility while solving the core mapping issue for module-prefixed function calls.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] @scottrogowski/code2flow/issues/104 Fix module prefix function mapping for Python imports Jul 27, 2025
@Copilot Copilot AI requested a review from scottrogowski July 27, 2025 17:44
Copilot finished work on behalf of scottrogowski July 27, 2025 17:44
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

Successfully merging this pull request may close these issues.

Functions with module prefix are not mapped (Python)
2 participants