Skip to content

importlib.abc.SourceLoader issues DeprecationWarning because it inherits from ResourceLoader #137426

@TTsangSC

Description

@TTsangSC

Bug report

Bug description:

Problem

  • importlib.abc.SourceLoader itself does not seem to be deprecated as far as the docs go, either in 3.14 or 3.15.
  • However, since it inherits from the deprecated importlib.abc.ResourceLoader, a DeprecationWarning is issued at instantiation time since 3.14.

Example

import os
import sys
import warnings
from collections.abc import Iterable
from importlib.abc import SourceLoader
from importlib.machinery import ModuleSpec
from typing import Protocol


class FinderLike(Protocol):
    def find_spec(self, fullname: str) -> ModuleSpec | None: ...


class MyLoader(SourceLoader):
    """Bare-bone `SourceLoader` subclass, only providing implementations for the abstract methods."""
    @staticmethod
    def get_data(path: os.PathLike | str) -> bytes:
        with open(path, mode='rb') as fobj:
            return fobj.read()

    @staticmethod
    def get_filename(fullname: str, finders: Iterable[FinderLike] | None = None) -> str:
        if finders is None:
            finders = (
                f for f in sys.meta_path
                if callable(getattr(f, 'find_spec', None))
                if not isinstance(f, MyLoader)  # Avoid circular dependence
            )
        for finder in finders:
            try:
                spec = finder.find_spec(fullname)
                if spec is None: raise TypeError
            except (TypeError, ImportError):
                continue
            if spec.origin is not None and os.path.isfile(spec.origin):
                return str(spec.origin)
        raise ImportError(f'cannot find filename for `{fullname}`')


with warnings.catch_warnings():
    warnings.filterwarnings('error', category=DeprecationWarning)
    loader = MyLoader()  # DeprecationWarning: importlib.abc.ResourceLoader is deprecated in favour of supporting resource loading through importlib.resources.abc.TraversableResources.

Questions

  • Is this to be considered a bug? Or is SourceLoader supposed to be deprecated too?
  • If the former, how can it be fixed? If the latter, should the docs be updated?

Possibly related issues

#89710, #121604

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixes3.15new features, bugs and security fixesrelease-blockerstdlibPython modules in the Lib dirtopic-importlibtype-bugAn unexpected behavior, bug, or error

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions