Skip to content

Commit

Permalink
Add type hints to methods of interface, including arguments (#168)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mwcraig authored Jul 5, 2023
1 parent 210d772 commit ebf8da2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci_workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: '3.10'
- name: Install and build
run: python -m pip install tox --upgrade
- name: Run tests
run: tox -e py39-test
run: tox -e py310-test

devtests:
runs-on: ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ sphinx:
fail_on_warning: true

# Set the version of Python and requirements required to build your docs
build:
os: ubuntu-22.04
tools:
python: "3.10"

python:
version: 3.8
system_packages: false
install:
- method: pip
Expand Down
44 changes: 26 additions & 18 deletions astrowidgets/interface_definition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from typing import Protocol, runtime_checkable, Any
from abc import abstractmethod
import os

from astropy.coordinates import SkyCoord
from astropy.nddata import NDData
from astropy.table import Table
from astropy.units import Quantity

from numpy.typing import ArrayLike

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

# Methods for loading data
@abstractmethod
def load_fits(self, file):
def load_fits(self, file: str | os.PathLike) -> None:
"""
Load a FITS file into the viewer.
Expand All @@ -57,7 +65,7 @@ def load_fits(self, file):
raise NotImplementedError

@abstractmethod
def load_array(self, array):
def load_array(self, array: ArrayLike) -> None:
"""
Load a 2D array into the viewer.
Expand All @@ -69,7 +77,7 @@ def load_array(self, array):
raise NotImplementedError

@abstractmethod
def load_nddata(self, data):
def load_nddata(self, data: NDData) -> None:
"""
Load an `astropy.nddata.NDData` object into the viewer.
Expand All @@ -82,7 +90,7 @@ def load_nddata(self, data):

# Saving contents of the view and accessing the view
@abstractmethod
def save(self, filename):
def save(self, filename: str | os.PathLike) -> None:
"""
Save the current view to a file.
Expand All @@ -96,7 +104,7 @@ def save(self, filename):

# Marker-related methods
@abstractmethod
def start_marking(self, marker_name=None):
def start_marking(self, marker_name: str | None = None) -> None:
"""
Start interactive marking of points on the image.
Expand All @@ -109,7 +117,7 @@ def start_marking(self, marker_name=None):
raise NotImplementedError

@abstractmethod
def stop_marking(self, clear_markers=False):
def stop_marking(self, clear_markers: bool = False) -> None:
"""
Stop interactive marking of points on the image.
Expand All @@ -122,9 +130,9 @@ def stop_marking(self, clear_markers=False):
raise NotImplementedError

@abstractmethod
def add_markers(self, table, x_colname='x', y_colname='y',
skycoord_colname='coord', use_skycoord=False,
marker_name=None):
def add_markers(self, table: Table, x_colname: str = 'x', y_colname: str = 'y',
skycoord_colname: str = 'coord', use_skycoord: bool = False,
marker_name: str | None = None) -> None:
"""
Add markers to the image.
Expand Down Expand Up @@ -156,7 +164,7 @@ def add_markers(self, table, x_colname='x', y_colname='y',
# raise NotImplementedError

@abstractmethod
def reset_markers(self):
def reset_markers(self) -> None:
"""
Remove all markers from the image.
"""
Expand All @@ -167,7 +175,7 @@ def reset_markers(self):
# raise NotImplementedError

@abstractmethod
def remove_markers(self, marker_name=None):
def remove_markers(self, marker_name: str | None = None) -> None:
"""
Remove markers from the image.
Expand All @@ -184,9 +192,9 @@ def remove_markers(self, marker_name=None):
# raise NotImplementedError

@abstractmethod
def get_markers(self, x_colname='x', y_colname='y',
skycoord_colname='coord',
marker_name=None):
def get_markers(self, x_colname: str = 'x', y_colname: str = 'y',
skycoord_colname: str = 'coord',
marker_name: str | None = None) -> Table:
"""
Get the marker positions.
Expand Down Expand Up @@ -214,7 +222,7 @@ def get_markers(self, x_colname='x', y_colname='y',

# Methods that modify the view
@abstractmethod
def center_on(self, point):
def center_on(self, point: tuple | SkyCoord):
"""
Center the view on the point.
Expand All @@ -227,22 +235,22 @@ def center_on(self, point):
raise NotImplementedError

@abstractmethod
def offset_by(self, dx, dy):
def offset_by(self, dx: float | Quantity, dy: float | Quantity) -> None:
"""
Move the center to a point that is given offset
away from the current center.
Parameters
----------
dx, dy : float or `~astropy.unit.Quantity`
dx, dy : float or `~astropy.units.Quantity`
Offset value. Without a unit, assumed to be pixel offsets.
If a unit is attached, offset by pixel or sky is assumed from
the unit.
"""
raise NotImplementedError

@abstractmethod
def zoom(self):
def zoom(self) -> None:
"""
Zoom in or out by the given factor.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ install_requires =
ipyevents>=0.6.3
jupyterlab>=3
aggdraw
python_requires >=3.8
python_requires >=3.10

[options.extras_require]
test =
Expand Down

0 comments on commit ebf8da2

Please sign in to comment.