Skip to content

Commit dbb5c7f

Browse files
authored
Fail Neo4jOperator argument mistakes at Dag parse time (#70508)
Whether `cypher` or `sql` was passed is a property of how the Dag was written, not of the rendered value, and the constructor is the only place that can read it: under render_template_as_native_obj a supplied field can render to None, so the same check in execute reports an argument the author did provide as missing. Checking at construction also surfaces the mistake as a Dag import error instead of once per task instance. The execute guard stays: a passed field can still render to None and the hook needs a query string. related: #70503
1 parent abd837a commit dbb5c7f

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎providers/neo4j/src/airflow/providers/neo4j/operators/neo4j.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def __init__(
5757
**kwargs,
5858
) -> None:
5959
super().__init__(**kwargs)
60+
if sql is not None and cypher is not None:
61+
raise ValueError("Cannot provide both `sql` and `cypher`. Use `cypher` only.")
62+
if cypher is None and sql is None:
63+
raise ValueError("Parameter `cypher` is required.")
6064
self.neo4j_conn_id = neo4j_conn_id
6165
self.cypher = cypher
6266
self.sql = sql
@@ -70,10 +74,10 @@ def execute(self, context: Context) -> None:
7074
AirflowProviderDeprecationWarning,
7175
stacklevel=2,
7276
)
73-
if cypher is not None:
74-
raise ValueError("Cannot provide both `sql` and `cypher`. Use `cypher` only.")
7577
cypher = self.sql
7678
if cypher is None:
79+
# The constructor only sees whether the argument was passed; a passed field can still
80+
# render to None, and the hook needs a query string.
7781
raise ValueError("Parameter `cypher` is required.")
7882

7983
self.log.info("Executing: %s", cypher)

‎providers/neo4j/tests/unit/neo4j/operators/test_neo4j.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ def test_neo4j_operator_sql_param_is_deprecated(self, mock_hook):
6666
op.execute(mock.MagicMock())
6767
mock_hook.return_value.run.assert_called_once_with(cypher, None)
6868

69-
def test_neo4j_operator_both_sql_and_cypher_raises_on_execute(self):
70-
op = Neo4jOperator(task_id="basic_neo4j", sql="a", cypher="b")
69+
def test_neo4j_operator_both_sql_and_cypher_raises(self):
70+
with pytest.raises(ValueError, match="Cannot provide both `sql` and `cypher`"):
71+
Neo4jOperator(task_id="basic_neo4j", sql="a", cypher="b")
7172

72-
with pytest.warns(AirflowProviderDeprecationWarning):
73-
with pytest.raises(ValueError, match="Cannot provide both `sql` and `cypher`"):
74-
op.execute(mock.MagicMock())
73+
def test_neo4j_operator_missing_cypher_raises(self):
74+
with pytest.raises(ValueError, match="Parameter `cypher` is required."):
75+
Neo4jOperator(task_id="basic_neo4j")
7576

76-
def test_neo4j_operator_missing_cypher_raises_on_execute(self):
77-
op = Neo4jOperator(task_id="basic_neo4j")
77+
def test_neo4j_operator_cypher_rendering_to_none_raises_on_execute(self):
78+
op = Neo4jOperator(task_id="basic_neo4j", cypher="{{ missing }}")
79+
op.cypher = None
7880

7981
with pytest.raises(ValueError, match="Parameter `cypher` is required."):
8082
op.execute(mock.MagicMock())

0 commit comments

Comments
 (0)