add TYPE_CHECKING hints for lazy imports in data and filters#6429
add TYPE_CHECKING hints for lazy imports in data and filters#6429tlambert03 wants to merge 1 commit intoscikit-image:mainfrom
Conversation
|
Hello @tlambert03! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:
|
|
looks like there's a line length linting error error, but I matched the declarations below exactly (which has the same line length). If i fix it here, i think it should also be fixed below to match. lemme know |
|
Thanks, @tlambert03. It looks like a nice solution! For reference of other reviewers, TYPE_CHECKING is defined when tools like
yeah, the linter only checks modified lines, so that is possible. Please just fix both |
|
@tlambert03 how does this solution compare to napari's .pyi files? Which would you favour more generally? Either way we should apply it to all modules? |
I'm struggling to come up with a strong opinion 😂. I guess the reason I did it this way for this PR is that it's the least "invasive" method for this library (which doesn't yet have any However, I guess i find myself using
I looked around a bit, and I think the only other module is |
|
Oh! I missed that #5101 only applied to
Ech. For me tbh I find the btw have you seen PEP-690??? Not that I'm advocating waiting for it 😂 but its very existence is interesting and exciting! |
see alternative approach building the dynamic imports with a new scientific-python/specs#139 (comment) do you prefer that? |
|
PEP 690 is, indeed, very exciting for the scientific Python stack – most of which suffers from excrutiating startup times. Sadly, that's orthogonal to the issue at hand. PEP 690 won't land in a stable Python release for another year or two; meanwhile, excrutiating startup times beleaguer us all.
You think that until you actually use Instead, embed type hints directly in code and defer to type hint machinery like I should probably admit at this juncture that I unconditionally support everything @tlambert03 does. As the maintainer of a large project that @tlambert03 recently contributed an equally stunning PR to, I am now on Team @tlambert03. I've learned to accept his fearsome prowess. |
lol, you crack me up. Im curious what you think about this crazy pattern: scientific-python/lazy-loader#10 It seems to solve the DRY and desynch woes. What are the caching quandaries to look out for here? If someone changes the pyi in a dev environment, should we expect any stale/missing runtime imports? |
|
Thank you @tlambert03! |
|
This was fixed by #6577 via the pyi-loading mechanism implemented by @tlambert03 (thanks, Talley!). |
Description
#5101 made all imports from data and filters lazy, which is great for import times, but terrible for IDEs.
This PR adds a TYPE_CHECKING clause to each of those modules so linters and IDEs can find the functions again:
current release:

with this PR:

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.