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

importosimportsysimportwarningsfromcollections.abcimportIterablefromimportlib.abcimportSourceLoaderfromimportlib.machineryimportModuleSpecfromtypingimportProtocolclassFinderLike(Protocol):
deffind_spec(self, fullname: str) ->ModuleSpec|None: ...
classMyLoader(SourceLoader):
"""Bare-bone `SourceLoader` subclass, only providing implementations for the abstract methods."""@staticmethoddefget_data(path: os.PathLike|str) ->bytes:
withopen(path, mode='rb') asfobj:
returnfobj.read()
@staticmethoddefget_filename(fullname: str, finders: Iterable[FinderLike] |None=None) ->str:
iffindersisNone:
finders= (
fforfinsys.meta_pathifcallable(getattr(f, 'find_spec', None))
ifnotisinstance(f, MyLoader) # Avoid circular dependence
)
forfinderinfinders:
try:
spec=finder.find_spec(fullname)
ifspecisNone: raiseTypeErrorexcept (TypeError, ImportError):
continueifspec.originisnotNoneandos.path.isfile(spec.origin):
returnstr(spec.origin)
raiseImportError(f'cannot find filename for `{fullname}`')
withwarnings.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.15pre-release feature fixes, bugs and security fixesrelease-blockerstdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions