Skip to content

Commit

Permalink
Disallow incomplete type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Apr 24, 2024
1 parent 0f755cc commit 463dfe8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ check_untyped_defs = true
disallow_untyped_decorators = true
disallow_any_generics = true

disallow_incomplete_defs = true

[[tool.mypy.overrides]]
module = [
"zarr.v2._storage.store",
Expand Down Expand Up @@ -196,6 +198,13 @@ module = [
]
disallow_any_generics = false

[[tool.mypy.overrides]]
module = [
"zarr.v2.*",
"zarr.array_v2",
"zarr.group"
]
disallow_incomplete_defs = false

[tool.pytest.ini_options]
doctest_optionflags = [
Expand Down
12 changes: 6 additions & 6 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from zarr.sync import sync


def parse_array_metadata(data: Any):
def parse_array_metadata(data: Any) -> ArrayMetadata:
if isinstance(data, ArrayMetadata):
return data
elif isinstance(data, dict):
Expand Down Expand Up @@ -192,7 +192,7 @@ def dtype(self) -> np.dtype:
def attrs(self) -> dict:
return self.metadata.attributes

async def getitem(self, selection: Selection):
async def getitem(self, selection: Selection) -> np.ndarray:
assert isinstance(self.metadata.chunk_grid, RegularChunkGrid)
indexer = BasicIndexer(
selection,
Expand Down Expand Up @@ -231,7 +231,7 @@ async def _read_chunk(
chunk_selection: SliceSelection,
out_selection: SliceSelection,
out: np.ndarray,
):
) -> None:
chunk_spec = self.metadata.get_chunk_spec(chunk_coords)
chunk_key_encoding = self.metadata.chunk_key_encoding
chunk_key = chunk_key_encoding.encode_chunk_key(chunk_coords)
Expand Down Expand Up @@ -301,7 +301,7 @@ async def _write_chunk(
chunk_coords: ChunkCoords,
chunk_selection: SliceSelection,
out_selection: SliceSelection,
):
) -> None:
chunk_spec = self.metadata.get_chunk_spec(chunk_coords)
chunk_key_encoding = self.metadata.chunk_key_encoding
chunk_key = chunk_key_encoding.encode_chunk_key(chunk_coords)
Expand Down Expand Up @@ -350,7 +350,7 @@ async def _write_chunk(

async def _write_chunk_to_store(
self, store_path: StorePath, chunk_array: np.ndarray, chunk_spec: ArraySpec
):
) -> None:
if np.all(chunk_array == self.metadata.fill_value):
# chunks that only contain fill_value will be removed
await store_path.delete()
Expand Down Expand Up @@ -514,7 +514,7 @@ def metadata(self) -> ArrayMetadata:
def store_path(self) -> StorePath:
return self._async_array.store_path

def __getitem__(self, selection: Selection):
def __getitem__(self, selection: Selection) -> np.ndarray:
return sync(
self._async_array.getitem(selection),
self._async_array.runtime_configuration.asyncio_loop,
Expand Down
4 changes: 2 additions & 2 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def create_empty(cls, chunks_per_shard: ChunkCoords) -> _ShardBuilder:
obj.index = _ShardIndex.create_empty(chunks_per_shard)
return obj

def append(self, chunk_coords: ChunkCoords, value: BytesLike):
def append(self, chunk_coords: ChunkCoords, value: BytesLike) -> None:
chunk_start = len(self.buf)
chunk_length = len(value)
self.buf.extend(value)
Expand Down Expand Up @@ -424,7 +424,7 @@ async def _read_chunk(
shard_spec: ArraySpec,
runtime_configuration: RuntimeConfiguration,
out: np.ndarray,
):
) -> None:
chunk_spec = self._get_chunk_spec(shard_spec)
chunk_bytes = shard_dict.get(chunk_coords, None)
if chunk_bytes is not None:
Expand Down
6 changes: 3 additions & 3 deletions src/zarr/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _ensure_tuple(v: Selection) -> SliceSelection:
return v


def _err_too_many_indices(selection: SliceSelection, shape: ChunkCoords):
def _err_too_many_indices(selection: SliceSelection, shape: ChunkCoords) -> None:
raise IndexError(
"too many indices for array; expected {}, got {}".format(len(shape), len(selection))
)
Expand All @@ -23,7 +23,7 @@ def _err_negative_step():
raise IndexError("only slices with step >= 1 are supported")


def _check_selection_length(selection: SliceSelection, shape: ChunkCoords):
def _check_selection_length(selection: SliceSelection, shape: ChunkCoords) -> None:
if len(selection) > len(shape):
_err_too_many_indices(selection, shape)

Expand Down Expand Up @@ -179,7 +179,7 @@ def c_order_iter(chunks_per_shard: ChunkCoords) -> Iterator[ChunkCoords]:
return itertools.product(*(range(x) for x in chunks_per_shard))


def is_total_slice(item: Selection, shape: ChunkCoords):
def is_total_slice(item: Selection, shape: ChunkCoords) -> bool:
"""Determine whether `item` specifies a complete slice of array with the
given `shape`. Used to optimize __setitem__ operations on the Chunk
class."""
Expand Down
3 changes: 2 additions & 1 deletion src/zarr/store/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def _put(
value: BytesLike,
start: Optional[int] = None,
auto_mkdir: bool = True,
):
) -> int | None:
if auto_mkdir:
path.parent.mkdir(parents=True, exist_ok=True)
if start is not None:
with path.open("r+b") as f:
f.seek(start)
f.write(value)
return None
else:
return path.write_bytes(value)

Expand Down

0 comments on commit 463dfe8

Please sign in to comment.