|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import warnings
|
| 6 | +from typing import Literal |
6 | 7 |
|
7 | 8 | from pygmt.exceptions import GMTInvalidInput
|
8 | 9 |
|
9 | 10 |
|
10 |
| -def validate_output_table_type(output_type, outfile=None): |
| 11 | +def validate_output_table_type( |
| 12 | + output_type: Literal["pandas", "numpy", "file"], outfile: str | None = None |
| 13 | +) -> Literal["pandas", "numpy", "file"]: |
11 | 14 | """
|
12 | 15 | Check if the ``output_type`` and ``outfile`` parameters are valid.
|
13 | 16 |
|
14 | 17 | Parameters
|
15 | 18 | ----------
|
16 |
| - output_type : str |
17 |
| - The type for a table output. Valid values are "file", "numpy", and |
18 |
| - "pandas". |
19 |
| - outfile : str |
20 |
| - The file name for the output table file. Required if |
21 |
| - ``output_type="file"``. |
| 19 | + output_type |
| 20 | + Desired output type of tabular data. Valid values are ``"pandas"``, |
| 21 | + ``"numpy"`` and ``"file"``. |
| 22 | + outfile |
| 23 | + File name for saving the result data. Required if ``output_type`` is ``"file"``. |
| 24 | + If specified, ``output_type`` will be forced to be ``"file"``. |
22 | 25 |
|
23 | 26 | Returns
|
24 | 27 | -------
|
25 | 28 | str
|
26 |
| - The original or corrected output type. |
| 29 | + The original or updated output type. |
| 30 | +
|
| 31 | + Examples |
| 32 | + -------- |
| 33 | + >>> validate_output_table_type(output_type="pandas") |
| 34 | + 'pandas' |
| 35 | + >>> validate_output_table_type(output_type="numpy") |
| 36 | + 'numpy' |
| 37 | + >>> validate_output_table_type(output_type="file", outfile="output-fname.txt") |
| 38 | + 'file' |
| 39 | + >>> validate_output_table_type(output_type="invalid-type") |
| 40 | + Traceback (most recent call last): |
| 41 | + ... |
| 42 | + pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' either as 'file', ... |
| 43 | + >>> validate_output_table_type("file", outfile=None) |
| 44 | + Traceback (most recent call last): |
| 45 | + ... |
| 46 | + pygmt.exceptions.GMTInvalidInput: Must specify 'outfile' for output_type='file'. |
| 47 | + >>> with warnings.catch_warnings(record=True) as w: |
| 48 | + ... validate_output_table_type("pandas", outfile="not-none.txt") |
| 49 | + ... assert len(w) == 1 |
| 50 | + 'file' |
27 | 51 | """
|
28 | 52 | if output_type not in ["file", "numpy", "pandas"]:
|
29 | 53 | raise GMTInvalidInput(
|
|
0 commit comments