2222
2323Serialization/deserialization
2424-----------------------------
25- .. autofunction:: is_array_container
25+ .. autofunction:: is_array_container_type
2626.. autofunction:: serialize_container
2727.. autofunction:: deserialize_container
2828
@@ -95,8 +95,6 @@ class ArrayContainer:
9595
9696 This allows enumeration of the component arrays in a container and the
9797 construction of modified containers from an iterable of those component arrays.
98- :func:`is_array_container` will return *True* for types that have
99- a container serialization function registered.
10098
10199 Packages may register their own types as array containers. They must not
102100 register other types (e.g. :class:`list`) as array containers.
@@ -160,6 +158,13 @@ def is_array_container_type(cls: type) -> bool:
160158 """
161159 :returns: *True* if the type *cls* has a registered implementation of
162160 :func:`serialize_container`, or if it is an :class:`ArrayContainer`.
161+
162+ .. warning::
163+
164+ Not all instances of a type that this function labels an array container
165+ must automatically be array containers. For example, while this
166+ function will say that :class:`numpy.ndarray` is an array container
167+ type, only object arrays *actually are* array containers.
163168 """
164169 return (
165170 cls is ArrayContainer
@@ -172,6 +177,13 @@ def is_array_container(ary: Any) -> bool:
172177 :returns: *True* if the instance *ary* has a registered implementation of
173178 :func:`serialize_container`.
174179 """
180+
181+ from warnings import warn
182+ warn ("is_array_container is deprecated and will be removed in 2022. "
183+ "If you must know precisely whether something is an array container, "
184+ "try serializing it and catch TypeError. For a cheaper option, see "
185+ "is_array_container_type." ,
186+ DeprecationWarning , stacklevel = 2 )
175187 return (serialize_container .dispatch (ary .__class__ )
176188 is not serialize_container .__wrapped__ ) # type:ignore[attr-defined]
177189
@@ -194,7 +206,7 @@ def get_container_context(ary: ArrayContainer) -> Optional[ArrayContext]:
194206@serialize_container .register (np .ndarray )
195207def _serialize_ndarray_container (ary : np .ndarray ) -> Iterable [Tuple [Any , Any ]]:
196208 if ary .dtype .char != "O" :
197- raise ValueError (
209+ raise TypeError (
198210 f"cannot seriealize '{ type (ary ).__name__ } ' with dtype '{ ary .dtype } '" )
199211
200212 # special-cased for speed
@@ -236,7 +248,7 @@ def get_container_context_recursively(ary: Any) -> Optional[ArrayContext]:
236248 any level, an assertion error is raised.
237249 """
238250 actx = None
239- if not is_array_container (ary ):
251+ if not is_array_container_type (ary . __class__ ):
240252 return actx
241253
242254 # try getting the array context directly
0 commit comments