backports.weakref
Backport of new features in Python's weakref module
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-weakrefuv
uv add backports-weakrefpoetry
poetry add backports-weakrefInstalling 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
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
weakrefmethodProvides a WeakMethod class for storing bound…
permissive · top 15,000 on PyPI
backports.tempfileProvides the TemporaryDirectory context manager…
permissive · top 5,000 on PyPI
async-exit-stackProvides AsyncExitStack, a backport of Python…
permissive · top 15,000 on PyPI
contextlib2contextlib2 backports the Python standard…
permissive · top 5,000 on PyPI
traceback2Provides a backport of Python's traceback…
permissive · top 5,000 on PyPI
backports.zstdProvides Zstandard compression support for…
permissive · top 1,000 on PyPI
configparserProvides an updated ConfigParser implementation…
permissive · top 5,000 on PyPI
pickle5Backports the pickle 5 protocol (PEP 574) and…
permissive · top 15,000 on PyPI
objsizeMeasures the complete memory footprint of…
permissive · top 5,000 on PyPI
exceptiongroupProvides exception group classes and utilities…
permissive · top 1,000 on PyPI