dabla commented on code in PR #69859:
URL: https://github.com/apache/airflow/pull/69859#discussion_r4601744652


##########
airflow-core/src/airflow/dag_processing/bundles/base.py:
##########
@@ -350,6 +352,19 @@ def initialize(self) -> None:
                 bundle_path,
             )
 
+    def _log_context(self) -> dict:
+        """Return extra structlog context fields for this bundle. Override in 
subclasses."""
+        return {}
+
+    @property
+    def _log(self):
+        """Lazy structlog logger bound with common bundle context plus 
subclass-specific fields."""
+        if self.__log is None:
+            self.__log = structlog.get_logger(type(self).__module__).bind(
+                bundle_name=self.name, version=self.version, 
**self._log_context()
+            )
+        return self.__log

Review Comment:
   Thanks, that was a real break. `_log` has the setter you suggested, in core 
and in the `common.compat` fallback, so a bundle that assigns `self._log` keeps 
its own logger. In `6dedc23104` the default also moved from `__init__` to the 
class, because the base constructor was resetting a logger that a subclass had 
assigned before calling `super().__init__()`. Covered by 
`test_log_setter_allows_direct_assignment` and 
`test_log_assigned_before_base_constructor_is_kept` in core and by 
`test_log_setter_allows_direct_assignment` for the fallback.



##########
airflow-core/src/airflow/dag_processing/bundles/base.py:
##########
@@ -350,6 +352,19 @@ def initialize(self) -> None:
                 bundle_path,
             )
 
+    def _log_context(self) -> dict:

Review Comment:
   Done: the hook is `_get_log_context() -> dict[str, Any]`, `_log` returns 
`Any`, and the slot is declared as `Any`. `6dedc23104` gives the provider 
overrides and the compat fallback the same annotation.



##########
providers/common/compat/tests/unit/common/compat/bundles/test_dag_bundle.py:
##########
@@ -0,0 +1,129 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+
+pytest.importorskip("airflow.dag_processing.bundles", reason="Requires Airflow 
3+")
+
+from airflow.dag_processing.bundles.base import BaseDagBundle as 
CoreBaseDagBundle
+from airflow.providers.common.compat.bundles import BaseDagBundle
+
+
+class TestDagBundleCompat:
+    def test_base_dag_bundle_is_importable(self):
+        assert BaseDagBundle is not None
+
+    def test_base_dag_bundle_is_subclass_of_core(self):
+        assert issubclass(BaseDagBundle, CoreBaseDagBundle)
+
+    def test_has_log_property(self):
+        assert hasattr(BaseDagBundle, "_log")
+
+    def test_log_property_on_subclass_instance(self, tmp_path):
+        """_log returns a bound structlog logger on concrete bundle 
subclasses."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars
+
+        class _DummyBundle(BaseDagBundle):
+            @property
+            def path(self) -> Path:
+                return tmp_path
+
+            def initialize(self) -> None:
+                pass
+
+            def get_current_version(self):
+                return None
+
+            def refresh(self) -> None:
+                pass
+
+        with conf_vars({("dag_processor", "dag_bundle_storage_path"): 
str(tmp_path)}):
+            bundle = _DummyBundle(name="test-bundle")
+
+        log = bundle._log
+        assert log is not None
+        # Calling _log twice returns the same (cached) logger.
+        assert bundle._log is log
+
+    def test_log_is_bound_with_context(self, tmp_path):
+        """_log includes bundle_name and version in its bound variables."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars
+
+        class _DummyBundle(BaseDagBundle):
+            @property
+            def path(self) -> Path:
+                return tmp_path
+
+            def initialize(self) -> None:
+                pass
+
+            def get_current_version(self):
+                return None
+
+            def refresh(self) -> None:
+                pass
+
+        with conf_vars({("dag_processor", "dag_bundle_storage_path"): 
str(tmp_path)}):
+            bundle = _DummyBundle(name="my-bundle", version="abc123")
+
+        log = bundle._log
+        # structlog bound loggers expose their bindings via _context
+        ctx = log._context if hasattr(log, "_context") else {}
+        assert ctx.get("bundle_name") == "my-bundle"
+        assert ctx.get("version") == "abc123"
+
+    def test_compat_subclass_provides_log_when_missing(self, tmp_path):
+        """Even when the installed BaseDagBundle has no _log, the compat class 
fills it in."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars
+
+        # Simulate an older Airflow version by temporarily hiding _log from
+        # the core class; the compat class must still expose it.
+        with mock.patch.object(CoreBaseDagBundle, "_log", None, create=True):
+            from importlib import reload
+
+            import airflow.providers.common.compat.bundles as mod
+
+            reload(mod)
+            CompatBase = mod.BaseDagBundle

Review Comment:
   Fixed as you proposed: `legacy_compat_base` yields inside the patch and 
reloads the module again once the patch is lifted.



##########
providers/common/compat/tests/unit/common/compat/bundles/test_dag_bundle.py:
##########
@@ -0,0 +1,129 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+
+pytest.importorskip("airflow.dag_processing.bundles", reason="Requires Airflow 
3+")
+
+from airflow.dag_processing.bundles.base import BaseDagBundle as 
CoreBaseDagBundle
+from airflow.providers.common.compat.bundles import BaseDagBundle
+
+
+class TestDagBundleCompat:
+    def test_base_dag_bundle_is_importable(self):
+        assert BaseDagBundle is not None
+
+    def test_base_dag_bundle_is_subclass_of_core(self):
+        assert issubclass(BaseDagBundle, CoreBaseDagBundle)
+
+    def test_has_log_property(self):
+        assert hasattr(BaseDagBundle, "_log")
+
+    def test_log_property_on_subclass_instance(self, tmp_path):
+        """_log returns a bound structlog logger on concrete bundle 
subclasses."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars
+
+        class _DummyBundle(BaseDagBundle):
+            @property
+            def path(self) -> Path:
+                return tmp_path
+
+            def initialize(self) -> None:
+                pass
+
+            def get_current_version(self):
+                return None
+
+            def refresh(self) -> None:
+                pass
+
+        with conf_vars({("dag_processor", "dag_bundle_storage_path"): 
str(tmp_path)}):
+            bundle = _DummyBundle(name="test-bundle")
+
+        log = bundle._log
+        assert log is not None
+        # Calling _log twice returns the same (cached) logger.
+        assert bundle._log is log
+
+    def test_log_is_bound_with_context(self, tmp_path):
+        """_log includes bundle_name and version in its bound variables."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars
+
+        class _DummyBundle(BaseDagBundle):
+            @property
+            def path(self) -> Path:
+                return tmp_path
+
+            def initialize(self) -> None:
+                pass
+
+            def get_current_version(self):
+                return None
+
+            def refresh(self) -> None:
+                pass
+
+        with conf_vars({("dag_processor", "dag_bundle_storage_path"): 
str(tmp_path)}):
+            bundle = _DummyBundle(name="my-bundle", version="abc123")
+
+        log = bundle._log
+        # structlog bound loggers expose their bindings via _context
+        ctx = log._context if hasattr(log, "_context") else {}
+        assert ctx.get("bundle_name") == "my-bundle"
+        assert ctx.get("version") == "abc123"
+
+    def test_compat_subclass_provides_log_when_missing(self, tmp_path):
+        """Even when the installed BaseDagBundle has no _log, the compat class 
fills it in."""
+        from pathlib import Path
+
+        from tests_common.test_utils.config import conf_vars

Review Comment:
   Done: all imports are at module level (the last one, in the fixture, moved 
in `6dedc23104`), and the dummy bundle comes from one `_make_bundle_class` 
factory. The fallback tests now check what the fallback binds (`bundle_name`, 
`version`, the subclass context, caching, the setter) instead of only that a 
logger exists, and the two tests that could not fail are gone.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to