Use lazy.attach_stub for lazy imports, to accommodate type checkers and IDEs#6435
Closed
tlambert03 wants to merge 8 commits intoscikit-image:mainfrom
Closed
Use lazy.attach_stub for lazy imports, to accommodate type checkers and IDEs#6435tlambert03 wants to merge 8 commits intoscikit-image:mainfrom
lazy.attach_stub for lazy imports, to accommodate type checkers and IDEs#6435tlambert03 wants to merge 8 commits intoscikit-image:mainfrom
Conversation
lazy.attach_stub for lazy imports, to accommodate type checkers and IDEs
jni
approved these changes
Jul 13, 2022
Member
|
One thing I hadn't quite considered is that this is now a semi-clean way to enforce that the module-level API is the public API, and things not in the stub will work but be hard to find, right? |
Contributor
Author
|
Yes.... but, I'm not sure I'm quite following your thought: are you contrasting that with the previous setup? |
Member
|
Thank you @tlambert03! @jni are you still happy with this one? I'll merge if you are. |
Member
|
Implemented in #6577 using |
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
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.
Description
This is an alternative to #6429 that uses
pyitype stubs instead of an if TYPE_CHECKING clause. At runtime, when a module is imported, it will look for an adjacent.pyi, parse the abstract syntax tree, and pass the detected modules and sumodule attrs to the regularlazy.attach()function. The benefits here are that:from *. importsyntax.if TYPE_CHECKINGSome caveats to be aware of:
setup.pyhere to that effect)lazy.attach(), this currently only supports within-module exports. Meaning you can't use a relative import that goes beyond the modulefrom ..something import.lazy.attachalso doesn't support this, but it's a bit easier to accidentally do it with a type stub since the syntax is more familiar. In my implementation, attempting this will raise an exception at runtime, so it will be easy to catch with tests.3.__all__clauses are unfortunately required because type checkings treat stub imports as implicit: https://mypy.readthedocs.io/en/stable/config_file.html#confval-implicit_reexport. It would still "work" without the__all__clause, but you would get no suggestions when you dofrom skimage.filters import ...(this pattern was also added as a PR to lazy_loader: scientific-python/lazy-loader#10 and is vendored here)
Checklist
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.example, to backport to v0.19.x after merging, add the following in a PR
comment:
@meeseeksdev backport to v0.19.xrun-benchmarklabel. To rerun, the labelcan be removed and then added again. The benchmark output can be checked in
the "Actions" tab.