Skip to content

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

@Timmmm

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:

defread_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(). """withopen_text(package, resource, encoding, errors) asfp: returnfp.read() defopen_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 ) deffiles(package): # type: (Package) -> Traversable""" Get a Traversable resource from a package """returnfrom_package(get_package(package)) defnormalize_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) ifparent: raiseValueError(f'{path!r} must be only a file name') returnfile_namedefget_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: ignoreifreaderisNone: returnNonereturnreader(spec.name) # type: ignoredefresolve(cand): # type: (Package) -> types.ModuleTypereturncandifisinstance(cand, types.ModuleType) elseimportlib.import_module(cand) defget_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) ifwrap_spec(resolved).submodule_search_locationsisNone: raiseTypeError(f'{package!r} is not a package') returnresolveddeffrom_package(package): """ Return a Traversable object for the given package. """spec=wrap_spec(package) reader=spec.loader.get_resource_reader(spec.name) returnreader.files()

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions