@@ -187,23 +187,26 @@ def _check_encoding(
187
187
return "ISOLatin1+"
188
188
189
189
190
- def data_kind (
190
+ def data_kind ( # noqa: PLR0911
191
191
data : Any = None , 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 :
198
+ The ``data`` argument can be in any type. Following data kinds are recognized :
199
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)
200
+ - ``"arg"``: data is ``None`` and ``required=False``, or bool, int, float,
201
+ representing an optional argument, used for dealing with optional virtual files
202
+ - ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
203
+ representing one or more file names
204
+ - ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
205
+ (e.g., geopandas.GeoDataFrame or shapely.geometry)
206
+ - ``"grid"``: a :class:`xarray.DataArray` object that is not 3-D
207
+ - ``"image"``: a 3-D :class:`xarray.DataArray` object
208
+ - ``"matrix"``: anything that is not None
209
+ - ``"vectors"``: data is ``None`` and ``required=True``
207
210
208
211
Parameters
209
212
----------
@@ -287,30 +290,36 @@ def data_kind(
287
290
>>> data_kind(data=None)
288
291
'vectors'
289
292
"""
290
- kind : Literal [
291
- "arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "stringio" , "vectors"
292
- ]
293
+ # One file or a list/tuple of files.
293
294
if isinstance (data , str | pathlib .PurePath ) or (
294
295
isinstance (data , list | tuple )
295
296
and all (isinstance (_file , str | pathlib .PurePath ) for _file in data )
296
297
):
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
298
+ return "file"
299
+
300
+ # A StringIO object.
301
+ if isinstance (data , io .StringIO ):
302
+ return "stringio"
303
+
304
+ # An option argument, mainly for dealing optional virtual files.
305
+ if isinstance (data , bool | int | float ) or (data is None and not required ):
306
+ return "arg"
307
+
308
+ # An xarray.DataArray object, representing a grid or an image.
309
+ if isinstance (data , xr .DataArray ):
310
+ return "image" if len (data .dims ) == 3 else "grid"
311
+
312
+ # Geo-like Python object that implements ``__geo_interface__`` (e.g.,
313
+ # geopandas.GeoDataFrame or shapely.geometry).
314
+ # Reference: https://gist.github.com/sgillies/2217756
315
+ if hasattr (data , "__geo_interface__" ):
316
+ return "geojson"
317
+
318
+ # Any not-None is considered as a matrix.
319
+ if data is not None :
320
+ return "matrix"
321
+
322
+ return "vectors"
314
323
315
324
316
325
def non_ascii_to_octal (
0 commit comments