2121@dataclass (frozen = True , kw_only = True , slots = True )
2222class 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