Skip to content

Commit 383acfc

Browse files
committed
docstrings
1 parent 15ebfa6 commit 383acfc

12 files changed

Lines changed: 3284 additions & 152 deletions

File tree

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
autoapi_member_order = "groupwise"
5757
autoapi_root = "api"
5858
autoapi_keep_files = True
59-
autoapi_options = [ 'members', 'undoc-members', 'show-inheritance', 'show-module-summary', 'imported-members', ]
59+
autoapi_options = [ 'members', 'undoc-members', 'show-inheritance', 'show-module-summary', 'imported-members', 'inherited-members']
6060

6161
def skip_submodules(
6262
app: sphinx.application.Sphinx,
@@ -124,7 +124,7 @@ def skip_submodules(
124124
# List of patterns, relative to source directory, that match files and
125125
# directories to ignore when looking for source files.
126126
# This patterns also effect to html_static_path and html_extra_path
127-
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "talks"]
127+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "talks", "api"]
128128

129129
# The reST default role (used for this markup: `text`) to use for all
130130
# documents.

docs/user-guide/data_types.rst

Lines changed: 176 additions & 52 deletions
Large diffs are not rendered by default.

src/zarr/core/dtype/common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,23 @@ class DataTypeValidationError(ValueError): ...
151151
class ScalarTypeValidationError(ValueError): ...
152152

153153

154-
@dataclass(frozen=True)
154+
@dataclass(frozen=True, kw_only=True)
155155
class HasLength:
156156
"""
157157
A mix-in class for data types with a length attribute, such as fixed-size collections
158158
of unicode strings, or bytes.
159+
160+
Attributes
161+
----------
162+
length : int
163+
The length of the scalars belonging to this data type. Note that this class does not assign
164+
a unit to the length. Child classes may assign units.
159165
"""
160166

161167
length: int
162168

163169

164-
@dataclass(frozen=True)
170+
@dataclass(frozen=True, kw_only=True)
165171
class HasEndianness:
166172
"""
167173
A mix-in class for data types with an endianness attribute
@@ -170,7 +176,7 @@ class HasEndianness:
170176
endianness: EndiannessStr = "little"
171177

172178

173-
@dataclass(frozen=True)
179+
@dataclass(frozen=True, kw_only=True)
174180
class HasItemSize:
175181
"""
176182
A mix-in class for data types with an item size attribute.
@@ -183,7 +189,7 @@ def item_size(self) -> int:
183189
raise NotImplementedError
184190

185191

186-
@dataclass(frozen=True)
192+
@dataclass(frozen=True, kw_only=True)
187193
class HasObjectCodec:
188194
"""
189195
A mix-in class for data types that require an object codec id.

src/zarr/core/dtype/npy/bool.py

Lines changed: 157 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@
2121
@dataclass(frozen=True, kw_only=True, slots=True)
2222
class Bool(ZDType[np.dtypes.BoolDType, np.bool_], HasItemSize):
2323
"""
24-
Wrapper for numpy boolean dtype.
24+
The boolean data type.
2525
2626
Attributes
2727
----------
28-
name : str
29-
The name of the dtype.
30-
dtype_cls : ClassVar[type[np.dtypes.BoolDType]]
31-
The numpy dtype class.
28+
_zarr_v3_name : Literal["bool"] = "bool"
29+
The Zarr v3 name of the dtype.
30+
_zarr_v2_name : Literal["|b1"] = "|b1"
31+
The Zarr v2 name of the dtype, which is also a string representation
32+
of the boolean dtype used by NumPy.
33+
dtype_cls : ClassVar[type[np.dtypes.BoolDType]] = np.dtypes.BoolDType
34+
The NumPy dtype class.
35+
36+
Notes
37+
-----
38+
This class implements the boolean data type defined in Zarr V2 and V3.
39+
You can read the formal specification of that data type in the respective
40+
`specification document <https://zarr-specs.readthedocs.io/en/latest/specs.html>`_
3241
"""
3342

3443
_zarr_v3_name: ClassVar[Literal["bool"]] = "bool"
@@ -38,7 +47,22 @@ class Bool(ZDType[np.dtypes.BoolDType, np.bool_], HasItemSize):
3847
@classmethod
3948
def from_native_dtype(cls, dtype: TBaseDType) -> Self:
4049
"""
41-
Create a Bool from a np.dtype('bool') instance.
50+
Create an instance of Bool from an instance of np.dtypes.BoolDType.
51+
52+
Parameters
53+
----------
54+
dtype : TBaseDType
55+
The NumPy boolean dtype instance to convert.
56+
57+
Returns
58+
-------
59+
Bool
60+
An instance of Bool.
61+
62+
Raises
63+
------
64+
DataTypeValidationError
65+
If the provided dtype is not compatible with this ZDType.
4266
"""
4367
if cls._check_native_dtype(dtype):
4468
return cls()
@@ -48,7 +72,12 @@ def from_native_dtype(cls, dtype: TBaseDType) -> Self:
4872

4973
def to_native_dtype(self: Self) -> np.dtypes.BoolDType:
5074
"""
51-
Create a NumPy boolean dtype instance from this ZDType
75+
Create a NumPy boolean dtype instance from this ZDType.
76+
77+
Returns
78+
-------
79+
np.dtypes.BoolDType
80+
The NumPy boolean dtype.
5281
"""
5382
return self.dtype_cls()
5483

@@ -59,6 +88,16 @@ def _check_json_v2(
5988
) -> TypeGuard[DTypeConfig_V2[Literal["|b1"], None]]:
6089
"""
6190
Check that the input is a valid JSON representation of a Bool.
91+
92+
Parameters
93+
----------
94+
data : DTypeJSON
95+
The JSON data to check.
96+
97+
Returns
98+
-------
99+
bool
100+
True if the input is a valid JSON representation, False otherwise.
62101
"""
63102
return (
64103
check_dtype_spec_v2(data)
@@ -68,17 +107,66 @@ def _check_json_v2(
68107

69108
@classmethod
70109
def _check_json_v3(cls, data: DTypeJSON) -> TypeGuard[Literal["bool"]]:
110+
"""
111+
Check that the input is a valid JSON representation of a Bool in Zarr V3 format.
112+
113+
Parameters
114+
----------
115+
data : DTypeJSON
116+
The JSON data to check.
117+
118+
Returns
119+
-------
120+
bool
121+
True if the input is a valid JSON representation, False otherwise.
122+
"""
71123
return data == cls._zarr_v3_name
72124

73125
@classmethod
74126
def _from_json_v2(cls, data: DTypeJSON) -> Self:
127+
"""
128+
Create an instance of Bool from Zarr V2-flavored JSON.
129+
130+
Parameters
131+
----------
132+
data : DTypeJSON
133+
The JSON data.
134+
135+
Returns
136+
-------
137+
Bool
138+
An instance of Bool.
139+
140+
Raises
141+
------
142+
DataTypeValidationError
143+
If the input JSON is not a valid representation of a Bool.
144+
"""
75145
if cls._check_json_v2(data):
76146
return cls()
77147
msg = f"Invalid JSON representation of {cls.__name__}. Got {data!r}, expected the string {cls._zarr_v2_name!r}"
78148
raise DataTypeValidationError(msg)
79149

80150
@classmethod
81151
def _from_json_v3(cls: type[Self], data: DTypeJSON) -> Self:
152+
"""
153+
Create an instance of Bool from Zarr V3-flavored JSON.
154+
155+
Parameters
156+
----------
157+
data : DTypeJSON
158+
The JSON data.
159+
160+
Returns
161+
-------
162+
Bool
163+
An instance of Bool.
164+
165+
Raises
166+
------
167+
DataTypeValidationError
168+
If the input JSON is not a valid representation of a Bool.
169+
"""
82170
if cls._check_json_v3(data):
83171
return cls()
84172
msg = f"Invalid JSON representation of {cls.__name__}. Got {data!r}, expected the string {cls._zarr_v3_name!r}"
@@ -93,17 +181,65 @@ def to_json(self, zarr_format: Literal[3]) -> Literal["bool"]: ...
93181
def to_json(
94182
self, zarr_format: ZarrFormat
95183
) -> DTypeConfig_V2[Literal["|b1"], None] | Literal["bool"]:
184+
"""
185+
Serialize this Bool instance to JSON.
186+
187+
Parameters
188+
----------
189+
zarr_format : ZarrFormat
190+
The Zarr format version (2 or 3).
191+
192+
Returns
193+
-------
194+
DTypeConfig_V2[Literal["|b1"], None] or Literal["bool"]
195+
The JSON representation of the Bool instance.
196+
197+
Raises
198+
------
199+
ValueError
200+
If the zarr_format is not 2 or 3.
201+
"""
96202
if zarr_format == 2:
97203
return {"name": self._zarr_v2_name, "object_codec_id": None}
98204
elif zarr_format == 3:
99205
return self._zarr_v3_name
100206
raise ValueError(f"zarr_format must be 2 or 3, got {zarr_format}") # pragma: no cover
101207

102208
def _check_scalar(self, data: object) -> bool:
103-
# Anything can become a bool
209+
"""
210+
Check if the input can be cast to a boolean scalar.
211+
212+
Parameters
213+
----------
214+
data : object
215+
The data to check.
216+
217+
Returns
218+
-------
219+
bool
220+
True if the input can be cast to a boolean scalar, False otherwise.
221+
"""
104222
return True
105223

106224
def cast_scalar(self, data: object) -> np.bool_:
225+
"""
226+
Cast the input to a numpy boolean scalar.
227+
228+
Parameters
229+
----------
230+
data : object
231+
The data to cast.
232+
233+
Returns
234+
-------
235+
np.bool_
236+
The numpy boolean scalar.
237+
238+
Raises
239+
------
240+
TypeError
241+
If the input cannot be converted to a numpy boolean.
242+
"""
107243
if self._check_scalar(data):
108244
return np.bool_(data)
109245
msg = f"Cannot convert object with type {type(data)} to a numpy boolean."
@@ -153,11 +289,24 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> np.bool_:
153289
-------
154290
np.bool_
155291
The numpy boolean scalar.
292+
293+
Raises
294+
------
295+
TypeError
296+
If the input is not a valid boolean type.
156297
"""
157298
if self._check_scalar(data):
158299
return np.bool_(data)
159300
raise TypeError(f"Invalid type: {data}. Expected a boolean.") # pragma: no cover
160301

161302
@property
162303
def item_size(self) -> int:
304+
"""
305+
Return the item size of the boolean dtype.
306+
307+
Returns
308+
-------
309+
int
310+
The item size in bytes.
311+
"""
163312
return 1

0 commit comments

Comments
 (0)