Skip to content

Add some typings informations and typecheck #589

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

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ matrix:
- python: 3.8
dist: xenial
sudo: true
env: MYPY_CHECK='true'

before_install:
- docker pull arafato/azurite
Expand All @@ -38,11 +39,12 @@ before_script:
- mongo mydb_test --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'

install:
- pip install -U pip setuptools wheel tox-travis coveralls
- pip install -U pip setuptools wheel tox-travis coveralls mypy

script:
- tox
- if [[ $BUILD_DOCS == 'true' ]]; then tox -e docs; fi
- if [[ $MYPY_CHECK == 'true' ]]; then mypy zarr; fi

after_success:
- coveralls --service=travis-pro
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Next release
without ``ipytree`` installed.
By :user:`Zain Patel <mzjp2>`; :issue:`537`

* Add typing informations to many of the core functions :issue:`589`

* Explicitly close stores during testing.
By :user:`Elliott Sales de Andrade <QuLogic>`; :issue:`442`

Expand Down
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]
python_version = 3.6
ignore_missing_imports = True
follow_imports = silent
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS IGNORE_EXCEPTION_DETAIL
addopts = --durations=10
filterwarnings =
error::DeprecationWarning:zarr.*

ignore:PY_SSIZE_T_CLEAN will be required.*:DeprecationWarning
2 changes: 1 addition & 1 deletion requirements_dev_optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pymongo==3.9.0
tox==3.14.0
coverage==5.0.3
coveralls==1.11.1
flake8==3.7.8
flake8==3.8.3
pytest-cov==2.7.1
pytest-doctestplus==0.4.0
pytest-remotedata==0.3.2
Expand Down
1 change: 1 addition & 0 deletions zarr/codecs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# flake8: noqa
from numcodecs import *
from numcodecs import get_codec, Blosc, Zlib, Delta, AsType, BZ2
from numcodecs.registry import codec_registry
2 changes: 1 addition & 1 deletion zarr/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def __iter__(self):
yield ChunkDimProjection(dim_chunk_ix, dim_chunk_sel, dim_out_sel)


def slice_to_range(s, l):
def slice_to_range(s, l): # noqa: E741
return range(*s.indices(l))


Expand Down
20 changes: 11 additions & 9 deletions zarr/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from zarr.errors import MetadataError
from zarr.util import json_dumps, json_loads

from typing import Union, Any, List, Mapping as MappingType

ZARR_FORMAT = 2


def parse_metadata(s):
def parse_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:

# Here we allow that a store may return an already-parsed metadata object,
# or a string of JSON that we will parse here. We allow for an already-parsed
Expand All @@ -28,7 +30,7 @@ def parse_metadata(s):
return meta


def decode_array_metadata(s):
def decode_array_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:
meta = parse_metadata(s)

# check metadata format
Expand Down Expand Up @@ -56,7 +58,7 @@ def decode_array_metadata(s):
return meta


def encode_array_metadata(meta):
def encode_array_metadata(meta: MappingType[str, Any]) -> bytes:
dtype = meta['dtype']
sdshape = ()
if dtype.subdtype is not None:
Expand All @@ -74,27 +76,27 @@ def encode_array_metadata(meta):
return json_dumps(meta)


def encode_dtype(d):
def encode_dtype(d: np.dtype) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

see #700

if d.fields is None:
return d.str
else:
return d.descr


def _decode_dtype_descr(d):
def _decode_dtype_descr(d) -> List[Any]:
# need to convert list of lists to list of tuples
if isinstance(d, list):
# recurse to handle nested structures
d = [(k[0], _decode_dtype_descr(k[1])) + tuple(k[2:]) for k in d]
return d


def decode_dtype(d):
def decode_dtype(d) -> np.dtype:
d = _decode_dtype_descr(d)
return np.dtype(d)


def decode_group_metadata(s):
def decode_group_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:
meta = parse_metadata(s)

# check metadata format version
Expand All @@ -108,7 +110,7 @@ def decode_group_metadata(s):

# N.B., keep `meta` parameter as a placeholder for future
# noinspection PyUnusedLocal
def encode_group_metadata(meta=None):
def encode_group_metadata(meta=None) -> bytes:
meta = dict(
zarr_format=ZARR_FORMAT,
)
Expand Down Expand Up @@ -161,7 +163,7 @@ def decode_fill_value(v, dtype):
return np.array(v, dtype=dtype)[()]


def encode_fill_value(v, dtype):
def encode_fill_value(v: Any, dtype: np.dtype) -> Any:
# early out
if v is None:
return v
Expand Down
Loading