Refactor validate_key to raise ValueError instead of AirflowException - #68890
Conversation
|
Needs conflict resolving |
potiuk
left a comment
There was a problem hiding this comment.
Thanks — this is the right direction and better justified than the diff alone suggests. The Task SDK's own validate_key in airflow/sdk/definitions/_internal/node.py already raises ValueError for both the length and regex cases, so this brings airflow.utils.helpers.validate_key into line with a migration that's already happened on the other copy rather than making a fresh judgement call. Decrementing the known_airflow_exceptions.txt count from 4 to 1 alongside is good hygiene.
One thing to flag on blast radius, since it's wider than "a helper in utils": besides airflow connections add/import, this function is what KubernetesPodOperator uses for pod-name validation (providers/cncf/kubernetes/.../operators/pod.py:1517, validate_key(name, max_length=220)). So a Dag that wraps KPO execution in except AirflowException and relies on catching an invalid pod name will stop catching it — ValueError isn't a subclass of AirflowException.
That's a user-visible behaviour change in airflow-core, so I think it wants a newsfragment (airflow-core/newsfragments/68890.significant.rst or .misc.rst) noting that validate_key now raises ValueError and naming the KPO pod-name path specifically, so anyone with that except clause finds it. Happy to approve on that basis — please add one before merge.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting
…apache#68890) * Replace AirflowException with ValueError in validate_key * Update Kubernetes tests and known exceptions for refactor * Fix backwards compatibility in Kubernetes tests for validate_key * Align validate_key error message with Task SDK * Add newsfragment for KubernetesPodOperator behavior change
Why
The
AirflowExceptionis too broad for simple input validation errors. Sincevalidate_keystrictly checks the format and length of a string argument, using a standardValueErrorprovides a more specific error type for input validation failures.What
AirflowExceptionwithValueErrorinside thevalidate_keyutility function.test_validate_keyto expect the new exception.ValueError.Error Output Example
Before:
After:
Verification
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.