pypa/pip · error · TypeError

Could not find adapter for {registry} and {ob}

Error message

Could not find adapter for {registry} and {ob}

What it means

Raised as TypeError by _find_adapter when no type in the MRO of the given object's class is registered in the adapter registry. pkg_resources uses registries to map object types (e.g. resource provider types) to adapter factories; a miss means the library does not know how to handle that object.

Source

Thrown at src/pip/_vendor/pkg_resources/__init__.py:3498

def _always_object(classes):
    """
    Ensure object appears in the mro even
    for old-style classes.
    """
    if object not in classes:
        return classes + (object,)
    return classes


def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT:
    """Return an adapter factory for `ob` from `registry`"""
    types = _always_object(inspect.getmro(getattr(ob, '__class__', type(ob))))
    for t in types:
        if t in registry:
            return registry[t]
    # _find_adapter would previously return None, and immediately be called.
    # So we're raising a TypeError to keep backward compatibility if anyone depended on that behaviour.
    raise TypeError(f"Could not find adapter for {registry} and {ob}")


def ensure_directory(path: StrOrBytesPath):
    """Ensure that the parent directory of `path` exists"""
    dirname = os.path.dirname(path)
    os.makedirs(dirname, exist_ok=True)


def _bypass_ensure_directory(path):
    """Sandbox-bypassing version of ensure_directory()"""
    if not WRITE_SUPPORT:
        raise OSError('"os.mkdir" not supported on this platform.')
    dirname, filename = split(path)
    if dirname and filename and not isdir(dirname):
        _bypass_ensure_directory(dirname)
        try:
            mkdir(dirname, 0o755)
        except FileExistsError:

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Inspect which object type triggered the lookup and ensure you pass a supported built-in type (e.g. a real Distribution/provider).
  2. If extending pkg_resources, register your custom class in the relevant adapter registry before invoking the API.
  3. Upgrade pkg_resources/setuptools, since missing registrations are usually a version/compatibility bug.

Example fix

# before
adapter = _find_adapter(registry, my_custom_obj)  # TypeError

# after
from pip._vendor.pkg_resources import _find_adapter
if not any(t in registry for t in type(my_custom_obj).__mro__):
    raise TypeError('unsupported object; register an adapter')
adapter = _find_adapter(registry, my_custom_obj)
Defensive patterns

Strategy: validation

Validate before calling

def adapter_available(registry, ob) -> bool:
    import inspect
    mro = inspect.getmro(getattr(ob, '__class__', type(ob)))
    return any(t in registry for t in mro)
if adapter_available(registry, obj):
    _find_adapter(registry, obj)

Type guard

def has_registered_type(registry, ob) -> bool:
    return any(t in registry for t in type(ob).__mro__)

Try / catch

try:
    adapter = _find_adapter(registry, ob)
except TypeError as e:
    if 'Could not find adapter' in str(e):
        # register or use a default adapter
        ...
    raise

Prevention

When it happens

Trigger: An internal registry lookup (_find_adapter(registry, ob)) where ob's class (and all bases up to object) has no entry in the registry mapping. Historically this returned None and blew up on call; now it fails fast with TypeError.

Common situations: Passing a custom/unsupported metadata provider or path type to a pkg_resources API that expects a registered adapter, or an outdated vendored copy whose registry was not populated.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/1d577a806beff210.json. Report an issue: GitHub.