Skip to content

Commit 2f598c5

Browse files
Add doctests for the validate_output_table_type function (#3098)
Co-authored-by: Michael Grund <[email protected]>
1 parent 752305c commit 2f598c5

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

pygmt/helpers/validators.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,51 @@
33
"""
44

55
import warnings
6+
from typing import Literal
67

78
from pygmt.exceptions import GMTInvalidInput
89

910

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"]:
1114
"""
1215
Check if the ``output_type`` and ``outfile`` parameters are valid.
1316
1417
Parameters
1518
----------
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"``.
2225
2326
Returns
2427
-------
2528
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'
2751
"""
2852
if output_type not in ["file", "numpy", "pandas"]:
2953
raise GMTInvalidInput(

0 commit comments

Comments
 (0)