Remove deprecation notes for non-deprecated importlib.resources functions #139344
Copy link
Copy link
Closed
Labels
Description
I wrote some code using read_text() and a few other functions from importlib and was surprised that Pylint complained they are deprecated. There's no mention of that in the documentation.
Eventually I discovered that the Python 3.13 release notes claim that they are "pending removal" as if they've already been deprecated.
It links to this note which says that files() is "preferred", but are the old methods really deprecated? Are they really going to be removed?
Seems kind of insane to me. The suggested alternative to read_text() is to copy all this code into your project:
def read_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> str:
"""Return the decoded string of the resource.
The decoding-related arguments have the same semantics as those of
bytes.decode().
"""
with open_text(package, resource, encoding, errors) as fp:
return fp.read()
def open_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> TextIO:
"""Return a file-like object opened for text reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open(
'r', encoding=encoding, errors=errors
)
def files(package):
# type: (Package) -> Traversable
"""
Get a Traversable resource from a package
"""
return from_package(get_package(package))
def normalize_path(path):
# type: (Any) -> str
"""Normalize a path by ensuring it is a string.
If the resulting string contains path separators, an exception is raised.
"""
str_path = str(path)
parent, file_name = os.path.split(str_path)
if parent:
raise ValueError(f'{path!r} must be only a file name')
return file_name
def get_resource_reader(package):
# type: (types.ModuleType) -> Optional[ResourceReader]
"""
Return the package's loader if it's a ResourceReader.
"""
# We can't use
# a issubclass() check here because apparently abc.'s __subclasscheck__()
# hook wants to create a weak reference to the object, but
# zipimport.zipimporter does not support weak references, resulting in a
# TypeError. That seems terrible.
spec = package.__spec__
reader = getattr(spec.loader, 'get_resource_reader', None) # type: ignore
if reader is None:
return None
return reader(spec.name) # type: ignore
def resolve(cand):
# type: (Package) -> types.ModuleType
return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)
def get_package(package):
# type: (Package) -> types.ModuleType
"""Take a package name or module object and return the module.
Raise an exception if the resolved module is not a package.
"""
resolved = resolve(package)
if wrap_spec(resolved).submodule_search_locations is None:
raise TypeError(f'{package!r} is not a package')
return resolved
def from_package(package):
"""
Return a Traversable object for the given package.
"""
spec = wrap_spec(package)
reader = spec.loader.get_resource_reader(spec.name)
return reader.files()Linked PRs
- gh-139344: Remove pending removal notice for undeprecated importlib.resources API #141507
- [3.13] gh-139344: Remove pending removal notice for undeprecated importlib.resources API (GH-141507) #141557
- [3.14] gh-139344: Remove pending removal notice for undeprecated importlib.resources API (GH-141507) #141558
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Activity
picnixz commentedon Sep 26, 2025
Actually, they were deprecated in #106532 and then un-deprecated, but I think we forgot to remove the mention of them being deprecated in #116608.
cc @jaraco @graingert
[-]Are importlib.read_text etc. really deprecated?[/-][+]Remove deprecation notes for non-deprecated `importlib.resources` functions[/+]gpshead commentedon Nov 11, 2025
cc @encukou
pythongh-139344: Remove pending removal notice for undeprecated impor…
encukou commentedon Nov 13, 2025
Thanks for the catch! They've indeed been un-deprecated.
gh-139344: Remove pending removal notice for undeprecated importlib.r…
pythongh-139344: Remove pending removal notice for undeprecated impor…
pythongh-139344: Remove pending removal notice for undeprecated impor…
[3.14] gh-139344: Remove pending removal notice for undeprecated impo…
[3.13] gh-139344: Remove pending removal notice for undeprecated impo…
pythongh-139344: Remove pending removal notice for undeprecated impor…