Skip to content

Avoid import-time dependency on typing 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Sachaa-Thanasius
Copy link

@Sachaa-Thanasius Sachaa-Thanasius commented Apr 7, 2025

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 import typing.
    • e.g. functools.singledispatch
  • Isolation of components that unavoidably depend on typing 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 import typing immediately as a side-effect. This will improve the import time of importlib_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 of functools.singledispatch, but if we want to use typing without incurring its import cost on multiple Python versions, I think it's easiest to just avoid singledispatch altogether in importlib_resources._common. singledispatch usage is also a potential blocker for possible future improvements, e.g. lazily importing pathlib.Path.

That being said, I'll try to see if there's a way to use singledispatch for the moment without it unconditionally importing typing. 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-Thanasius Sachaa-Thanasius changed the title Avoid runtime typing in common cases Avoid import-time dependency on typing in common cases Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant