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

mypy implementation for adapters in tiled #700

Merged
merged 45 commits into from
Apr 26, 2024

Conversation

skarakuzu
Copy link
Contributor

beginning of mypy implementation in tiled. It is not ready to merge.

@danielballan
Copy link
Member

Protocols that we should write up together:

  • metadata, which is like dict[str, Any] but more specific than Any because we know which types are JSON-serializable, and metadata must be JSON-serializable
  • AccessPolicy
  • BaseAdapter (general interface, includes things like metadata(), specs, structure_family, structure
  • Separate Protocol for each structure family: ArrayAdapter, AwkwardAdapter, ContainerAdapter, SparseAdapter, TableAdapter

@skarakuzu skarakuzu force-pushed the mypy_for_tiled branch 2 times, most recently from e317207 to 03b2408 Compare April 5, 2024 18:44
):
metadata: Optional[JSON] = None,
specs: Optional[List[Spec]] = None,
access_policy: Optional[Union[SimpleAccessPolicy, DummyAccessPolicy]] = None,
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a good place to use the AccessPolicy protocol. Same for everywhere else this is used.

array = self._array
if slice is not None:
array = array[slice]
array = array[slice]
Copy link
Member

Choose a reason for hiding this comment

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

Might as well combine this into a single statement.

def read(
self,
slice: Union[
int, slice, Tuple[Union[int, slice, EllipsisType], ...], EllipsisType
Copy link
Member

Choose a reason for hiding this comment

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

I seems worthwhile to make a "type alias" for this, like you did for JSON. I suggest the name NDSlice.

def slice_and_shape_from_block_and_chunks(block, chunks):
def slice_and_shape_from_block_and_chunks(
block: Tuple[int, ...], chunks: Tuple[Tuple[int, ...], ...]
) -> Tuple[Tuple[slice, ...], Tuple[int, ...]]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
) -> Tuple[Tuple[slice, ...], Tuple[int, ...]]:
) -> Tuple[NDSlice, Tuple[int, ...]]:

return self._metadata

def read_buffers(self, form_keys=None):
def read_buffers(self, form_keys: Optional[List[str]] = None) -> Dict[str, Any]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def read_buffers(self, form_keys: Optional[List[str]] = None) -> Dict[str, Any]:
def read_buffers(self, form_keys: Optional[List[str]] = None) -> Dict[str, bytes]:

if len(self.blocks) > 1:
raise NotImplementedError
uri = self.blocks[(0,) * len(self._structure.shape)]
data.to_parquet(path_from_uri(uri))

def read(self, slice=...):
def read(self, slice: Optional[Union[int, slice]]) -> NDArray[Any]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def read(self, slice: Optional[Union[int, slice]]) -> NDArray[Any]:
def read(self, slice: Optional[Union[int, slice]]) -> sparse.COO:

@@ -22,13 +26,23 @@ class TiffAdapter:

def __init__(
self,
data_uri,
data_uri: Union[str, List[str]],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data_uri: Union[str, List[str]],
data_uri: str,


INLINED_DEPTH = int(os.getenv("TILED_HDF5_INLINED_CONTENTS_MAX_DEPTH", "7"))


def read_zarr(data_uri, structure=None, **kwargs):
def read_zarr(
data_uri: Union[str, List[str]],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data_uri: Union[str, List[str]],
data_uri: str,

def read_block(self, block, slice=None):
def read_block(
self, block: Tuple[int, ...], slice: Optional[Union[int, slice]] = None
) -> NDArray[Any]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
) -> NDArray[Any]:
) -> sparse.COO:

coords, data = self.blocks[block]
_, shape = slice_and_shape_from_block_and_chunks(block, self._structure.chunks)
arr = sparse.COO(data=data[:], coords=coords[:], shape=shape)
if slice:
arr = arr[slice]
return arr

def read(self, slice=None):
def read(self, slice: Optional[Union[int, slice]] = None) -> NDArray[Any]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def read(self, slice: Optional[Union[int, slice]] = None) -> NDArray[Any]:
def read(self, slice: Optional[Union[int, slice]] = None) -> sparse.COO:

@skarakuzu skarakuzu changed the title started mypy implementation for tiled: Note lots of mypy errors! mypy implementation for adapters in tiled Apr 26, 2024
@skarakuzu skarakuzu marked this pull request as ready for review April 26, 2024 15:36
pass


if sys.version_info < (3, 9):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this PR the time to upgrade Tiled's minimum supported python version to SPEC0, which at present would be 3.10 or greater? We could then remove this Mapping stuff.

Copy link
Member

Choose a reason for hiding this comment

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

One or two users out there have asked if we can give them a 3.8 (or even a 3.7) compatible release up through the first beta, so they have at least a basically usable client available in legacy environments.

I figure we'll fast-forward to 3.10 shortly after tagging beta.

@danielballan danielballan merged commit 143dd22 into bluesky:main Apr 26, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants