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

potiuk 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 727db077b4 Better error message when serializing callable without name 
(#31778)
727db077b4 is described below

commit 727db077b4d2ccf6714a892f19d30a7a0e4225e7
Author: Jarek Potiuk <[email protected]>
AuthorDate: Wed Jun 7 21:25:12 2023 +0200

    Better error message when serializing callable without name (#31778)
    
    When Callable without `__name__` got serialized, the error message was
    pretty cryptic: `AttributeError: __name__. Did you mean: '__ne__'?`
    without printing what the failing type is.
    
    This PR adds a more meaningful message
    
    Related to #31753, #31499
---
 airflow/utils/module_loading.py   |  2 +-
 tests/serialization/test_serde.py | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/airflow/utils/module_loading.py b/airflow/utils/module_loading.py
index 3053a2ed04..8decbc6a61 100644
--- a/airflow/utils/module_loading.py
+++ b/airflow/utils/module_loading.py
@@ -43,7 +43,7 @@ def import_string(dotted_path: str):
 
 def qualname(o: object | Callable) -> str:
     """Convert an attribute/class/function to a string importable by 
``import_string``."""
-    if callable(o):
+    if callable(o) and hasattr(o, "__module__") and hasattr(o, "__name__"):
         return f"{o.__module__}.{o.__name__}"
 
     cls = o
diff --git a/tests/serialization/test_serde.py 
b/tests/serialization/test_serde.py
index a86b9ce816..1b2a426ac1 100644
--- a/tests/serialization/test_serde.py
+++ b/tests/serialization/test_serde.py
@@ -100,6 +100,11 @@ class U(BaseModel):
     u: tuple
 
 
+class C:
+    def __call__(self):
+        return None
+
+
 @pytest.mark.usefixtures("recalculate_patterns")
 class TestSerDe:
     def test_ser_primitives(self):
@@ -331,3 +336,10 @@ class TestSerDe:
         e = serialize(i)
         s = deserialize(e)
         assert i == s
+
+    def test_error_when_serializing_callable_without_name(self):
+        i = C()
+        with pytest.raises(
+            TypeError, match="cannot serialize object of type <class 
'tests.serialization.test_serde.C'>"
+        ):
+            serialize(i)

Reply via email to