Skip to content

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

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

Activity

  1. brettcannon commented on Aug 8, 2025

    @brettcannon
    Member
    • Is this to be considered a bug? Or is SourceLoader supposed to be deprecated too?

    It's a bug in so much as SourceLoader isn't deprecated.

    • If the former, how can it be fixed?

    One option is for SourceLoader to stop inheriting from ResourceLoader and copy over the abstract method from it (as well as update the docstring and documentation).

    What I'm wondering about, though, is why didn't the tests catch this?

  2. brettcannon commented on Aug 8, 2025

    @brettcannon
    Member

    @hugovk not sure if this should be a release blocker, made it one just in case since the fix might be annoying in a point release if we change the inheritance structure.

  3. brettcannon commented on Aug 8, 2025

    @brettcannon
    Member

    I found my mistake: I merged 27494dd probably thinking the warning suppression was for tests of ResourceLoader instead of SourceLoader.

  4. brettcannon commented on Aug 8, 2025

    @brettcannon
    Member

    Another option is to drop the warning on ResourceLoader since it has no removal date and it isn't hurting anything having it there.

  5. added a commit that references this issue on Aug 8, 2025
  6. brettcannon commented on Aug 8, 2025

    @brettcannon
    Member

    #137567 reverts the deprecation of ResourceLoader if that's the direction we choose to go.

  7. self-assigned this
    on Aug 8, 2025
  8. added a commit that references this issue on Aug 11, 2025
  9. added a commit that references this issue on Aug 11, 2025
  10. brettcannon commented on Aug 11, 2025

    @brettcannon
    Member

    This is now merged into main and awaiting Hugo to cherrypick into 3.14.

  11. TTsangSC commented on Aug 12, 2025

    @TTsangSC
    Author

    Thanks for the fix!

  12. added a commit that references this issue on Aug 12, 2025
  13. hugovk commented on Aug 12, 2025

    @hugovk
    Member

    Makes sense for this to be done for 3.14.0 and not 3.14.1, thanks all!

  14. brettcannon commented on Aug 12, 2025

    @brettcannon
    Member

    @TTsangSC and thanks for testing the RC and reporting the bug!

  15. added a commit that references this issue on Aug 19, 2025
  16. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixes3.15bugs and security fixesrelease-blockerstdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions