From 480412cc270faefc577b7cf4f8f8584269e491c7 Mon Sep 17 00:00:00 2001 From: Thomas Vandal Date: Mon, 16 Sep 2024 03:17:58 -0300 Subject: [PATCH] Update typing.Self import to be backward compatible typing.Self was introduced in 3.11. Use a custom TypeVar for older releases. --- astroplan/constraints.py | 6 ++++-- astroplan/observer.py | 5 ++++- astroplan/scheduling.py | 6 ++++-- astroplan/target.py | 11 +++++++---- astroplan/utils.py | 13 +++++++++++-- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/astroplan/constraints.py b/astroplan/constraints.py index 252afda5..f2e69993 100644 --- a/astroplan/constraints.py +++ b/astroplan/constraints.py @@ -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 @@ -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", diff --git a/astroplan/observer.py b/astroplan/observer.py index 9af0b3d7..aeaf1724 100644 --- a/astroplan/observer.py +++ b/astroplan/observer.py @@ -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 @@ -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"] diff --git a/astroplan/scheduling.py b/astroplan/scheduling.py index b173a137..9ae1cfe0 100644 --- a/astroplan/scheduling.py +++ b/astroplan/scheduling.py @@ -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 @@ -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', diff --git a/astroplan/target.py b/astroplan/target.py index 3144b347..28ed1aeb 100644 --- a/astroplan/target.py +++ b/astroplan/target.py @@ -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"] diff --git a/astroplan/utils.py b/astroplan/utils.py index fe49f784..4b74e20b 100644 --- a/astroplan/utils.py +++ b/astroplan/utils.py @@ -4,7 +4,7 @@ # Standard library import warnings -from typing import Union +from typing import Union, _SpecialForm, TypeVar import os # Third-party @@ -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:" @@ -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