Registry / serialization / backports-abc

backports-abc

JSON →
library0.5pypypi✓ verified 85d ago

A backport of recent additions to the 'collections.abc' module, providing Abstract Base Classes (ABCs) like `Generator`, `Awaitable`, and `Coroutine` for older Python versions (including Python 2.x, 3.2, 3.3, 3.4). It allows for direct imports of these ABCs or offers a `patch()` function to integrate them into the `collections` or `collections.abc` module namespace. The latest release, 0.5, was released in 2016.

pip install backports-abc
INSTALL
IMPORT
SIG · BACKPORTS-ABC
B
backports-abc
serializationpythonv0.5
Install
1.5s avg
Import
24ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.024s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.023s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Coroutine
from backports_abc import Coroutine
from collections.abc import Coroutine
This library is intended for Python versions where `collections.abc.Coroutine` (or similar ABCs) is not available. The recommended pattern is a conditional import based on Python version.
Generator
from backports_abc import Generator
from collections.abc import Generator
This library is intended for Python versions where `collections.abc.Generator` (or similar ABCs) is not available. The recommended pattern is a conditional import based on Python version.
Awaitable
from backports_abc import Awaitable
from collections.abc import Awaitable
This library is intended for Python versions where `collections.abc.Awaitable` (or similar ABCs) is not available. The recommended pattern is a conditional import based on Python version.
patch
import backports_abc; backports_abc.patch()
The `patch()` function modifies the `collections` or `collections.abc` module in place to add missing ABCs. Use with caution as it alters global state.

Demonstrates the recommended conditional import pattern to use `backports-abc` when `collections.abc` is missing or incomplete, falling back to the standard library where available. It primarily targets Python versions where `Coroutine` and `Awaitable` (Python < 3.5) or `collections.abc` itself (Python < 3.3) are not present or incomplete. The `patch()` function is also available but modifies global state.

import sys if sys.version_info < (3, 5): # Coroutine/Awaitable were added in 3.5 try: from backports_abc import Coroutine, Generator except ImportError: # Fallback for even older Python or if backport isn't installed # (though this should not happen if backports_abc is installed) from collections import Generator # Python 2.x / 3.2 might have it here # Coroutine might not be available at all else: # In Python 3.3+, ABCs are in collections.abc # Coroutine/Awaitable from 3.5 from collections.abc import Coroutine, Generator print(f"Using Coroutine from: {Coroutine.__module__}") print(f"Using Generator from: {Generator.__module__}") # Example of using patch (generally less recommended due to global state) # if sys.version_info < (3, 5): # import backports_abc # backports_abc.patch() # from collections.abc import Coroutine as PatchedCoroutine # print(f"Patched Coroutine from: {PatchedCoroutine.__module__}")
Debug
Known issues
gotchaThe `patch()` function modifies Python's standard `collections` or `collections.abc` module in-place. In Python 2.x and 3.2, it patches `collections`, while for Python 3.3+ it targets `collections.abc`. This global state modification can potentially lead to unexpected behavior or conflicts in complex environments. It will not overwrite names already present.
fix
Prefer explicit conditional imports (`try...except ImportError` or `if sys.version_info`) over `backports_abc.patch()` to minimize side effects and clearly manage dependencies based on the Python environment.
affects: < 3.5 (Python 2.x, 3.2-3.4)
deprecatedThis library has not been updated since 2016. While functional for its intended purpose of supporting older Python versions, its inactive maintenance status means it will not receive new features or bug fixes. Users should consider upgrading to a Python version where these ABCs are natively available in the standard library if possible.
fix
For new projects or when targeting modern Python versions (3.5+ for `Coroutine`/`Awaitable`, 3.3+ for `collections.abc`), use the native `collections.abc` module directly without `backports-abc`.
affects: All versions
gotchaThe primary use case for `backports-abc` is to provide `collections.abc` elements (like `Generator`, `Awaitable`, `Coroutine`) that were introduced in later Python versions. Using this library on Python versions where these ABCs are already available (e.g., Python 3.5+ for `Coroutine`) is unnecessary and could potentially mask issues if an older, possibly incomplete, backported definition were to take precedence, though the library attempts to avoid overwriting existing names.
fix
Implement a `sys.version_info` check or `try...except ImportError` to conditionally import from `backports_abc` only when running on target older Python environments. This ensures the native standard library definitions are used on newer Python versions.
affects: > 3.4 (for Coroutine/Awaitable), > 3.2 (for collections.abc module)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'backports_abc'
The 'backports-abc' library is not installed in the current Python environment, or it's not accessible through the Python path.
fix
Install the library using pip: `pip install backports-abc`
ImportError: cannot import name 'Generator' from 'collections.abc'
This error occurs on Python versions (e.g., 3.3, 3.4) where the `collections.abc` module exists but does not yet include Abstract Base Classes like `Generator`, `Awaitable`, or `Coroutine`, which were added in Python 3.5.
fix
Use a conditional import structure to first try importing from `collections.abc` and fall back to `backports_abc` for older Python versions, or simply import directly from `backports_abc`: 
```python
try:
    from collections.abc import Generator
except ImportError:
    from backports_abc import Generator
```
AttributeError: module 'collections' has no attribute 'abc'
This error typically occurs in very old Python versions (e.g., Python 2.x, 3.2) where the `collections.abc` module itself did not exist in the standard library.
fix
For these Python versions, either import the ABCs directly from `backports_abc` (e.g., `from backports_abc import Generator`) or use the `patch()` function provided by the library to integrate the ABCs into the `collections` module namespace: 
```python
import backports_abc
backports_abc.patch()
# Now you can try to import from collections.abc, or collections directly for older Pythons
# For example, if you need Generator, you'd still import from backports_abc directly or rely on other libs to do the patching.
```
Upgrade
Version history
0.5latest on PyPI · released Nov 12, 2016
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
backports-abc — pip install backports-abc · libregistry