Skip to content

Validate PsrpOperator parameters after rendering - #70347

Open
1fanwang wants to merge 8 commits into
apache:mainfrom
1fanwang:fix-psrp-init
Open

Validate PsrpOperator parameters after rendering#70347
1fanwang wants to merge 8 commits into
apache:mainfrom
1fanwang:fix-psrp-init

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

command, powershell, cmdlet, arguments and parameters are template fields, so they are rendered after __init__ runs. The constructor validated their combination (exactly one of command/powershell/cmdlet; arguments/parameters only with powershell/cmdlet) and derived task_id from cmdlet — all reading the un-rendered Jinja expressions. Move the validation into execute(), which runs after rendering.

Breaking change (flagged for maintainers): this drops the cmdlet-derived task_id default. That default read the cmdlet template field at construction, before super().__init__, and task_id is identity — so it is the one piece that can't move to execute(). Pass task_id explicitly when using cmdlet. If you'd rather keep the convenience, the alternative is to leave PsrpOperator on the exemption list until the check can sanction a task_id-from-template default; I went with the removal to clear the entry, but happy to revert that part.

related: #70296

Testing Done

test_no_command_or_powershell now asserts the ValueError surfaces from execute(); it fails on the pre-fix source (the constructor raised first) and passes after. Dropped test_cmdlet_task_id_default (behaviour removed) and passed task_id explicitly in the two securestring tests. test_psrp.py: 33 passed. validate_operators_init.py on the operator exits 0.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: GitHub Copilot CLI following the guidelines

command, powershell, cmdlet, arguments and parameters are template fields,
rendered after __init__ runs. The constructor validated their combination and
derived task_id from cmdlet, all reading the un-rendered Jinja expressions. Move
the validation into execute(), which runs after rendering.

This drops the cmdlet-derived task_id default (a construction-time read of a
template field that cannot move): task_id must now be passed explicitly when
using cmdlet.

related: apache#70296
Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang marked this pull request as draft July 24, 2026 05:47
Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang marked this pull request as ready for review July 24, 2026 06:23
@shahar1 shahar1 removed the backport-to-v3-3-test Backport to v3-3-test label Jul 24, 2026
1fanwang added 3 commits July 24, 2026 11:32
Signed-off-by: 1fanwang <1fannnw@gmail.com>

# Conflicts:
#	scripts/ci/prek/validate_operators_init_exemptions.txt
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
command is a template field, so validating it in __init__ checked the raw
Jinja expression, not the rendered value. Add a test that renders a command
resolving to an empty string, then executes: it fails on the pre-fix source
(the empty command slips past __init__ and reaches execution) and passes with
validation in execute().

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Comment thread providers/microsoft/psrp/docs/changelog.rst Outdated
Comment thread providers/microsoft/psrp/src/airflow/providers/microsoft/psrp/operators/psrp.py Outdated
exactly_one deduped equal values before counting them, so passing the same value to two of command/powershell/cmdlet passed validation. Pass the options positionally so each is counted.

Drop the manual changelog note; the provider release manager regenerates the changelog from git log.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Comment on lines -109 to -117
args = {command, powershell, cmdlet}
if not exactly_one(*args):
raise ValueError("Must provide exactly one of 'command', 'powershell', or 'cmdlet'")
if arguments and not (powershell or cmdlet):
raise ValueError("Arguments only allowed with 'powershell' or 'cmdlet'")
if parameters and not (powershell or cmdlet):
raise ValueError("Parameters only allowed with 'powershell' or 'cmdlet'")
if cmdlet:
kwargs.setdefault("task_id", cmdlet)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don’t think this is correct. The checks are against argument usage, not the rendered results. Errors should be raised even if the template would eventually evaluate to a falsy value IMO.

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.

Thanks for the feedback, TP. The current definition in the pre-commit is indeed too coarse, so I'll try to fine-tune it so such cases of checking against argument usage won't be flagged. There were about 20% of false-positive cases like that have already been merged - I'll figure out how to handle them after fixing the pre-commit.

@1fanwang - please hold with this PR until I figure it out

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Act, thanks the feedback / heads-up, will keep an eye out on the new issue/pr raised on addressing this at scale in repo.

The mutual-exclusivity and arguments/parameters checks are about which options the Dag author provided, not what the templates render to. Keying them off rendered truthiness dropped a provided option that rendered to a falsy value, and let a second option slip through when the first rendered empty. Check is-not-None (usage) instead, and dispatch the chosen option consistently.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants