This is an automated email from the ASF dual-hosted git repository.

vatsrahul1001 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new fd22958936c Accept a single string for Dag tags and 
`allowed_run_types` (#75379)
fd22958936c is described below

commit fd22958936c5ad8ada6de6fe8ae16ed19d649c5e
Author: W. H. Wang <[email protected]>
AuthorDate: Fri Nov 27 22:09:07 2026 +0800

    Accept a single string for Dag tags and `allowed_run_types` (#75379)
    
    * Accept a single string for Dag tags and allowed_run_types
    
    Passing a bare string such as tags="finance" silently produced one tag per
    character, because the converter iterated over the string. The same happened
    for @asset(tags=...), which forwards its tags to the Dag. A bare string for
    allowed_run_types failed with a confusing "'m' is not a valid DagRunType"
    error even though a single DagRunType member was already accepted.
    
    Treat a bare string as a single value, consistent with how other Dag
    arguments such as template_searchpath already handle it.
    
    * Widen allowed_run_types annotations to accept strings
    
    DagRunType is not exported from airflow.sdk, so Dag authors pass plain
    strings. The converter and the dag() overload now accept str and
    collections of str, so mypy no longer rejects allowed_run_types="manual".
    
    * Treat an empty allowed_run_types like None and document string inputs
    
    An empty string used to iterate to an empty frozenset, which the validator
    and serializer already treat as no restriction. Wrapping a bare string 
turned
    it into [""] and raised ValueError, so the converter now returns None for
    any falsy value, the same way _convert_tags handles tags="".
---
 task-sdk/src/airflow/sdk/definitions/dag.py     | 20 ++++++++++++--------
 task-sdk/tests/task_sdk/definitions/test_dag.py | 11 ++++++++++-
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/task-sdk/src/airflow/sdk/definitions/dag.py 
b/task-sdk/src/airflow/sdk/definitions/dag.py
index 4a7083965b1..7bb5a633641 100644
--- a/task-sdk/src/airflow/sdk/definitions/dag.py
+++ b/task-sdk/src/airflow/sdk/definitions/dag.py
@@ -201,8 +201,10 @@ def _convert_str_to_tuple(val: str | Iterable[str] | None) 
-> Iterable[str] | No
     return val
 
 
-def _convert_tags(tags: Collection[str] | None) -> MutableSet[str]:
-    return set(tags or [])
+def _convert_tags(tags: str | Collection[str] | None) -> MutableSet[str]:
+    if not tags:
+        return set()
+    return {tags} if isinstance(tags, str) else set(tags)
 
 
 def _convert_access_control(access_control):
@@ -220,14 +222,14 @@ def _convert_access_control(access_control):
 
 
 def _convert_allowed_run_types(
-    val: DagRunType | Collection[DagRunType] | None,
+    val: DagRunType | str | Collection[DagRunType | str] | None,
 ) -> frozenset[DagRunType] | None:
     """Convert allowed_run_types parameter to a frozenset of DagRunType 
values."""
-    if val is None:
+    if not val:
         return None
     from airflow.sdk.api.datamodels._generated import DagRunType
 
-    if isinstance(val, enum.Enum):
+    if isinstance(val, str | enum.Enum):
         val = [val]
     return frozenset(DagRunType(v) if not isinstance(v, DagRunType) else v for 
v in val)
 
@@ -431,7 +433,7 @@ class DAG:
     :param render_template_as_native_obj: If True, uses a Jinja 
``NativeEnvironment``
         to render templates as native Python types. If False, a Jinja
         ``Environment`` is used to render templates as string values.
-    :param tags: List of tags to help filtering Dags in the UI.
+    :param tags: List of tags to help filtering Dags in the UI. A single 
string is treated as one tag.
     :param owner_links: Dict of owners and their links, that will be clickable 
on the Dags view UI.
         Can be used as an HTTP link (for example the link to your Slack 
channel), or a mailto link.
         e.g: ``{"dag_owner": "https://airflow.apache.org/"}``
@@ -441,6 +443,8 @@ class DAG:
         An exception will be thrown if any task in a fail stop dag has a non 
default trigger rule.
     :param allowed_run_types: An optional list or single DagRunType specifying 
which run types are
         permitted for this dag. When set, the scheduler and API will only 
allow runs of the specified types.
+        Each run type can also be given as its string value, such as 
``"manual"``. An empty value means
+        no restriction, the same as ``None``.
     :param dag_display_name: The display name of the Dag which appears on the 
UI.
     :param rerun_with_latest_version: If True, cleared or rerun tasks will use 
the latest
         available bundle version. If False, they use the original bundle 
version. If None
@@ -551,7 +555,7 @@ class DAG:
     owner_links: dict[str, str] = attrs.field(factory=dict)
     auto_register: bool = attrs.field(default=True, converter=bool)
     fail_fast: bool = attrs.field(default=False, converter=bool)
-    allowed_run_types: DagRunType | Collection[DagRunType] | None = 
attrs.field(
+    allowed_run_types: DagRunType | str | Collection[DagRunType | str] | None 
= attrs.field(
         default=None, converter=_convert_allowed_run_types
     )
     dag_display_name: str = attrs.field(
@@ -1679,7 +1683,7 @@ if TYPE_CHECKING:
         owner_links: dict[str, str] | None = None,
         auto_register: bool = True,
         fail_fast: bool = False,
-        allowed_run_types: DagRunType | Collection[DagRunType] | None = None,
+        allowed_run_types: DagRunType | str | Collection[DagRunType | str] | 
None = None,
         dag_display_name: str | None = None,
         disable_bundle_versioning: bool = False,
         rerun_with_latest_version: bool | None = None,
diff --git a/task-sdk/tests/task_sdk/definitions/test_dag.py 
b/task-sdk/tests/task_sdk/definitions/test_dag.py
index be9975f1b93..8ec66ceb50b 100644
--- a/task-sdk/tests/task_sdk/definitions/test_dag.py
+++ b/task-sdk/tests/task_sdk/definitions/test_dag.py
@@ -570,9 +570,10 @@ def test__tags_length(tags: list[str], should_pass: bool):
             {"a"},
             id="two same tags",
         ),
+        pytest.param("a normal tag", {"a normal tag"}, id="single string tag"),
     ],
 )
-def test__tags_duplicates(input_tags: list[str], expected_result: set[str]):
+def test__tags_duplicates(input_tags: str | list[str], expected_result: 
set[str]):
     result = DAG("test-dag", tags=input_tags)
     assert result.tags == expected_result
 
@@ -581,6 +582,8 @@ def test__tags_duplicates(input_tags: list[str], 
expected_result: set[str]):
     ("schedule", "input_val", "expected"),
     [
         pytest.param("@daily", None, None, id="none"),
+        pytest.param("@daily", "", None, id="empty_str"),
+        pytest.param("@daily", [], None, id="empty_list"),
         pytest.param(
             "@daily",
             [DagRunType.SCHEDULED],
@@ -611,6 +614,12 @@ def test__tags_duplicates(input_tags: list[str], 
expected_result: set[str]):
             frozenset([DagRunType.MANUAL]),
             id="no_schedule_single_enum",
         ),
+        pytest.param(
+            None,
+            "manual",
+            frozenset([DagRunType.MANUAL]),
+            id="no_schedule_single_str",
+        ),
         pytest.param(
             None,
             [DagRunType.MANUAL, DagRunType.BACKFILL_JOB],

Reply via email to