{"id":6538,"library":"backports-abc","title":"backports-abc","description":"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.","status":"maintenance","version":"0.5","language":"en","source_language":"en","source_url":"https://github.com/cython/backports_abc","tags":["backport","collections","abc","async","compatibility","python2","python3"],"install":[{"cmd":"pip install backports-abc","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"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.","wrong":"from collections.abc import Coroutine","symbol":"Coroutine","correct":"from backports_abc import Coroutine"},{"note":"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.","wrong":"from collections.abc import Generator","symbol":"Generator","correct":"from backports_abc import Generator"},{"note":"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.","wrong":"from collections.abc import Awaitable","symbol":"Awaitable","correct":"from backports_abc import Awaitable"},{"note":"The `patch()` function modifies the `collections` or `collections.abc` module in place to add missing ABCs. Use with caution as it alters global state.","symbol":"patch","correct":"import backports_abc; backports_abc.patch()"}],"quickstart":{"code":"import sys\n\nif sys.version_info < (3, 5): # Coroutine/Awaitable were added in 3.5\n    try:\n        from backports_abc import Coroutine, Generator\n    except ImportError:\n        # Fallback for even older Python or if backport isn't installed\n        # (though this should not happen if backports_abc is installed)\n        from collections import Generator # Python 2.x / 3.2 might have it here\n        # Coroutine might not be available at all\nelse:\n    # In Python 3.3+, ABCs are in collections.abc\n    # Coroutine/Awaitable from 3.5\n    from collections.abc import Coroutine, Generator\n\nprint(f\"Using Coroutine from: {Coroutine.__module__}\")\nprint(f\"Using Generator from: {Generator.__module__}\")\n\n# Example of using patch (generally less recommended due to global state)\n# if sys.version_info < (3, 5):\n#     import backports_abc\n#     backports_abc.patch()\n#     from collections.abc import Coroutine as PatchedCoroutine\n#     print(f\"Patched Coroutine from: {PatchedCoroutine.__module__}\")\n","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":"< 3.5 (Python 2.x, 3.2-3.4)"},{"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`.","message":"This 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.","severity":"deprecated","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"> 3.4 (for Coroutine/Awaitable), > 3.2 (for collections.abc module)"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}