Skip to content

Commit 4ba349a

Browse files
shahar1xvega
authored andcommitted
Stop defaulting PsrpOperator task_id to cmdlet (apache#73036)
cmdlet is a template field, rendered after the constructor runs, so deriving task_id from it in __init__ read the un-rendered Jinja value. It was the last construction-time read keeping PsrpOperator on the validate-operators-init exemption list, and it cannot move to execute() because task_id must exist before BaseOperator is initialised. PR apache#70347 deliberately kept the default because dropping it changes task identity for Dags that relied on it; this is the separate follow-up it deferred. Users keep the same task_id by passing it explicitly with the cmdlet name, as the changelog note describes. related: apache#70296 Generated-by: Claude Fable 5.1
1 parent 3090905 commit 4ba349a

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎providers/microsoft/psrp/docs/changelog.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
Changelog
2828
---------
2929

30+
Breaking changes
31+
~~~~~~~~~~~~~~~~
32+
33+
* ``Stop defaulting PsrpOperator task_id to cmdlet``
34+
35+
``PsrpOperator`` no longer uses ``cmdlet`` as the default ``task_id``; ``task_id`` is now required, as
36+
on every other operator. ``cmdlet`` is a template field rendered after the constructor runs, so the
37+
default read the un-rendered value. To keep the existing task identity (history, logs, XComs), pass
38+
``task_id`` explicitly with the same value as ``cmdlet``, for example
39+
``PsrpOperator(task_id="Get-Process", cmdlet="Get-Process", psrp_conn_id="psrp_default")``.
40+
3041
3.2.7
3142
.....
3243

‎providers/microsoft/psrp/src/airflow/providers/microsoft/psrp/operators/psrp.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ class PsrpOperator(BaseOperator):
5555
:param psrp_conn_id: connection id
5656
:param command: command to execute on remote host. (templated)
5757
:param powershell: powershell to execute on remote host. (templated)
58-
:param cmdlet:
59-
cmdlet to execute on remote host (templated). Also used as the default
60-
value for `task_id`.
58+
:param cmdlet: cmdlet to execute on remote host (templated).
6159
:param arguments:
6260
When using the `cmdlet` or `powershell` option, use `arguments` to
6361
provide arguments (templated).
@@ -112,8 +110,6 @@ def __init__(
112110
raise ValueError("Arguments only allowed with 'powershell' or 'cmdlet'")
113111
if parameters is not None and powershell is None and cmdlet is None:
114112
raise ValueError("Parameters only allowed with 'powershell' or 'cmdlet'")
115-
if cmdlet:
116-
kwargs.setdefault("task_id", cmdlet)
117113
super().__init__(**kwargs)
118114
self.conn_id = psrp_conn_id
119115
self.command = command

‎providers/microsoft/psrp/tests/unit/microsoft/psrp/operators/test_psrp.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def test_invalid_option_combination(self, kwargs, match):
6868
def test_empty_option_counts_as_provided(self, kwargs):
6969
PsrpOperator(task_id="test_task_id", psrp_conn_id=CONNECTION_ID, **kwargs)
7070

71-
def test_cmdlet_task_id_default(self):
72-
operator = PsrpOperator(cmdlet="Invoke-Foo", psrp_conn_id=CONNECTION_ID)
73-
assert operator.task_id == "Invoke-Foo"
71+
def test_cmdlet_requires_explicit_task_id(self):
72+
with pytest.raises(TypeError, match="task_id"):
73+
PsrpOperator(cmdlet="Invoke-Foo", psrp_conn_id=CONNECTION_ID)
7474

7575
@patch(f"{PsrpOperator.__module__}.PsrpHook")
7676
def test_command_rendering_to_empty_dispatches_as_command(self, hook_impl):
@@ -152,14 +152,14 @@ def test_execute(self, hook_impl, parameter, had_errors, rc, do_xcom_push):
152152
assert ps.mock_calls == expected_ps_calls
153153

154154
def test_securestring_sandboxed(self):
155-
op = PsrpOperator(psrp_conn_id=CONNECTION_ID, cmdlet="test")
155+
op = PsrpOperator(task_id="test", psrp_conn_id=CONNECTION_ID, cmdlet="test")
156156
template = op.get_template_env().from_string("{{ 'foo' | securestring }}")
157157
with pytest.raises(AirflowException):
158158
template.render()
159159

160160
@patch.object(BaseOperator, "get_template_env")
161161
def test_securestring_native(self, get_template_env):
162-
op = PsrpOperator(psrp_conn_id=CONNECTION_ID, cmdlet="test")
162+
op = PsrpOperator(task_id="test", psrp_conn_id=CONNECTION_ID, cmdlet="test")
163163
get_template_env.return_value = NativeEnvironment()
164164
template = op.get_template_env().from_string("{{ 'foo' | securestring }}")
165165
rendered = template.render()

‎scripts/ci/prek/validate_operators_init_exemptions.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ providers/google/src/airflow/providers/google/cloud/operators/functions.py::Clou
1616
providers/google/src/airflow/providers/google/cloud/operators/gcs.py::GCSFileTransformOperator
1717
providers/google/src/airflow/providers/google/cloud/sensors/bigquery_dts.py::BigQueryDataTransferServiceTransferRunSensor
1818
providers/google/src/airflow/providers/google/cloud/sensors/cloud_composer.py::CloudComposerExternalTaskSensor
19-
providers/microsoft/psrp/src/airflow/providers/microsoft/psrp/operators/psrp.py::PsrpOperator

0 commit comments

Comments
 (0)