Skip to content
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

(fix): move zarr import inside try-catch in test helpers #1343

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np
import pandas as pd
import pytest
import zarr
from pandas.api.types import is_numeric_dtype
from scipy import sparse

Expand Down Expand Up @@ -759,21 +758,30 @@ def shares_memory_sparse(x, y):
),
]

try:
import zarr

class AccessTrackingStore(zarr.DirectoryStore):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._access_count = {}
class AccessTrackingStore(zarr.DirectoryStore):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._access_count = {}

def __getitem__(self, key):
for tracked in self._access_count:
if tracked in key:
self._access_count[tracked] += 1
return super().__getitem__(key)
def __getitem__(self, key):
for tracked in self._access_count:
if tracked in key:
self._access_count[tracked] += 1
return super().__getitem__(key)

def get_access_count(self, key):
return self._access_count[key]
def get_access_count(self, key):
return self._access_count[key]

def set_key_trackers(self, keys_to_track):
for k in keys_to_track:
self._access_count[k] = 0
def set_key_trackers(self, keys_to_track):
for k in keys_to_track:
self._access_count[k] = 0
except ImportError:

class AccessTrackingStore:
def __init__(self) -> None:
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
raise ImportError(
"zarr must be imported to create an `AccessTrackingStore` instance."
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of this, but I think the other option is to create AccessTrackingStore based on some dummy base class, which will error out on construction with a less helpful error. And if we want to raise an error, we're sort of back to this.

Loading