Skip to content

Commit

Permalink
Use typing.Self to annotate classmethods return types
Browse files Browse the repository at this point in the history
  • Loading branch information
vandalt committed Sep 16, 2024
1 parent 88fe72b commit 63a7b13
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
14 changes: 7 additions & 7 deletions astroplan/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions astroplan/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions astroplan/scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions astroplan/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 63a7b13

Please sign in to comment.