Repository navigation
Use annotations from future (PEP563) to solve type hint references - #1600
Merged
Merged
Conversation
This was referenced Apr 28, 2023
Closed
alejoe91
approved these changes
Apr 28, 2023
Contributor
|
Thanks @h-mayorquin this is great solution, it works well now. It seems like a nice idea for the images to pull the most recent version of spikeinterface from PyPI rather than directly from main (if, I understand correctly, that's what it's doing). I don't know much about images but will open issue on this unless reason not to? |
Contributor
Author
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.
OK, so #1597 turn out too big as there were a lot of imports that were exposed naively. I am passing the small patch to fix the import with pynwb here as that one will probably take longer to reivew.
Copying PR text verbatim from here:
This PR resolves #1596.
As described in the issue, importing extractors fails when pynwb is not installed. The problem arises from the newly added type-hints, which are currently evaluated at import time.
PR #1593 suggests a solution that employs forward references, where type hints are defined as strings to force deferred evaluation. While this approach works, we can improve upon it.
Starting with Python 3.8, we can use the following statement at the top of a module to defer the evaluation of type annotations until runtime:
More details can be found in PEP 563: https://peps.python.org/pep-0563/
This change allows us to maintain the same notation regardless of whether the module is installed or not, simplifying code navigation.
Moreover, deferring type hints by default is expected to enhance import performance, which aligns with the long-term goals of the library.
Finally, I've added a test to the core that verifies all major modules can be imported with only a core installation, without raising errors. This test would have helped prevent the current issue. Furthermore, the test will be used to monitor import times, helping us track and reduce them in the future.
The tests and the use of run-time imports are now in #1597