Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Un-deprecate functional API for importlib resources & add subdirectory support #116608

Open
Labels
@encukou

Description

@encukou

Feature or enhancement

Proposal:

Theimportlib.resources functions{open,read}_{text,binary},path,is_resource andcontents, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed by quite a few users.
They provide a simple API for simple tasks, while the full-featuredTraversable API is better suited for complex ones -- especially for implementing new resources-aware loaders.

I'm now in a position where I can add these functions back and support them.

Their main drawback -- not allowing subdirectories -- can be solved by taking multiple path components as positional arguments, for example:

importlib.resources.read_text('modulename','subdirectory','subsubdir','resource.txt')

The additional arguments (encoding anderrors) would become keyword-only.


There is a wrinkle in this: in Python 3.9-3.11, the above would mean:

importlib.resources.read_text('modulename','subdirectory',encoding='subsubdir',errors='resource.txt',)

I believe that this is acceptable, since:

  • pragmatically: typical file names do not match typical encoding/errorhandler names
  • lawyerly: the functions have already been deprecated for 2 releases; no one is using them now, right?

However, if this is a problem, I can

[edit: This is solved by:]

  • make theencoding argumentrequired if a text-reading function more than one path component is given.
  • plan to lift this limitation around 3.15.
importlib.resources.read_text('modulename','subdirectory','subsubdir','resource.txt',encoding='utf-8',)importlib.resources.read_text('modulename','resource.txt')# OKimportlib.resources.read_text('modulename','subdirectory','utf-8')# error

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386/29

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp