{"id":"1d577a806beff210","repo":"pypa/pip","slug":"could-not-find-adapter-for-registry-and-ob","errorCode":null,"errorMessage":"Could not find adapter for {registry} and {ob}","messagePattern":"Could not find adapter for (.+?) and (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pkg_resources/__init__.py","lineNumber":3498,"sourceCode":"def _always_object(classes):\n    \"\"\"\n    Ensure object appears in the mro even\n    for old-style classes.\n    \"\"\"\n    if object not in classes:\n        return classes + (object,)\n    return classes\n\n\ndef _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT:\n    \"\"\"Return an adapter factory for `ob` from `registry`\"\"\"\n    types = _always_object(inspect.getmro(getattr(ob, '__class__', type(ob))))\n    for t in types:\n        if t in registry:\n            return registry[t]\n    # _find_adapter would previously return None, and immediately be called.\n    # So we're raising a TypeError to keep backward compatibility if anyone depended on that behaviour.\n    raise TypeError(f\"Could not find adapter for {registry} and {ob}\")\n\n\ndef ensure_directory(path: StrOrBytesPath):\n    \"\"\"Ensure that the parent directory of `path` exists\"\"\"\n    dirname = os.path.dirname(path)\n    os.makedirs(dirname, exist_ok=True)\n\n\ndef _bypass_ensure_directory(path):\n    \"\"\"Sandbox-bypassing version of ensure_directory()\"\"\"\n    if not WRITE_SUPPORT:\n        raise OSError('\"os.mkdir\" not supported on this platform.')\n    dirname, filename = split(path)\n    if dirname and filename and not isdir(dirname):\n        _bypass_ensure_directory(dirname)\n        try:\n            mkdir(dirname, 0o755)\n        except FileExistsError:","sourceCodeStart":3480,"sourceCodeEnd":3516,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pkg_resources/__init__.py#L3480-L3516","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect which object type triggered the lookup and ensure you pass a supported built-in type (e.g. a real Distribution/provider).","If extending pkg_resources, register your custom class in the relevant adapter registry before invoking the API.","Upgrade pkg_resources/setuptools, since missing registrations are usually a version/compatibility bug."],"exampleFix":"# before\nadapter = _find_adapter(registry, my_custom_obj)  # TypeError\n\n# after\nfrom pip._vendor.pkg_resources import _find_adapter\nif not any(t in registry for t in type(my_custom_obj).__mro__):\n    raise TypeError('unsupported object; register an adapter')\nadapter = _find_adapter(registry, my_custom_obj)","handlingStrategy":"validation","validationCode":"def adapter_available(registry, ob) -> bool:\n    import inspect\n    mro = inspect.getmro(getattr(ob, '__class__', type(ob)))\n    return any(t in registry for t in mro)\nif adapter_available(registry, obj):\n    _find_adapter(registry, obj)","typeGuard":"def has_registered_type(registry, ob) -> bool:\n    return any(t in registry for t in type(ob).__mro__)","tryCatchPattern":"try:\n    adapter = _find_adapter(registry, ob)\nexcept TypeError as e:\n    if 'Could not find adapter' in str(e):\n        # register or use a default adapter\n        ...\n    raise","preventionTips":["Treat _find_adapter as internal; avoid passing unregistered custom types.","Register custom classes in the adapter registry before lookup."],"tags":["python","pkg-resources","adapter","registry","internal"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}