Skip to content

ref: Remove Python 2 compat from qualname_from_function#6137

Open
ericapisani wants to merge 1 commit intomasterfrom
py-2298-remove-python2-compat-code-cg9li
Open

ref: Remove Python 2 compat from qualname_from_function#6137
ericapisani wants to merge 1 commit intomasterfrom
py-2298-remove-python2-compat-code-cg9li

Conversation

@ericapisani
Copy link
Copy Markdown
Member

@ericapisani ericapisani commented Apr 24, 2026

Remove two Python 2 compatibility branches from sentry_sdk/utils.py::qualname_from_function that can never fire.

The SDK requires Python ≥ 3.6 (setup.py), so the im_class try/except (Python 2 bound-method attribute) and the elif hasattr(func, "__name__") fallback annotated # Python 2.7 has no __qualname__ are dead code.

No behavior change for any Python 3 input — the return contract is preserved for callers in tracing_utils.py, integrations/ray.py, integrations/asgi.py, and integrations/django/tasks.py.

Refs PY-2298

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) <noreply@anthropic.com>
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Apr 24, 2026

@ericapisani ericapisani marked this pull request as ready for review April 24, 2026 18:59
@ericapisani ericapisani requested a review from a team as a code owner April 24, 2026 18:59
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.57s

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 14783 uncovered lines.

Files with missing lines (1)
File Patch % Lines
utils.py 53.02% ⚠️ 435 Missing and 84 partials

Generated by Codecov Action

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6d4e045. Configure here.

Comment thread sentry_sdk/utils.py
)
except Exception:
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback removal breaks built-in function name resolution

Medium Severity

The removed elif hasattr(func, "__name__") fallback was labeled as Python 2 compatibility, but it also handles Python 3 builtin_function_or_method objects (e.g., len, print) and method_descriptor objects (e.g., str.strip), which have __name__ but not __qualname__ (confirmed by CPython issue #57786). After this change, qualname_from_function returns None instead of a name string for any such callable. While current callers mostly pass user-defined functions, the function's contract is silently narrowed.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6d4e045. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat incorrect.

CleanShot 2026-04-24 at 15 14 41@2x

The issue that it's referencing is python/cpython#57786 , which relates to Python 3.3 (and we support 3.6+).

Where I can see this being correct is that if a custom class has a __name__ defined, but not a __qualname__, this would no longer work without adding the :

if hasattr(func, "__name__"):
    func_qualname = func.__name__

logic back in.

Will come back to this on Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant