Skip to content

Commit b1939ff

Browse files
fix(db): resolve get_by_tags shadowing causing AttributeError
Rename the loop variable so an empty get_nodes() result does not overwrite the DB node used in the fatal error path. Add regression test. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 2d1aa47 commit b1939ff

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

application/database/db.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,23 +1175,23 @@ def get_by_tags(self, tags: List[str]) -> List[cre_defs.Document]:
11751175
cre_where_clause.append(sqla.and_(CRE.tags.like("%{}%".format(tag))))
11761176

11771177
nodes = Node.query.filter(*nodes_where_clause).all() or []
1178-
for node in nodes:
1179-
node = self.get_nodes(
1180-
name=node.name,
1181-
section=node.section,
1182-
subsection=node.subsection,
1183-
version=node.version,
1184-
link=node.link,
1185-
ntype=node.ntype,
1186-
sectionID=node.section_id,
1178+
for db_node in nodes:
1179+
resolved = self.get_nodes(
1180+
name=db_node.name,
1181+
section=db_node.section,
1182+
subsection=db_node.subsection,
1183+
version=db_node.version,
1184+
link=db_node.link,
1185+
ntype=db_node.ntype,
1186+
sectionID=db_node.section_id,
11871187
)
1188-
if node:
1189-
documents.extend(node)
1188+
if resolved:
1189+
documents.extend(resolved)
11901190
else:
11911191
logger.fatal(
1192-
"db.get_node returned None for"
1192+
"db.get_node returned None for "
11931193
"Node %s:%s:%s that exists, BUG!"
1194-
% (node.name, node.section, node.section_id)
1194+
% (db_node.name, db_node.section, db_node.section_id)
11951195
)
11961196

11971197
cres = CRE.query.filter(*cre_where_clause).all() or []

application/tests/db_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ def test_get_by_tags(self) -> None:
125125
self.assertEqual(self.collection.get_by_tags([]), [])
126126
self.assertEqual(self.collection.get_by_tags(["this should not be a tag"]), [])
127127

128+
@patch("application.database.db.logger")
129+
def test_get_by_tags_empty_nodes_regression(self, mock_logger) -> None:
130+
"""
131+
Simulate get_nodes returning an empty list to test the error path in get_by_tags.
132+
See PR #837 regression fix.
133+
"""
134+
dbstandard = db.Node(
135+
section="regression_section",
136+
name="regression_name",
137+
tags="regression_tag",
138+
ntype=defs.Standard.__name__,
139+
)
140+
self.collection.session.add(dbstandard)
141+
self.collection.session.commit()
142+
143+
with patch.object(self.collection, "get_nodes", return_value=[]):
144+
res = self.collection.get_by_tags(["regression_tag"])
145+
self.assertEqual(res, [])
146+
mock_logger.fatal.assert_called_once()
147+
128148
def test_get_standards_names(self) -> None:
129149
result = self.collection.get_node_names()
130150
expected = [("Standard", "BarStand"), ("Standard", "Unlinked")]

0 commit comments

Comments
 (0)