python/cpython · error · AttributeError

module 'collections.abc' has no attribute {attr!r}

Error message

module 'collections.abc' has no attribute {attr!r}

What it means

AttributeError raised by the module-level __getattr__ in Lib/_collections_abc.py for any attribute of collections.abc that does not exist (after the ByteString deprecation shim is consulted). The module defines __getattr__ solely to service the deprecated 'ByteString' alias; every other missing-name lookup falls through to this explicit error.

Source

Thrown at Lib/_collections_abc.py:1181

        del self[self.index(value)]

    def __iadd__(self, values):
        self.extend(values)
        return self


MutableSequence.register(list)
MutableSequence.register(bytearray)

_deprecated_ByteString = globals().pop("ByteString")

def __getattr__(attr):
    if attr == "ByteString":
        import warnings
        warnings._deprecated("collections.abc.ByteString", remove=(3, 17))
        globals()["ByteString"] = _deprecated_ByteString
        return _deprecated_ByteString
    raise AttributeError(f"module 'collections.abc' has no attribute {attr!r}")

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Check the spelling against dir(collections.abc) or the docs for your Python version.
  2. For version-dependent ABCs, guard with getattr(collections.abc, 'Buffer', None) or hasattr before access.
  3. Replace old collections.X usages with collections.abc.X (Mapping, Sequence, Iterable, ...).
  4. Note collections.abc.ByteString is deprecated for removal (3.17) — migrate to Sequence/bytes checks now to avoid this error later.

Example fix

# before
from collections.abc import ByteString  # works today, removal scheduled (3.17)
x = collections.abc.Itrable  # AttributeError

# after
from collections.abc import Sequence
x = collections.abc.Iterable
Buffer = getattr(collections.abc, 'Buffer', None)  # version-safe feature detection
Defensive patterns

Strategy: validation

Validate before calling

import collections.abc as cabc

name = 'Buffer'
ABC = getattr(cabc, name, None)
if ABC is None:
    raise NotImplementedError(f'collections.abc.{name} not on Python {sys.version_info}')

Type guard

def abc_exists(name: str) -> bool:
    import collections.abc as cabc
    return name in dir(cabc) or name == 'ByteString'  # shimmed name

Try / catch

try:
    ABC = getattr(collections.abc, name)
except AttributeError as e:
    if 'has no attribute' in str(e):
        ABC = None  # feature absent on this Python; use fallback path
    else:
        raise

Prevention

When it happens

Trigger: collections.abc.Bytearray (typo for ByteString/Bytearray mismatch), collections.abc.mappping, or any attribute access on the module that is not one of its ABCs. Also triggered by hasattr-driven feature detection and getattr(collections.abc, name) with dynamic names.

Common situations: Attribute typos in imports ('from collections.abc import Iterble'); code assuming a newer ABC (e.g. Buffer) exists on an older Python; IDE-autocompleted but nonexistent names; migration from collections container ABCs to collections.abc where names differ (e.g. collections.Mapping vs collections.abc.Mapping).

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/bac9b1541b153d45. Report an issue: GitHub.