This is an automated email from the ASF dual-hosted git repository.
weilee 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 8b1c57a1999 Fix PythonOperator DAG error when DAG has hyphen in name
(#42902)
8b1c57a1999 is described below
commit 8b1c57a19992acf64c294626bdb0244f82a1e693
Author: LIU ZHE YOU <[email protected]>
AuthorDate: Sun Oct 13 09:55:16 2024 +0800
Fix PythonOperator DAG error when DAG has hyphen in name (#42902)
---
airflow/utils/file.py | 2 +-
tests/utils/test_file.py | 18 ++++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/airflow/utils/file.py b/airflow/utils/file.py
index 2e39eb7dd7b..86b7a7891ca 100644
--- a/airflow/utils/file.py
+++ b/airflow/utils/file.py
@@ -355,6 +355,6 @@ def get_unique_dag_module_name(file_path: str) -> str:
"""Return a unique module name in the format unusual_prefix_{sha1 of
module's file path}_{original module name}."""
if isinstance(file_path, str):
path_hash = hashlib.sha1(file_path.encode("utf-8")).hexdigest()
- org_mod_name = Path(file_path).stem
+ org_mod_name = re2.sub(r"[.-]", "_", Path(file_path).stem)
return MODIFIED_DAG_MODULE_NAME.format(path_hash=path_hash,
module_name=org_mod_name)
raise ValueError("file_path should be a string to generate unique module
name")
diff --git a/tests/utils/test_file.py b/tests/utils/test_file.py
index 2ffb89ec341..9424b90a92c 100644
--- a/tests/utils/test_file.py
+++ b/tests/utils/test_file.py
@@ -212,3 +212,21 @@ class TestListPyFilesPath:
modules = list(file_utils.iter_airflow_imports(file_path))
assert len(modules) == 0
+
+
[email protected](
+ "edge_filename, expected_modification",
+ [
+ ("test_dag.py", "unusual_prefix_mocked_path_hash_sha1_test_dag"),
+ ("test-dag.py", "unusual_prefix_mocked_path_hash_sha1_test_dag"),
+ ("test-dag-1.py", "unusual_prefix_mocked_path_hash_sha1_test_dag_1"),
+ ("test-dag_1.py", "unusual_prefix_mocked_path_hash_sha1_test_dag_1"),
+ ("test-dag.dev.py",
"unusual_prefix_mocked_path_hash_sha1_test_dag_dev"),
+ ("test_dag.prod.py",
"unusual_prefix_mocked_path_hash_sha1_test_dag_prod"),
+ ],
+)
+def test_get_unique_dag_module_name(edge_filename, expected_modification):
+ with mock.patch("hashlib.sha1") as mocked_sha1:
+ mocked_sha1.return_value.hexdigest.return_value =
"mocked_path_hash_sha1"
+ modify_module_name =
file_utils.get_unique_dag_module_name(edge_filename)
+ assert modify_module_name == expected_modification