-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Sachaa-Thanasius
wants to merge
10
commits into
python:main
Choose a base branch
from
Sachaa-Thanasius:avoid-runtime-typing-in-common-cases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Avoid import-time dependency on typing
in common cases
#329
Sachaa-Thanasius
wants to merge
10
commits into
python:main
from
Sachaa-Thanasius:avoid-runtime-typing-in-common-cases
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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.
… import annotations is active.
…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.
typing
in common cases
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:typing
.functools.singledispatch
typing
at import time, so that importing them is opt-in.importlib_resources.abc.Traversable
__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 of
functools.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 use
singledispatch
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.