Skip to content

Commit 90c1262

Browse files
authored
clib.Session: Add type hints and reformat docstrings (part 1) (#3504)
1 parent 3e3b575 commit 90c1262

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

pygmt/clib/session.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,28 @@ class Session:
105105
"""
106106
A GMT API session where most operations involving the C API happen.
107107
108-
Works as a context manager (for use in a ``with`` block) to create a GMT C
109-
API session and destroy it in the end to clean up memory.
108+
Works as a context manager (for use in a ``with`` block) to create a GMT C API
109+
session and destroy it in the end to clean up memory.
110110
111-
Functions of the shared library are exposed as methods of this class. Most
112-
methods MUST be used with an open session (inside a ``with`` block). If
113-
creating GMT data structures to communicate data, put that code inside the
114-
same ``with`` block as the API calls that will use the data.
111+
Functions of the shared library are exposed as methods of this class. Most methods
112+
MUST be used with an open session (inside a ``with`` block). If creating GMT data
113+
structures to communicate data, put that code inside the same ``with`` block as the
114+
API calls that will use the data.
115115
116-
By default, will let :mod:`ctypes` try to find the GMT shared library
117-
(``libgmt``). If the environment variable :term:`GMT_LIBRARY_PATH` is set, will
118-
look for the shared library in the directory specified by it.
116+
By default, will let :mod:`ctypes` try to find the GMT shared library (``libgmt``).
117+
If the environment variable :term:`GMT_LIBRARY_PATH` is set, will look for the
118+
shared library in the directory specified by it.
119119
120-
A ``GMTVersionError`` exception will be raised if the GMT shared library
121-
reports a version older than the required minimum GMT version.
122-
123-
The ``session_pointer`` attribute holds a ctypes pointer to the currently
124-
open session.
120+
The ``session_pointer`` attribute holds a ctypes pointer to the currently open
121+
session.
125122
126123
Raises
127124
------
128125
GMTCLibNotFoundError
129-
If there was any problem loading the library (couldn't find it or
130-
couldn't access the functions).
126+
If there was any problem loading the library (couldn't find it or couldn't
127+
access the functions).
131128
GMTCLibNoSessionError
132-
If you try to call a method outside of a 'with' block.
133-
GMTVersionError
134-
If the minimum required version of GMT is not found.
129+
If you try to call a method outside of a ``with`` block.
135130
136131
Examples
137132
--------
@@ -141,45 +136,44 @@ class Session:
141136
>>> grid = load_static_earth_relief()
142137
>>> type(grid)
143138
<class 'xarray.core.dataarray.DataArray'>
144-
>>> # Create a session and destroy it automatically when exiting the "with"
145-
>>> # block.
146-
>>> with Session() as ses:
139+
>>> # Create a session and destroy it automatically when exiting the "with" block.
140+
>>> with Session() as lib:
147141
... # Create a virtual file and link to the memory block of the grid.
148-
... with ses.virtualfile_from_grid(grid) as fin:
142+
... with lib.virtualfile_from_grid(grid) as fin:
149143
... # Create a temp file to use as output.
150144
... with GMTTempFile() as fout:
151-
... # Call the grdinfo module with the virtual file as input
152-
... # and the temp file as output.
153-
... ses.call_module("grdinfo", [fin, "-C", f"->{fout.name}"])
145+
... # Call the grdinfo module with the virtual file as input and the
146+
... # temp file as output.
147+
... lib.call_module("grdinfo", [fin, "-C", f"->{fout.name}"])
154148
... # Read the contents of the temp file before it's deleted.
155149
... print(fout.read().strip())
156150
-55 -47 -24 -10 190 981 1 1 8 14 1 1
157151
"""
158152

159153
@property
160-
def session_pointer(self):
154+
def session_pointer(self) -> ctp.c_void_p:
161155
"""
162156
The :class:`ctypes.c_void_p` pointer to the current open GMT session.
163157
164158
Raises
165159
------
166160
GMTCLibNoSessionError
167-
If trying to access without a currently open GMT session (i.e.,
168-
outside of the context manager).
161+
If trying to access without a currently open GMT session (i.e., outside of
162+
the context manager).
169163
"""
170164
if not hasattr(self, "_session_pointer") or self._session_pointer is None:
171165
raise GMTCLibNoSessionError("No currently open GMT API session.")
172166
return self._session_pointer
173167

174168
@session_pointer.setter
175-
def session_pointer(self, session):
169+
def session_pointer(self, session: ctp.c_void_p):
176170
"""
177171
Set the session void pointer.
178172
"""
179173
self._session_pointer = session
180174

181175
@property
182-
def info(self):
176+
def info(self) -> dict[str, str]:
183177
"""
184178
Dictionary with the GMT version and default paths and parameters.
185179
"""

0 commit comments

Comments
 (0)