skillfed

backports.weakref

Backport of new features in Python's weakref module

backports-weakref v1.0.post1 1.2M downloads/30d#4,172 on PyPI8
Permissive license Python Software Foundation License Abandoned released

What it is and what it does

This package provides a backport of `weakref.finalize`, a feature added to Python's standard `weakref` module in Python 3.4. The `finalize` class lets you register a callback function to be called when an object is garbage collected, enabling deterministic cleanup of resources tied to that object's lifetime.

It is intended only for projects that must support Python 2.7 or early Python 3.x versions (3.4–3.6) and cannot upgrade. On Python 3.4 and later, the standard library's `weakref.finalize` should be used directly. The package has no runtime dependencies and installs cleanly, but has been unmaintained since 2017.

Use it for:

  • Register cleanup code for Python 2.7 projects that need to run finalizers when objects are garbage collected.
  • Support legacy Python 3.4–3.6 codebases that use `weakref.finalize` but cannot yet upgrade to a newer Python version.
  • Provide a uniform API across old and new Python versions when backporting code from Python 3.4+.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Backports the `weakref.finalize` class from Python 3.4+ to earlier Python versions, allowing you to register cleanup callbacks for objects when they are garbage collected.

Only if you are maintaining code on Python 2.7, 3.4, 3.5, or 3.6 and need `weakref.finalize`. The package is abandoned and unmaintained since 2017, so it should not be used in new projects. On Python 3.4+, use the standard library's `weakref.finalize` directly. No known security vulnerabilities.

Install

backports-weakref on PyPI

pip

pip install backports-weakref

uv

uv add backports-weakref

poetry

poetry add backports-weakref

Installing backports.weakref

Before you install

Installation is straightforward with no runtime dependencies. The package is abandoned as of 2017 with no recent maintenance, so it should only be used if you are stuck on Python 2.7, 3.4, 3.5, or 3.6 and cannot upgrade.

License in practice

Licensed under the Python Software Foundation License, a permissive license that allows use in most contexts without restriction.

Quickstart

pip install backports.weakref

from backports.weakref import finalize

class MyClass:
    pass

obj = MyClass()
finalize(obj, print, "Object was garbage collected")

Only useful on Python 2.7, 3.4, 3.5, or 3.6; on Python 3.4+ you should import `finalize` directly from the standard `weakref` module instead.

Verify before relying

  • Whether this package is still compatible with modern Python tooling and dependency resolvers despite being abandoned since 2017.

Package facts

License Python Software Foundation License (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance abandoned — 3,253 days since the last release
Last repo commit
First released
Downloads 1,240,909/month — #4,172 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: backports.weakref-1.0.post1-py2.py3-none-any.whl

Development Status :: 6 - MatureIntended Audience :: DevelopersLicense :: OSI Approved :: Python Software Foundation LicenseProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.4Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: Libraries :: Python Modules

Tags

weakref finalize backportpython 2.7 weakref finalizegarbage collection callbacksobject cleanup on deletionweakref module backportfinalize weak references
legacy-pythonbackport

More Python Modules packages