Skip to content

Commit 3f01368

Browse files
committed
Add some typings informations and typecheck
Mostly make it a bit clearer what is allowed/expected where. In particular many of the decode functions allow already decoded objects. Add Typing to store. Note in particular that some function can get bytes/str/anything that can be stringified though others only can get str/None as they don't call normalize on path.
1 parent 35d709a commit 3f01368

File tree

10 files changed

+120
-64
lines changed

10 files changed

+120
-64
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ matrix:
2929
- python: 3.8
3030
dist: xenial
3131
sudo: true
32+
env: MYPY_CHECK='true'
3233

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

4041
install:
41-
- pip install -U pip setuptools wheel tox-travis coveralls
42+
- pip install -U pip setuptools wheel tox-travis coveralls mypy
4243

4344
script:
4445
- tox
4546
- if [[ $BUILD_DOCS == 'true' ]]; then tox -e docs; fi
47+
- if [[ $MYPY_CHECK == 'true' ]]; then mypy zarr; fi
4648

4749
after_success:
4850
- coveralls --service=travis-pro

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Next release
1414
without ``ipytree`` installed.
1515
By :user:`Zain Patel <mzjp2>`; :issue:`537`
1616

17+
* Add typing informations to many of the core functions :issue:`589`
18+
1719
* Explicitly close stores during testing.
1820
By :user:`Elliott Sales de Andrade <QuLogic>`; :issue:`442`
1921

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
python_version = 3.6
3+
ignore_missing_imports = True
4+
follow_imports = silent

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS IGNORE_EXCEPTION_DETAIL
33
addopts = --durations=10
44
filterwarnings =
55
error::DeprecationWarning:zarr.*
6-
6+
ignore:PY_SSIZE_T_CLEAN will be required.*:DeprecationWarning

requirements_dev_optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pymongo==3.9.0
1313
tox==3.14.0
1414
coverage==5.0.3
1515
coveralls==1.11.1
16-
flake8==3.7.8
16+
flake8==3.8.3
1717
pytest-cov==2.7.1
1818
pytest-doctestplus==0.4.0
1919
pytest-remotedata==0.3.2

zarr/codecs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# flake8: noqa
33
from numcodecs import *
4+
from numcodecs import get_codec, Blosc, Zlib, Delta, AsType, BZ2
45
from numcodecs.registry import codec_registry

zarr/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def __iter__(self):
468468
yield ChunkDimProjection(dim_chunk_ix, dim_chunk_sel, dim_out_sel)
469469

470470

471-
def slice_to_range(s, l):
471+
def slice_to_range(s, l): # noqa: E741
472472
return range(*s.indices(l))
473473

474474

zarr/meta.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from zarr.errors import MetadataError
88
from zarr.util import json_dumps, json_loads
99

10+
from typing import Union, Any, List, Mapping as MappingType
11+
1012
ZARR_FORMAT = 2
1113

1214

13-
def parse_metadata(s):
15+
def parse_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:
1416

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

3032

31-
def decode_array_metadata(s):
33+
def decode_array_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:
3234
meta = parse_metadata(s)
3335

3436
# check metadata format
@@ -56,7 +58,7 @@ def decode_array_metadata(s):
5658
return meta
5759

5860

59-
def encode_array_metadata(meta):
61+
def encode_array_metadata(meta: MappingType[str, Any]) -> bytes:
6062
dtype = meta['dtype']
6163
sdshape = ()
6264
if dtype.subdtype is not None:
@@ -74,27 +76,27 @@ def encode_array_metadata(meta):
7476
return json_dumps(meta)
7577

7678

77-
def encode_dtype(d):
79+
def encode_dtype(d: np.dtype) -> str:
7880
if d.fields is None:
7981
return d.str
8082
else:
8183
return d.descr
8284

8385

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

9193

92-
def decode_dtype(d):
94+
def decode_dtype(d) -> np.dtype:
9395
d = _decode_dtype_descr(d)
9496
return np.dtype(d)
9597

9698

97-
def decode_group_metadata(s):
99+
def decode_group_metadata(s: Union[MappingType, str]) -> MappingType[str, Any]:
98100
meta = parse_metadata(s)
99101

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

109111
# N.B., keep `meta` parameter as a placeholder for future
110112
# noinspection PyUnusedLocal
111-
def encode_group_metadata(meta=None):
113+
def encode_group_metadata(meta=None) -> bytes:
112114
meta = dict(
113115
zarr_format=ZARR_FORMAT,
114116
)
@@ -161,7 +163,7 @@ def decode_fill_value(v, dtype):
161163
return np.array(v, dtype=dtype)[()]
162164

163165

164-
def encode_fill_value(v, dtype):
166+
def encode_fill_value(v: Any, dtype: np.dtype) -> Any:
165167
# early out
166168
if v is None:
167169
return v

0 commit comments

Comments
 (0)