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

Avoid import-time dependency ontyping in common cases#329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Sachaa-Thanasius wants to merge10 commits intopython:main
base:main
Choose a base branch
Loading
fromSachaa-Thanasius:avoid-runtime-typing-in-common-cases

Conversation

@Sachaa-Thanasius
Copy link

@Sachaa-ThanasiusSachaa-Thanasius commentedApr 7, 2025
edited
Loading

Part of#326.

Overview

typing as a import-time dependency can be avoided for most of this library's use cases through a combination of the following:

  • Refactoring when possible to avoid using constructs that often importtyping.
    • e.g.functools.singledispatch
  • Isolation of components that unavoidably depend ontyping at import time, so that importing them is opt-in.
    • e.g.importlib_resources.abc.Traversable
  • Deferred annotations.
  • Lazy imports via module-level__getattr__.

This way, when importing just the base or functional API (files(),as_file(),open_binary, etc.), which I imagine is a very common use case, users don't also importtyping immediately as a side-effect. This will improve the import time ofimportlib_resources for such common cases by at least 10%, possibly more, while still having the type annotations be valid and resolve to the correct symbols when introspected/evaluated.

This PR's intended viewing is commit by commit, in order.

Notes

I'm guessing this one will be a bit more controversial than the other PRs due to refactoring away usage offunctools.singledispatch, but if we want to usetyping without incurring its import cost on multiple Python versions, I think it's easiest to just avoidsingledispatch altogether inimportlib_resources._common.singledispatch usage is also a potential blocker for possible future improvements, e.g. lazily importingpathlib.Path.

That being said, I'll try to see if there's a way to usesingledispatch for the moment without it unconditionally importingtyping. Doing so would rely on implementation details and thus be fragile at best, so I'm not a fan of it.

…me usage of typing constructs (in this case, Protocol).
…urces.abc to 1) publicly export Traversable from importlib_resources._traversable and 2) use Traversable in internal annotations, all without importing it eagerly at import time.
…s been removed from importlib_resources.abc, do the same for importlib_resources._common by avoiding typing.cast and avoiding eager import and execution of symbols from importlib_resources.abc.
…ols. This will be used shortly to avoid import-time typing imports in importlib_resources_common and importlib_resources.abc.
…e previously created _typing shim in conjunction with deferred annotations. This way, importlib_resources.abc never imports typing at import time.
…g the previously created _typing shim in conjunction with deferred annotations. This way, importlib_resources._common never imports typing at import time.
…tions to complement the __getattr__ functions. Also, improve __getattr__ comments and ensure those functions have the same structure.
…ces.__init__, since that's where it's eagerly imported from.
…s._common to not use functools.singledispatch, since singledispatch will always import typing.
@Sachaa-ThanasiusSachaa-Thanasius changed the titleAvoid runtime typing in common casesAvoid import-time dependency ontyping in common casesApr 7, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@Sachaa-Thanasius

[8]ページ先頭

©2009-2025 Movatter.jp