From 6d4e0450e6ff4a6fb918f8b0113159d6033a0259 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 24 Apr 2026 14:55:35 -0400 Subject: [PATCH 1/3] ref: Remove Python 2 compat from qualname_from_function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SDK requires Python ≥ 3.6, so the `im_class` try/except and the `__name__` fallback annotated "Python 2.7 has no __qualname__" can never fire. Dropping them also removes a broad `except Exception: pass` that could mask real bugs in the happy path. No behavior change for any Python 3 input; return contract is preserved for callers in `tracing_utils.py`, `integrations/ray.py`, `integrations/asgi.py`, and `integrations/django/tasks.py`. Refs PY-2298 Co-Authored-By: Claude Opus 4.7 (1M context) --- sentry_sdk/utils.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index be238e5142..78b22d06df 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1472,16 +1472,6 @@ def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]": """Return the qualified name of func. Works with regular function, lambda, partial and partialmethod.""" func_qualname: "Optional[str]" = None - # Python 2 - try: - return "%s.%s.%s" % ( - func.im_class.__module__, # type: ignore - func.im_class.__name__, # type: ignore - func.__name__, - ) - except Exception: - pass - prefix, suffix = "", "" if isinstance(func, partial) and hasattr(func.func, "__name__"): @@ -1499,10 +1489,7 @@ def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]": if hasattr(func, "__qualname__"): func_qualname = func.__qualname__ - elif hasattr(func, "__name__"): # Python 2.7 has no __qualname__ - func_qualname = func.__name__ - # Python 3: methods, functions, classes if func_qualname is not None: if hasattr(func, "__module__") and isinstance(func.__module__, str): func_qualname = func.__module__ + "." + func_qualname From c4bc1dee94713bcdd2b7c074de9a8053a8c7180e Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Mon, 27 Apr 2026 08:09:02 -0400 Subject: [PATCH 2/3] Add the check back in. This ensures that custom classes that may not have a defined, or C extension functions which only have return a value --- sentry_sdk/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 78b22d06df..ccf9893dbe 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1489,6 +1489,8 @@ def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]": if hasattr(func, "__qualname__"): func_qualname = func.__qualname__ + elif hasattr(func, "__name__"): + func_qualname = func.__name__ if func_qualname is not None: if hasattr(func, "__module__") and isinstance(func.__module__, str): From e9480cd53cbd74d1456213ecae7b6c90003f84fa Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Mon, 27 Apr 2026 08:09:02 -0400 Subject: [PATCH 3/3] Add the `__name__` check back in. This ensures that custom classes that may not have a `__qualname__` defined, or C extension functions which only have `__name__` return a value Co-Authored-By: Claude Opus 4.7 (1M context) --- sentry_sdk/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 78b22d06df..ccf9893dbe 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1489,6 +1489,8 @@ def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]": if hasattr(func, "__qualname__"): func_qualname = func.__qualname__ + elif hasattr(func, "__name__"): + func_qualname = func.__name__ if func_qualname is not None: if hasattr(func, "__module__") and isinstance(func.__module__, str):