Skip to content

Commit

Permalink
Ensure tags don't run into index errors when there are no upstream no…
Browse files Browse the repository at this point in the history
…des (astronomer#933)

This PR ensures we only try to access `node.depends_on[0]` if it is an iterable with items.

Co-authored-by: Tatiana Al-Chueyr <tatiana.alchueyr@gmail.com>
  • Loading branch information
2 people authored and arojasb3 committed Jul 14, 2024
1 parent 3fb8976 commit 95d0d72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cosmos/dbt/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _should_include_node(self, node_id: str, node: DbtNode) -> bool:

self.visited_nodes.add(node_id)

if node.resource_type == DbtResourceType.TEST and node.depends_on:
if node.resource_type == DbtResourceType.TEST and node.depends_on and len(node.depends_on) > 0:
node.tags = getattr(self.nodes.get(node.depends_on[0]), "tags", [])
logger.debug(
"The test node <%s> inherited these tags from the parent node <%s>: %s",
Expand Down
16 changes: 15 additions & 1 deletion tests/dbt/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from cosmos.constants import DbtResourceType
from cosmos.dbt.graph import DbtNode
from cosmos.dbt.selector import SelectorConfig, select_nodes
from cosmos.dbt.selector import NodeSelector, SelectorConfig, select_nodes
from cosmos.exceptions import CosmosValueError

SAMPLE_PROJ_PATH = Path("/home/user/path/dbt-proj/")
Expand Down Expand Up @@ -418,3 +418,17 @@ def test_node_without_depends_on_with_tag_selector_should_not_raise_exception():
)
nodes = {standalone_test_node.unique_id: standalone_test_node}
assert not select_nodes(project_dir=SAMPLE_PROJ_PATH, nodes=nodes, select=["tag:some-tag"])


def test_should_include_node_without_depends_on(selector_config):
node = DbtNode(
unique_id=f"{DbtResourceType.TEST.value}.{SAMPLE_PROJ_PATH.stem}.standalone",
resource_type=DbtResourceType.TEST,
depends_on=None,
tags=[],
config={},
file_path=SAMPLE_PROJ_PATH / "tests/generic/builtin.sql",
)
selector = NodeSelector({}, selector_config)
selector.visited_nodes = set()
selector._should_include_node(node.unique_id, node)

0 comments on commit 95d0d72

Please sign in to comment.