Skip to content

Remove deprecation notes for non-deprecated importlib.resources functions #139344

Closed

Description

@Timmmm

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

Activity

  1. picnixz commented on Sep 26, 2025

    @picnixz
    Member

    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

  2. changed the title [-]Are importlib.read_text etc. really deprecated?[/-] [+]Remove deprecation notes for non-deprecated `importlib.resources` functions[/+] on Sep 26, 2025
  3. gpshead commented on Nov 11, 2025

    @gpshead
    Member
  4. added a commit that references this issue on Nov 13, 2025
  5. encukou commented on Nov 13, 2025

    @encukou
    Member

    Thanks for the catch! They've indeed been un-deprecated.

  6. added a commit that references this issue on Nov 14, 2025
  7. added 2 commits that reference this issue on Nov 14, 2025
  8. added 2 commits that reference this issue on Nov 14, 2025
  9. added a commit that references this issue on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions