Skip to content

Commit

Permalink
Update typing.Self import to be backward compatible
Browse files Browse the repository at this point in the history
typing.Self was introduced in 3.11. Use a custom TypeVar for older
releases.
  • Loading branch information
vandalt committed Sep 16, 2024
1 parent 0a31cba commit 480412c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 4 additions & 2 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, Self, Sequence, Union
from typing import Optional, Sequence, TypeVar, Union, _SpecialForm
import datetime
import time
import warnings
Expand All @@ -28,11 +28,13 @@
# Package
from .moon import moon_illumination
from .periodic import EclipsingSystem, PeriodicEvent
from .utils import time_grid_from_range
from .utils import time_grid_from_range, _import_typing_self_compat
from .observer import Observer
from .target import get_skycoord, FixedTarget
from .exceptions import MissingConstraintWarning

Self: Union[TypeVar, _SpecialForm] = _import_typing_self_compat()

__all__ = ["AltitudeConstraint", "AirmassConstraint", "AtNightConstraint",
"is_observable", "is_always_observable", "time_grid_from_range",
"GalacticLatitudeConstraint", "SunSeparationConstraint",
Expand Down
5 changes: 4 additions & 1 deletion 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, Self, Sequence, Union
from typing import Any, Callable, Optional, Sequence, Union
import datetime
import warnings

Expand All @@ -23,6 +23,9 @@
from .exceptions import TargetNeverUpWarning, TargetAlwaysUpWarning
from .moon import moon_illumination, moon_phase_angle
from .target import get_skycoord, SunFlag, MoonFlag, FixedTarget
from .utils import _import_typing_self_compat

Self = _import_typing_self_compat()


__all__ = ["Observer"]
Expand Down
6 changes: 4 additions & 2 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, Self, Sequence, Union
from typing import Optional, Sequence, Union

import numpy as np
from astropy import units as u
Expand All @@ -18,7 +18,9 @@
from .constraints import AltitudeConstraint, Constraint
from .observer import Observer
from .target import FixedTarget, get_skycoord
from .utils import stride_array, time_grid_from_range
from .utils import stride_array, time_grid_from_range, _import_typing_self_compat

Self = _import_typing_self_compat()

__all__ = ['ObservingBlock', 'TransitionBlock', 'Schedule', 'Slot',
'Scheduler', 'SequentialScheduler', 'PriorityScheduler',
Expand Down
11 changes: 7 additions & 4 deletions astroplan/target.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from __future__ import absolute_import, division, print_function, unicode_literals

# Standard library
from abc import ABCMeta
from typing import Optional, Self, Union
from typing import Optional, Union

# Third-party
import astropy.units as u
from astropy.coordinates import ICRS, SkyCoord, UnitSphericalRepresentation
from astropy.units import Quantity
from astropy.coordinates import SkyCoord, ICRS, UnitSphericalRepresentation

from .utils import _import_typing_self_compat

Self = _import_typing_self_compat()

__all__ = ["Target", "FixedTarget", "NonFixedTarget"]

Expand Down
13 changes: 11 additions & 2 deletions astroplan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Standard library
import warnings
from typing import Union
from typing import Union, _SpecialForm, TypeVar
import os

# Third-party
Expand All @@ -21,7 +21,7 @@

__all__ = ["download_IERS_A",
"time_grid_from_range", "_set_mpl_style_sheet",
"stride_array"]
"stride_array", "_import_typing_self_compat"]

IERS_A_WARNING = ("For best precision (on the order of arcseconds), you must "
"download an up-to-date IERS Bulletin A table. To do so, run:"
Expand Down Expand Up @@ -259,3 +259,12 @@ def _open_shelve(shelffn: Union[str, os.PathLike], withclosing: bool = False) ->
return contextlib.closing(shelf)
else:
return shelf


def _import_typing_self_compat() -> Union[_SpecialForm, TypeVar]:
# TODO: Remove when no support for Python<3.11
try:
from typing import Self
except ImportError:
Self = TypeVar("Self")
return Self

0 comments on commit 480412c

Please sign in to comment.