From ebf8da2088c58030d4a0487dd90a7a3d970a6dfd Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 5 Jul 2023 17:16:59 -0500 Subject: [PATCH] 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 --- .github/workflows/ci_workflows.yml | 4 +-- .readthedocs.yml | 6 +++- astrowidgets/interface_definition.py | 44 ++++++++++++++++------------ setup.cfg | 2 +- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci_workflows.yml b/.github/workflows/ci_workflows.yml index e1c23f8..8d45df3 100644 --- a/.github/workflows/ci_workflows.yml +++ b/.github/workflows/ci_workflows.yml @@ -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 diff --git a/.readthedocs.yml b/.readthedocs.yml index c269c5a..f88ffa6 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -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 diff --git a/astrowidgets/interface_definition.py b/astrowidgets/interface_definition.py index 49bff2a..95bba3a 100644 --- a/astrowidgets/interface_definition.py +++ b/astrowidgets/interface_definition.py @@ -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) @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. """ @@ -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. @@ -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. @@ -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. @@ -227,14 +235,14 @@ 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. @@ -242,7 +250,7 @@ def offset_by(self, dx, dy): raise NotImplementedError @abstractmethod - def zoom(self): + def zoom(self) -> None: """ Zoom in or out by the given factor. diff --git a/setup.cfg b/setup.cfg index 5cc2e38..37bc91a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 =