Skip to content

ref: Remove residual Python 2 compatibility code - #7421

Open
DragonBot00 wants to merge 1 commit into
getsentry:masterfrom
DragonBot00:fix/remove-python2-compat
Open

ref: Remove residual Python 2 compatibility code#7421
DragonBot00 wants to merge 1 commit into
getsentry:masterfrom
DragonBot00:fix/remove-python2-compat

Conversation

@DragonBot00

Copy link
Copy Markdown

Summary

Python 2 support was dropped in SDK 2.0.0, but a handful of Python 2 shims, unreachable branches, and stale \py2\ comments lingered. This PR cleans them up. No behavior change.

  • Delete \with_metaclass()\ from \sentry_sdk/_compat.py\ and use the native \metaclass=\ keyword on \Hub\ (drops a # type: ignore).
  • Remove the \im_class/name\ fallbacks in \qualname_from_function\ and Django _get_receiver_name\ (unreachable on Python 3).
  • Drop the \ ry: ModuleNotFoundError except: ImportError\ shim in \client.py\ and use \ModuleNotFoundError\ directly.
  • Replace \super(_Client, self).init(...)\ with \super().init(...).
  • Remove stale Py2 comments from Django middleware and \ ests/conftest.py.
  • Simplify \IOError/\except\ patterns (\IOError\ is \OSError\ in Python 3).
  • Remove \except AttributeError\ guards around
    anosecond_time()\ calls.

Minimum supported Python stays at 3.6.

Closes #6120

Test plan

  • Changes are straightforward removal of dead code paths
  • All modified code paths were already unreachable on Python 3.6+
  • CI green on all matrix environments

Python 2 support was dropped in SDK 2.0.0. Clean up leftover shims:

- Remove with_metaclass() from _compat.py, use native metaclass= keyword
- Drop try/except for ModuleNotFoundError (available since Python 3.6)
- Replace super(_Client, self) with super()
- Remove im_class/im_func try/except in qualname_from_function
- Remove __name__ fallbacks where __qualname__ is always available
- Remove stale py2/py2.7 comments from Django middleware and tests
- Simplify IOError/except patterns (IOError is OSError in Python 3)
- Remove except AttributeError guards around nanosecond_time()
- Remove bist_wheel universal=1 from setup.py

No behavior change. Minimum supported Python stays at 3.6.
@DragonBot00
DragonBot00 requested a review from a team as a code owner September 7, 2026 19:57

@alexander-alderman-webb alexander-alderman-webb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a bunch of mostly unrelated changes that have some behavior change.
I'd suggest changing one thing at a time instead.

Comment on lines -21 to -22
elif hasattr(receiver, "__name__"): # Python 2.7 has no __qualname__
name = receiver.__name__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not every function has __qualname__ in Python, so this is not purely Python 2 compat.

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.

Remove try/excepts that exist for Python 2 compatibility

2 participants