@@ -188,22 +188,27 @@ def _check_encoding(
188
188
189
189
190
190
def data_kind (
191
- data : Any = None , required : bool = True
191
+ data : Any , required : bool = True
192
192
) -> Literal [
193
193
"arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "stringio" , "vectors"
194
194
]:
195
195
r"""
196
196
Check the kind of data that is provided to a module.
197
197
198
- The ``data`` argument can be in any type, but only following types are supported:
199
-
200
- - a string or a :class:`pathlib.PurePath` object or a sequence of them, representing
201
- a file name or a list of file names
202
- - a 2-D or 3-D :class:`xarray.DataArray` object
203
- - a 2-D matrix
204
- - None, bool, int or float type representing an optional arguments
205
- - a geo-like Python object that implements ``__geo_interface__`` (e.g.,
206
- geopandas.GeoDataFrame or shapely.geometry)
198
+ The argument passed to the ``data`` parameter can have any data type. The
199
+ following data kinds are recognized and returned as ``kind``:
200
+
201
+ - ``"arg"``: ``data`` is ``None`` and ``required=False``, or bool, int, float,
202
+ representing an optional argument, used for dealing with optional virtual files
203
+ - ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
204
+ representing one or more file names
205
+ - ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
206
+ (e.g., geopandas.GeoDataFrame or shapely.geometry)
207
+ - ``"grid"``: a :class:`xarray.DataArray` object that is not 3-D
208
+ - ``"image"``: a 3-D :class:`xarray.DataArray` object
209
+ - ``"stringio"``: a :class:`io.StringIO` object
210
+ - ``"matrix"``: anything else that is not ``None``
211
+ - ``"vectors"``: ``data`` is ``None`` and ``required=True``
207
212
208
213
Parameters
209
214
----------
@@ -287,30 +292,31 @@ def data_kind(
287
292
>>> data_kind(data=None)
288
293
'vectors'
289
294
"""
290
- kind : Literal [
291
- "arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "stringio" , "vectors"
292
- ]
293
- if isinstance (data , str | pathlib .PurePath ) or (
294
- isinstance (data , list | tuple )
295
- and all (isinstance (_file , str | pathlib .PurePath ) for _file in data )
296
- ):
297
- # One or more files
298
- kind = "file"
299
- elif isinstance (data , bool | int | float ) or (data is None and not required ):
300
- kind = "arg"
301
- elif isinstance (data , io .StringIO ):
302
- kind = "stringio"
303
- elif isinstance (data , xr .DataArray ):
304
- kind = "image" if len (data .dims ) == 3 else "grid"
305
- elif hasattr (data , "__geo_interface__" ):
306
- # geo-like Python object that implements ``__geo_interface__``
307
- # (geopandas.GeoDataFrame or shapely.geometry)
308
- kind = "geojson"
309
- elif data is not None :
310
- kind = "matrix"
311
- else :
312
- kind = "vectors"
313
- return kind
295
+ match data :
296
+ case str () | pathlib .PurePath (): # One file.
297
+ kind = "file"
298
+ case list () | tuple () if all (
299
+ isinstance (_file , str | pathlib .PurePath ) for _file in data
300
+ ): # A list/tuple of files.
301
+ kind = "file"
302
+ case io .StringIO ():
303
+ kind = "stringio"
304
+ case (bool () | int () | float ()) | None if not required :
305
+ # An option argument, mainly for dealing with optional virtual files.
306
+ kind = "arg"
307
+ case xr .DataArray ():
308
+ # An xarray.DataArray object, representing either a grid or an image.
309
+ kind = "image" if len (data .dims ) == 3 else "grid"
310
+ case x if hasattr (x , "__geo_interface__" ):
311
+ # Geo-like Python object that implements ``__geo_interface__`` (e.g.,
312
+ # geopandas.GeoDataFrame or shapely.geometry).
313
+ # Reference: https://gist.github.com/sgillies/2217756
314
+ kind = "geojson"
315
+ case x if x is not None : # Any not-None is considered as a matrix.
316
+ kind = "matrix"
317
+ case _: # Fall back to "vectors" if data is None and required=True.
318
+ kind = "vectors"
319
+ return kind # type: ignore[return-value]
314
320
315
321
316
322
def non_ascii_to_octal (
0 commit comments