Skip to content

Use annotations from future (PEP563) to solve type hint references - #1600

Merged
h-mayorquin merged 1 commit into
SpikeInterface:mainfrom
catalystneuro:fix_real_nwb
Apr 28, 2023
Merged

h-mayorquin merged 1 commit into
SpikeInterface:mainfrom
catalystneuro:fix_real_nwb

Conversation

@h-mayorquin

@h-mayorquin h-mayorquin commented Apr 28, 2023 •

Copy link
Copy Markdown
Contributor

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:

from __future__ import annotations

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

@h-mayorquin h-mayorquin added installation Installation issues extractors Related to extractors module labels Apr 28, 2023
@h-mayorquin h-mayorquin self-assigned this Apr 28, 2023
@h-mayorquin h-mayorquin changed the title use from future Use annotations from future (PEP563) to solve type hint references Apr 28, 2023
@h-mayorquin
h-mayorquin merged commit d5a1ba4 into SpikeInterface:main Apr 28, 2023
@h-mayorquin
h-mayorquin deleted the fix_real_nwb branch April 28, 2023 15:47
@JoeZiminski

Copy link
Copy Markdown
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?

@h-mayorquin

Copy link
Copy Markdown
Contributor Author

@alejoe91
Can you pile up your take here:
#1384

I think I was proposing someting similar in the first point of the list on there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extractors Related to extractors module installation Installation issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

import spikeinterface.extractors fails if NWB not installed

3 participants