Skip to content

Commit

Permalink
Add type annotations in proxy.py
Browse files Browse the repository at this point in the history
  • Loading branch information
martaiborra committed Sep 13, 2024
1 parent 8f15af7 commit 35cba42
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/blosc2/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from abc import ABC, abstractmethod

import blosc2
import numpy as np


class ProxySource(ABC):
Expand All @@ -24,7 +25,7 @@ class ProxySource(ABC):
"""

@abstractmethod
def get_chunk(self, nchunk):
def get_chunk(self, nchunk: int) -> bytes:
"""
Return the compressed chunk in :paramref:`self`.
Expand All @@ -48,15 +49,15 @@ class Proxy(blosc2.Operand):
which follows the :ref:`ProxySource` interface in an urlpath.
"""

def __init__(self, src, urlpath=None, **kwargs):
def __init__(self, src: ProxySource, urlpath: str = None, **kwargs: dict):
"""
Create a new :ref:`Proxy` to serve like a cache to save accessed
chunks locally.
Parameters
----------
src: :ref:`ProxySource`
The original container
The original container.
urlpath: str, optional
The urlpath where to save the container that will work as a cache.
Expand Down Expand Up @@ -115,7 +116,7 @@ def __init__(self, src, urlpath=None, **kwargs):
for key in vlmeta:
self._schunk_cache.vlmeta[key] = vlmeta[key]

def fetch(self, item=None):
def fetch(self, item: slice | list[slice] = None) -> blosc2.NDArray | blosc2.schunk.SChunk:
"""
Get the container used as cache with the requested data updated.
Expand Down Expand Up @@ -146,7 +147,7 @@ def fetch(self, item=None):

return self._cache

async def afetch(self, item=None):
async def afetch(self, item: slice | list[slice] = None) -> blosc2.NDArray | blosc2.schunk.SChunk:
"""
Get the container used as cache with the requested data updated
in an asynchronous way.
Expand Down Expand Up @@ -185,7 +186,7 @@ async def afetch(self, item=None):

return self._cache

def __getitem__(self, item):
def __getitem__(self, item: slice | list[slice]) -> np.ndarray:
"""
Get a slice as a numpy.ndarray using the :ref:`Proxy`.
Expand All @@ -204,20 +205,20 @@ def __getitem__(self, item):
return self._cache[item]

@property
def dtype(self):
def dtype(self) -> np.dtype:
"""The dtype of :paramref:`self` or None if the data is unidimensional"""
return self._cache.dtype if isinstance(self._cache, blosc2.NDArray) else None

@property
def shape(self):
def shape(self) -> tuple[int]:
"""The shape of :paramref:`self`"""
return self._cache.shape if isinstance(self._cache, blosc2.NDArray) else len(self._cache)

def __str__(self):
return f"Proxy({self.src}, urlpath={self.urlpath})"

@property
def vlmeta(self):
def vlmeta(self) -> blosc2.schunk.vlmeta:
"""
Get the vlmeta of the cache.
Expand All @@ -228,7 +229,7 @@ def vlmeta(self):
return self._schunk_cache.vlmeta

@property
def fields(self):
def fields(self) -> dict:
"""
Dictionary with the fields of :paramref:`self`.
Expand All @@ -254,7 +255,7 @@ def __init__(self, proxy: Proxy, field: str):
self.shape = proxy.shape
self.dtype = proxy.dtype

def __getitem__(self, item: slice):
def __getitem__(self, item: slice | list[slice]) -> np.ndarray:
"""
Get a slice as a numpy.ndarray using the `field` in `proxy`.
Expand Down

0 comments on commit 35cba42

Please sign in to comment.