|
41 | 41 | "ISO-8859-16", |
42 | 42 | ] |
43 | 43 |
|
| 44 | +# Type hints for the list of data kinds. |
| 45 | +Kind = Literal[ |
| 46 | + "arg", "empty", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors" |
| 47 | +] |
| 48 | + |
44 | 49 |
|
45 | 50 | def _validate_data_input( # noqa: PLR0912 |
46 | 51 | data=None, x=None, y=None, z=None, required=True, mincols=2, kind=None |
@@ -272,11 +277,11 @@ def _check_encoding(argstr: str) -> Encoding: |
272 | 277 | return "ISOLatin1+" |
273 | 278 |
|
274 | 279 |
|
275 | | -def data_kind( |
276 | | - data: Any, required: bool = True |
277 | | -) -> Literal[ |
278 | | - "arg", "empty", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors" |
279 | | -]: |
| 280 | +def data_kind( # noqa: PLR0912 |
| 281 | + data: Any, |
| 282 | + required: bool = True, |
| 283 | + check_kind: Kind | Sequence[Kind] | Literal["raster", "vector"] | None = None, |
| 284 | +) -> Kind: |
280 | 285 | r""" |
281 | 286 | Check the kind of data that is provided to a module. |
282 | 287 |
|
@@ -307,6 +312,14 @@ def data_kind( |
307 | 312 | required |
308 | 313 | Whether 'data' is required. Set to ``False`` when dealing with optional virtual |
309 | 314 | files. |
| 315 | + check_kind |
| 316 | + Used to validate the type of data that can be passed in. Valid values are: |
| 317 | +
|
| 318 | + - Any recognized data kind |
| 319 | + - A list/tuple of recognized data kinds |
| 320 | + - ``"raster"``: shorthand for a sequence of raster-like data kinds |
| 321 | + - ``"vector"``: shorthand for a sequence of vector-like data kinds |
| 322 | + - ``None``: means no validatation. |
310 | 323 |
|
311 | 324 | Returns |
312 | 325 | ------- |
@@ -414,6 +427,24 @@ def data_kind( |
414 | 427 | kind = "matrix" |
415 | 428 | case _: # Fall back to "vectors" if data is None and required=True. |
416 | 429 | kind = "vectors" |
| 430 | + |
| 431 | + # Now start to check if the data kind is valid. |
| 432 | + if check_kind is not None: |
| 433 | + valid_kinds = ("file", "arg") if required is False else ("file",) |
| 434 | + match check_kind: |
| 435 | + case "raster": |
| 436 | + valid_kinds += ("grid", "image") |
| 437 | + case "vector": |
| 438 | + valid_kinds += ("empty", "matrix", "vectors", "geojson") |
| 439 | + case str(): |
| 440 | + valid_kinds = (check_kind,) |
| 441 | + case list() | tuple(): |
| 442 | + valid_kinds = check_kind |
| 443 | + |
| 444 | + if kind not in valid_kinds: |
| 445 | + msg = f"Unrecognized data type: {type(data)}." |
| 446 | + raise GMTInvalidInput(msg) |
| 447 | + |
417 | 448 | return kind # type: ignore[return-value] |
418 | 449 |
|
419 | 450 |
|
|
0 commit comments