From 63a7b13e783a8a3715ddfa336c7665f673d85e77 Mon Sep 17 00:00:00 2001 From: Thomas Vandal Date: Sun, 15 Sep 2024 22:29:54 -0300 Subject: [PATCH] Use `typing.Self` to annotate classmethods return types https://docs.python.org/3/library/typing.html#typing.Self --- astroplan/constraints.py | 14 +++++++------- astroplan/observer.py | 4 ++-- astroplan/scheduling.py | 8 ++++---- astroplan/target.py | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 6ab07f5d..1339d967 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -9,7 +9,7 @@ # Standard library from abc import ABCMeta, abstractmethod -from typing import Optional, Sequence, Union +from typing import Optional, Self, Sequence, Union import datetime import time import warnings @@ -444,21 +444,21 @@ def __init__(self, max_solar_altitude: Quantity = 0*u.deg, force_pressure_zero: self.force_pressure_zero = force_pressure_zero @classmethod - def twilight_civil(cls, **kwargs) -> "AtNightConstraint": + def twilight_civil(cls, **kwargs) -> Self: """ Consider nighttime as time between civil twilights (-6 degrees). """ return cls(max_solar_altitude=-6*u.deg, **kwargs) @classmethod - def twilight_nautical(cls, **kwargs) -> "AtNightConstraint": + def twilight_nautical(cls, **kwargs) -> Self: """ Consider nighttime as time between nautical twilights (-12 degrees). """ return cls(max_solar_altitude=-12*u.deg, **kwargs) @classmethod - def twilight_astronomical(cls, **kwargs) -> "AtNightConstraint": + def twilight_astronomical(cls, **kwargs) -> Self: """ Consider nighttime as time between astronomical twilights (-18 degrees). """ @@ -645,7 +645,7 @@ def __init__(self, min: Optional[float] = None, max: Optional[float] = None, eph self.ephemeris = ephemeris @classmethod - def dark(cls, min: Optional[float] = None, max: Optional[float] = 0.25, **kwargs) -> "MoonIlluminationConstraint": + def dark(cls, min: Optional[float] = None, max: Optional[float] = 0.25, **kwargs) -> Self: """ initialize a `~astroplan.constraints.MoonIlluminationConstraint` with defaults of no minimum and a maximum of 0.25 @@ -662,7 +662,7 @@ def dark(cls, min: Optional[float] = None, max: Optional[float] = 0.25, **kwargs return cls(min, max, **kwargs) @classmethod - def grey(cls, min: Optional[float] = 0.25, max: Optional[float] = 0.65, **kwargs) -> "MoonIlluminationConstraint": + def grey(cls, min: Optional[float] = 0.25, max: Optional[float] = 0.65, **kwargs) -> Self: """ initialize a `~astroplan.constraints.MoonIlluminationConstraint` with defaults of a minimum of 0.25 and a maximum of 0.65 @@ -679,7 +679,7 @@ def grey(cls, min: Optional[float] = 0.25, max: Optional[float] = 0.65, **kwargs return cls(min, max, **kwargs) @classmethod - def bright(cls, min: Optional[float] = 0.65, max: Optional[float] = None, **kwargs) -> "MoonIlluminationConstraint": + def bright(cls, min: Optional[float] = 0.65, max: Optional[float] = None, **kwargs) -> Self: """ initialize a `~astroplan.constraints.MoonIlluminationConstraint` with defaults of a minimum of 0.65 and no maximum diff --git a/astroplan/observer.py b/astroplan/observer.py index 0e2ee3ff..77a2474c 100644 --- a/astroplan/observer.py +++ b/astroplan/observer.py @@ -5,7 +5,7 @@ # Standard library import sys -from typing import Any, Callable, Optional, Sequence, Union +from typing import Any, Callable, Optional, Self, Sequence, Union import datetime import warnings @@ -359,7 +359,7 @@ def __ne__(self, other: "Observer") -> bool: return not self.__eq__(other) @classmethod - def at_site(cls, site_name: str, **kwargs) -> "Observer": + def at_site(cls, site_name: str, **kwargs) -> Self: """ Initialize an `~astroplan.observer.Observer` object with a site name. diff --git a/astroplan/scheduling.py b/astroplan/scheduling.py index ed2ab60c..efd76c1a 100644 --- a/astroplan/scheduling.py +++ b/astroplan/scheduling.py @@ -7,7 +7,7 @@ import copy from abc import ABCMeta, abstractmethod -from typing import Optional, Sequence, Union +from typing import Optional, Self, Sequence, Union import numpy as np from astropy import units as u @@ -93,7 +93,7 @@ def constraints_scores(self) -> Optional[dict[Constraint, np.ndarray]]: def from_exposures(cls, target: FixedTarget, priority: Union[int, float], time_per_exposure: Quantity, number_exposures: int, readout_time: Quantity = 0 * u.second, - configuration: dict = {}, constraints: Optional[list[Constraint]] = None) -> "ObservingBlock": + configuration: dict = {}, constraints: Optional[list[Constraint]] = None) -> Self: duration = number_exposures * (time_per_exposure + readout_time) ob = cls(target, duration, priority, configuration, constraints) ob.time_per_exposure = time_per_exposure @@ -222,7 +222,7 @@ def components(self, val: dict[str, Quantity]) -> None: @classmethod @u.quantity_input(duration=u.second) - def from_duration(cls, duration: Quantity) -> "TransitionBlock": + def from_duration(cls, duration: Quantity) -> Self: # for testing how to put transitions between observations during # scheduling without considering the complexities of duration tb = TransitionBlock({'duration': duration}) @@ -584,7 +584,7 @@ def _make_schedule(self, blocks: list[ObservingBlock]): @classmethod @u.quantity_input(duration=u.second) - def from_timespan(cls, center_time: Time, duration: Union[Quantity, TimeDelta], **kwargs) -> "Scheduler": + def from_timespan(cls, center_time: Time, duration: Union[Quantity, TimeDelta], **kwargs) -> Self: """ Create a new instance of this class given a center time and duration. diff --git a/astroplan/target.py b/astroplan/target.py index 1719780d..3144b347 100644 --- a/astroplan/target.py +++ b/astroplan/target.py @@ -4,7 +4,7 @@ # Standard library from abc import ABCMeta -from typing import Optional, Union +from typing import Optional, Self, Union # Third-party import astropy.units as u @@ -110,7 +110,7 @@ def __init__(self, coord: SkyCoord, name: Optional[str] = None, **kwargs): self.coord = coord @classmethod - def from_name(cls, query_name: str, name: Optional[str] = None, **kwargs) -> "FixedTarget": + def from_name(cls, query_name: str, name: Optional[str] = None, **kwargs) -> Self: """ Initialize a `FixedTarget` by querying for a name from the CDS name resolver, using the machinery in @@ -161,7 +161,7 @@ def __repr__(self) -> str: return '<{} "{}" at {}>'.format(class_name, self.name, fmt_coord) @classmethod - def _from_name_mock(cls, query_name: str, name: Optional[str] = None) -> "FixedTarget": + def _from_name_mock(cls, query_name: str, name: Optional[str] = None) -> Self: """ Mock method to replace `FixedTarget.from_name` in tests without internet connection.