Skip to content

Commit c15d79f

Browse files
authored
Add type hints to the unique_name function and the GMTTempFile class (#3440)
1 parent be8c450 commit c15d79f

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

pygmt/helpers/tempfile.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
from packaging.version import Version
1313

1414

15-
def unique_name():
15+
def unique_name() -> str:
1616
"""
1717
Generate a unique name.
1818
19-
Useful for generating unique names for figures (otherwise GMT will plot
20-
everything on the same figure instead of creating a new one).
19+
Useful for generating unique names for figures. Otherwise GMT will plot everything
20+
on the same figure instead of creating a new one.
2121
2222
Returns
2323
-------
24-
name : str
25-
A unique name generated by :func:`uuid.uuid4`
24+
name
25+
A unique name generated by :func:`uuid.uuid4`.
2626
"""
2727
return uuid.uuid4().hex
2828

@@ -31,15 +31,14 @@ class GMTTempFile:
3131
"""
3232
Context manager for creating closed temporary files.
3333
34-
This class does not return a file-like object. So, you can't do
35-
``for line in GMTTempFile()``, for example, or pass it to things that
36-
need file objects.
34+
This class does not return a file-like object. So, you can't iterate over the object
35+
like ``for line in GMTTempFile()``, or pass it to things that need a file object.
3736
3837
Parameters
3938
----------
40-
prefix : str
39+
prefix
4140
The temporary file name begins with the prefix.
42-
suffix : str
41+
suffix
4342
The temporary file name ends with the suffix.
4443
4544
Examples
@@ -60,7 +59,10 @@ class GMTTempFile:
6059
[0. 0. 0.] [1. 1. 1.] [2. 2. 2.]
6160
"""
6261

63-
def __init__(self, prefix="pygmt-", suffix=".txt"):
62+
def __init__(self, prefix: str = "pygmt-", suffix: str = ".txt"):
63+
"""
64+
Initialize the object.
65+
"""
6466
with NamedTemporaryFile(prefix=prefix, suffix=suffix, delete=False) as tmpfile:
6567
self.name = tmpfile.name
6668

@@ -76,33 +78,33 @@ def __exit__(self, *args):
7678
"""
7779
Path(self.name).unlink(missing_ok=True)
7880

79-
def read(self, keep_tabs=False):
81+
def read(self, keep_tabs: bool = False) -> str:
8082
"""
8183
Read the entire contents of the file as a Unicode string.
8284
8385
Parameters
8486
----------
85-
keep_tabs : bool
87+
keep_tabs
8688
If False, replace the tabs that GMT uses with spaces.
8789
8890
Returns
8991
-------
90-
content : str
92+
content
9193
Content of the temporary file as a Unicode string.
9294
"""
9395
content = Path(self.name).read_text(encoding="utf8")
9496
if not keep_tabs:
9597
content = content.replace("\t", " ")
9698
return content
9799

98-
def loadtxt(self, **kwargs):
100+
def loadtxt(self, **kwargs) -> np.ndarray:
99101
"""
100102
Load data from the temporary file using numpy.loadtxt.
101103
102104
Parameters
103105
----------
104-
kwargs : dict
105-
Any keyword arguments that can be passed to numpy.loadtxt.
106+
kwargs
107+
Any keyword arguments that can be passed to :func:`np.loadtxt`.
106108
107109
Returns
108110
-------

0 commit comments

Comments
 (0)