Skip to content

Commit e0ddfc8

Browse files
authored
Add type hints to methods of interface, including arguments (#168)
* Add type hints to methods, including arguments * PEP8 changes * Update minimum python version for the API package * Remove Python 3.9 from workflows and RTD * Update RTD configuration file
1 parent 9d4c8e4 commit e0ddfc8

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

astrowidgets/interface_definition.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from typing import Protocol, runtime_checkable, Any
22
from abc import abstractmethod
3+
import os
4+
5+
from astropy.coordinates import SkyCoord
6+
from astropy.nddata import NDData
7+
from astropy.table import Table
8+
from astropy.units import Quantity
9+
10+
from numpy.typing import ArrayLike
311

412
# Allowed locations for cursor display
513
ALLOWED_CURSOR_LOCATIONS = ('top', 'bottom', None)
@@ -44,7 +52,7 @@ class ImageViewerInterface(Protocol):
4452

4553
# Methods for loading data
4654
@abstractmethod
47-
def load_fits(self, file):
55+
def load_fits(self, file: str | os.PathLike) -> None:
4856
"""
4957
Load a FITS file into the viewer.
5058
@@ -57,7 +65,7 @@ def load_fits(self, file):
5765
raise NotImplementedError
5866

5967
@abstractmethod
60-
def load_array(self, array):
68+
def load_array(self, array: ArrayLike) -> None:
6169
"""
6270
Load a 2D array into the viewer.
6371
@@ -69,7 +77,7 @@ def load_array(self, array):
6977
raise NotImplementedError
7078

7179
@abstractmethod
72-
def load_nddata(self, data):
80+
def load_nddata(self, data: NDData) -> None:
7381
"""
7482
Load an `astropy.nddata.NDData` object into the viewer.
7583
@@ -82,7 +90,7 @@ def load_nddata(self, data):
8290

8391
# Saving contents of the view and accessing the view
8492
@abstractmethod
85-
def save(self, filename):
93+
def save(self, filename: str | os.PathLike) -> None:
8694
"""
8795
Save the current view to a file.
8896
@@ -96,7 +104,7 @@ def save(self, filename):
96104

97105
# Marker-related methods
98106
@abstractmethod
99-
def start_marking(self, marker_name=None):
107+
def start_marking(self, marker_name: str | None = None) -> None:
100108
"""
101109
Start interactive marking of points on the image.
102110
@@ -109,7 +117,7 @@ def start_marking(self, marker_name=None):
109117
raise NotImplementedError
110118

111119
@abstractmethod
112-
def stop_marking(self, clear_markers=False):
120+
def stop_marking(self, clear_markers: bool = False) -> None:
113121
"""
114122
Stop interactive marking of points on the image.
115123
@@ -122,9 +130,9 @@ def stop_marking(self, clear_markers=False):
122130
raise NotImplementedError
123131

124132
@abstractmethod
125-
def add_markers(self, table, x_colname='x', y_colname='y',
126-
skycoord_colname='coord', use_skycoord=False,
127-
marker_name=None):
133+
def add_markers(self, table: Table, x_colname: str = 'x', y_colname: str = 'y',
134+
skycoord_colname: str = 'coord', use_skycoord: bool = False,
135+
marker_name: str | None = None) -> None:
128136
"""
129137
Add markers to the image.
130138
@@ -156,7 +164,7 @@ def add_markers(self, table, x_colname='x', y_colname='y',
156164
# raise NotImplementedError
157165

158166
@abstractmethod
159-
def reset_markers(self):
167+
def reset_markers(self) -> None:
160168
"""
161169
Remove all markers from the image.
162170
"""
@@ -167,7 +175,7 @@ def reset_markers(self):
167175
# raise NotImplementedError
168176

169177
@abstractmethod
170-
def remove_markers(self, marker_name=None):
178+
def remove_markers(self, marker_name: str | None = None) -> None:
171179
"""
172180
Remove markers from the image.
173181
@@ -184,9 +192,9 @@ def remove_markers(self, marker_name=None):
184192
# raise NotImplementedError
185193

186194
@abstractmethod
187-
def get_markers(self, x_colname='x', y_colname='y',
188-
skycoord_colname='coord',
189-
marker_name=None):
195+
def get_markers(self, x_colname: str = 'x', y_colname: str = 'y',
196+
skycoord_colname: str = 'coord',
197+
marker_name: str | None = None) -> Table:
190198
"""
191199
Get the marker positions.
192200
@@ -214,7 +222,7 @@ def get_markers(self, x_colname='x', y_colname='y',
214222

215223
# Methods that modify the view
216224
@abstractmethod
217-
def center_on(self, point):
225+
def center_on(self, point: tuple | SkyCoord):
218226
"""
219227
Center the view on the point.
220228
@@ -227,22 +235,22 @@ def center_on(self, point):
227235
raise NotImplementedError
228236

229237
@abstractmethod
230-
def offset_by(self, dx, dy):
238+
def offset_by(self, dx: float | Quantity, dy: float | Quantity) -> None:
231239
"""
232240
Move the center to a point that is given offset
233241
away from the current center.
234242
235243
Parameters
236244
----------
237-
dx, dy : float or `~astropy.unit.Quantity`
245+
dx, dy : float or `~astropy.units.Quantity`
238246
Offset value. Without a unit, assumed to be pixel offsets.
239247
If a unit is attached, offset by pixel or sky is assumed from
240248
the unit.
241249
"""
242250
raise NotImplementedError
243251

244252
@abstractmethod
245-
def zoom(self):
253+
def zoom(self) -> None:
246254
"""
247255
Zoom in or out by the given factor.
248256

0 commit comments

Comments
 (0)