Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/entities/entity_encoder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from falkordb import Node, Edge, Path

def encode_node(n: Node) -> dict:
n.labels.remove('Searchable')
return vars(n)
result = vars(n).copy()
result['labels'] = [l for l in n.labels if l != 'Searchable']
return result

def encode_edge(e: Edge) -> dict:
return vars(e)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_entity_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import importlib.util
import sys
import os
import pytest
from falkordb import Node

# Import entity_encoder directly without triggering api/__init__.py
_spec = importlib.util.spec_from_file_location(
"entity_encoder",
os.path.join(os.path.dirname(__file__), "..", "api", "entities", "entity_encoder.py"),
)
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod)
encode_node = _mod.encode_node
Comment on lines +7 to +14
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check what api/__init__.py does that would cause issues when importing entity_encoder
cat api/__init__.py 2>/dev/null || echo "File not found"

# Check if other test files also use this pattern
rg -l "importlib.util.spec_from_file_location" tests/

Repository: FalkorDB/code-graph

Length of output: 336


🏁 Script executed:

# Check what api/entities/__init__.py does
cat api/entities/__init__.py 2>/dev/null || echo "File not found"

# Check the test file itself to understand the context
head -30 tests/test_entity_encoder.py

# Check if there are any initialization or side effects in the modules imported by api/__init__.py
ls -la api/*.py

Repository: FalkorDB/code-graph

Length of output: 1641


🏁 Script executed:

# Check entity_encoder.py to see what it actually imports
head -30 api/entities/entity_encoder.py

# Check if graph.py or llm.py have problematic initialization
grep -n "^import\|^from" api/graph.py | head -20
grep -n "^import\|^from" api/llm.py | head -20

# Check api/info.py as it's imported first
head -20 api/info.py

Repository: FalkorDB/code-graph

Length of output: 1614


🏁 Script executed:

# Check if api/info.py has module-level code that executes connections
cat api/info.py

# Also check if any other modules in api/ might have initialization code
grep -n "redis\|Redis\|connection\|connect" api/info.py

Repository: FalkorDB/code-graph

Length of output: 5453


Remove the importlib workaround and use standard imports instead.

The dynamic import pattern is unnecessary. entity_encoder.py only depends on falkordb (imports Node, Edge, Path), and api/__init__.py has no problematic side effects at import time—it merely imports modules and calls logging.basicConfig(). The Redis connection logic in api/info.py only executes when functions are called, not during module import.

Use: from api.entities.entity_encoder import encode_node

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_entity_encoder.py` around lines 7 - 14, Replace the dynamic import
block that loads entity_encoder via importlib with a normal import: use from
api.entities.entity_encoder import encode_node; update references that currently
use encode_node from the _mod variable to use the direct import, and ensure
entity_encoder.py (and its dependencies Node/Edge/Path from falkordb) continue
to import normally; you can ignore api/__init__.py and api/info.py side effects
because their logging/Redis logic does not run on import.



def make_node(labels, props=None):
return Node(node_id=1, alias="n", labels=labels, properties=props or {})


def test_encode_node_removes_searchable():
n = make_node(['File', 'Searchable'])
result = encode_node(n)
assert 'Searchable' not in result['labels']
assert 'File' in result['labels']


def test_encode_node_does_not_mutate_original():
n = make_node(['File', 'Searchable'])
encode_node(n)
assert 'Searchable' in n.labels


def test_encode_node_twice_does_not_raise():
n = make_node(['File', 'Searchable'])
encode_node(n)
# Second call must not raise ValueError
result = encode_node(n)
assert 'Searchable' not in result['labels']


def test_encode_node_without_searchable():
n = make_node(['Class'])
result = encode_node(n)
assert result['labels'] == ['Class']