Skip to content

Commit 7f3ed7c

Browse files
authored
Merge pull request #18358 from nstarman/cosmo/typing/future-annotations
style(cosmo): unstringify annotations
2 parents 7ee167b + 04ce0c7 commit 7f3ed7c

22 files changed

Lines changed: 117 additions & 183 deletions

‎astropy/cosmology/_src/core.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from __future__ import annotations
43

54
import inspect
65
from abc import ABCMeta, abstractmethod
76
from collections.abc import Mapping
87
from dataclasses import KW_ONLY, dataclass, replace
98
from types import MappingProxyType
10-
from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeVar
9+
from typing import Any, ClassVar, Self, TypeVar, Union
1110

1211
import numpy as np
1312

@@ -21,17 +20,14 @@
2120
ParametersAttribute,
2221
all_parameters,
2322
)
23+
from astropy.cosmology._src.typing import _CosmoT
2424
from astropy.cosmology.io import (
2525
CosmologyFromFormat,
2626
CosmologyRead,
2727
CosmologyToFormat,
2828
CosmologyWrite,
2929
)
3030

31-
if TYPE_CHECKING:
32-
from astropy.cosmology._src.funcs.comparison import _FormatType
33-
from astropy.cosmology._src.typing import _CosmoT
34-
3531
# Originally authored by Andrew Becker ([email protected]),
3632
# and modified by Neil Crighton ([email protected]), Roban Kramer
3733
# ([email protected]), and Nathaniel Starkman ([email protected]).
@@ -45,16 +41,13 @@
4541
##############################################################################
4642
# Parameters
4743

48-
# registry of cosmology classes with {key=name : value=class}
49-
_COSMOLOGY_CLASSES: dict[str, type[Cosmology]] = {}
50-
5144
# typing
5245
# NOTE: private b/c RTD error
5346
_FlatCosmoT = TypeVar("_FlatCosmoT", bound="FlatCosmologyMixin")
5447

5548

5649
# dataclass transformation
57-
def _with_signature(cls: type[Cosmology]) -> type[Cosmology]:
50+
def _with_signature(cls: type["Cosmology"]) -> type["Cosmology"]:
5851
"""Decorator to precompute the class' signature.
5952
6053
This provides around a 20x speedup for future calls of ``inspect.signature(cls)``.
@@ -98,14 +91,14 @@ class CosmologyError(Exception):
9891
class _NameField:
9992
default: str | None = None
10093

101-
def __get__(self, instance: Cosmology | None, owner: type) -> str:
94+
def __get__(self, instance: Union["Cosmology", None], owner: type) -> str:
10295
# Called from the class. `dataclass` uses this to create ``__init__``.
10396
if instance is None:
10497
return self.default
10598
# Called from the instance
10699
return instance._name
107100

108-
def __set__(self, instance: Cosmology, value: str | None) -> None:
101+
def __set__(self, instance: "Cosmology", value: str | None) -> None:
109102
object.__setattr__(instance, "_name", (None if value is None else str(value)))
110103

111104

@@ -289,7 +282,9 @@ def _init_has_kwargs(cls):
289282
# ---------------------------------------------------------------
290283
# comparison methods
291284

292-
def is_equivalent(self, other: Any, /, *, format: _FormatType = False) -> bool:
285+
def is_equivalent(
286+
self, other: Any, /, *, format: bool | None | str = False
287+
) -> bool:
293288
r"""Check equivalence between Cosmologies.
294289
295290
Two cosmologies may be equivalent even if not the same class.
@@ -462,6 +457,9 @@ def __astropy_table__(self, cls, copy, **kwargs):
462457
Cosmology.__dataclass_fields__["meta"].compare = False
463458
Cosmology.__dataclass_fields__["meta"].repr = False
464459

460+
# registry of cosmology classes with {key=name : value=class}
461+
_COSMOLOGY_CLASSES: dict[str, type[Cosmology]] = {}
462+
465463

466464
@dataclass_decorator
467465
class FlatCosmologyMixin(metaclass=ABCMeta):

‎astropy/cosmology/_src/flrw/base.py‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from __future__ import annotations
4-
53
__all__ = ["FLRW", "FlatFLRWMixin"]
64

75
import inspect
@@ -13,7 +11,7 @@
1311
from inspect import signature
1412
from math import exp, floor, log, pi, sqrt
1513
from numbers import Number
16-
from typing import TYPE_CHECKING, Self, TypeVar, overload
14+
from typing import Self, TypeVar, overload
1715

1816
import numpy as np
1917
from numpy import inf, sin
@@ -49,10 +47,6 @@
4947
vectorize_redshift_method,
5048
)
5149

52-
if TYPE_CHECKING:
53-
import astropy.units
54-
55-
5650
# isort: split
5751
if HAS_SCIPY:
5852
from scipy.integrate import quad
@@ -1025,16 +1019,14 @@ def _integral_age(self, z, /):
10251019
# Comoving distance
10261020

10271021
@overload
1028-
def comoving_distance(self, z: _InputT) -> astropy.units.Quantity: ...
1022+
def comoving_distance(self, z: _InputT) -> u.Quantity: ...
10291023

10301024
@overload
1031-
def comoving_distance(self, z: _InputT, z2: _InputT) -> astropy.units.Quantity: ...
1025+
def comoving_distance(self, z: _InputT, z2: _InputT) -> u.Quantity: ...
10321026

10331027
@deprecated_keywords("z2", since="7.1")
10341028
@deprecated_keywords("z", since="7.0")
1035-
def comoving_distance(
1036-
self, z: _InputT, z2: _InputT | None = None
1037-
) -> astropy.units.Quantity:
1029+
def comoving_distance(self, z: _InputT, z2: _InputT | None = None) -> u.Quantity:
10381030
r"""Comoving line-of-sight distance :math:`d_c(z1, z2)` in Mpc.
10391031
10401032
The comoving distance along the line-of-sight between two objects

‎astropy/cosmology/_src/io/builtin/cosmology.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313
True
1414
"""
1515

16-
from __future__ import annotations
17-
18-
from typing import TYPE_CHECKING
19-
2016
from astropy.cosmology._src.core import _COSMOLOGY_CLASSES, Cosmology
17+
from astropy.cosmology._src.typing import _CosmoT
2118
from astropy.cosmology.io import convert_registry
2219

23-
if TYPE_CHECKING:
24-
from astropy.cosmology._src.typing import _CosmoT
25-
2620
__all__: list[str] = [] # nothing is publicly scoped
2721

2822

‎astropy/cosmology/_src/io/builtin/ecsv.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,22 @@
159159
>>> temp_dir.cleanup()
160160
"""
161161

162-
from __future__ import annotations
163-
164162
from collections.abc import Mapping
165-
from typing import TYPE_CHECKING, Any, TypeVar
163+
from typing import Any, TypeVar
166164

167165
import astropy.units as u
168-
from astropy.table import QTable
166+
from astropy.table import QTable, Table
169167

170168
# isort: split
171169
import astropy.cosmology.units as cu
172170
from astropy.cosmology._src.core import Cosmology
173171
from astropy.cosmology._src.io.connect import readwrite_registry
172+
from astropy.cosmology._src.typing import _CosmoT
173+
from astropy.io.typing import PathLike, ReadableFileLike, WriteableFileLike
174174

175175
from .table import from_table, to_table
176176

177-
if TYPE_CHECKING:
178-
from astropy.cosmology._src.typing import _CosmoT
179-
from astropy.io.typing import PathLike, ReadableFileLike, WriteableFileLike
180-
from astropy.table import Table
181-
182-
_TableT = TypeVar("_TableT", "Table")
177+
_TableT = TypeVar("_TableT", bound=Table)
183178

184179

185180
def read_ecsv(

‎astropy/cosmology/_src/io/builtin/html.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,22 @@
8585
>>> temp_dir.cleanup()
8686
"""
8787

88-
from __future__ import annotations
89-
90-
from typing import TYPE_CHECKING, Any, TypeVar
88+
from typing import Any, TypeVar
9189

9290
import astropy.units as u
93-
from astropy.table import QTable
91+
from astropy.table import QTable, Table
9492

9593
# isort: split
9694
import astropy.cosmology.units as cu
9795
from astropy.cosmology._src.core import Cosmology
9896
from astropy.cosmology._src.io.connect import readwrite_registry
9997
from astropy.cosmology._src.parameter import Parameter
98+
from astropy.cosmology._src.typing import _CosmoT
99+
from astropy.io.typing import PathLike, ReadableFileLike, WriteableFileLike
100100

101101
from .table import from_table, to_table
102102

103-
if TYPE_CHECKING:
104-
from astropy.cosmology._src.typing import _CosmoT
105-
from astropy.io.typing import PathLike, ReadableFileLike, WriteableFileLike
106-
from astropy.table import Table
107-
108-
_TableT = TypeVar("_TableT", "Table")
103+
_TableT = TypeVar("_TableT", bound=Table)
109104

110105
# Format look-up for conversion, {original_name: new_name}
111106
# TODO! move this information into the Parameters themselves

‎astropy/cosmology/_src/io/builtin/latex.py‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,18 @@
5454
>>> temp_dir.cleanup()
5555
"""
5656

57-
from __future__ import annotations
58-
59-
from typing import TYPE_CHECKING, Any, TypeVar
57+
from typing import Any, TypeVar
6058

6159
import astropy.units as u
62-
from astropy.table import QTable
63-
64-
# isort: split
6560
from astropy.cosmology._src.core import Cosmology
6661
from astropy.cosmology._src.io.connect import readwrite_registry
6762
from astropy.cosmology._src.parameter import Parameter
63+
from astropy.io.typing import PathLike, WriteableFileLike
64+
from astropy.table import QTable, Table
6865

6966
from .table import to_table
7067

71-
if TYPE_CHECKING:
72-
from astropy.io.typing import PathLike, WriteableFileLike
73-
from astropy.table import Table
74-
75-
_TableT = TypeVar("_TableT", Table)
68+
_TableT = TypeVar("_TableT", bound=Table)
7669

7770
_FORMAT_TABLE = {
7871
"H0": "$H_0$",

‎astropy/cosmology/_src/io/builtin/mapping.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,18 @@
126126
'cosmo_name': 'Planck18', ...
127127
"""
128128

129-
from __future__ import annotations
130-
131129
__all__: list[str] = [] # nothing is publicly scoped
132130

133131
import copy
134132
import inspect
135133
from collections.abc import Mapping, MutableMapping
136-
from typing import TYPE_CHECKING, Any, TypeVar
134+
from typing import Any, TypeVar
137135

138136
from astropy.cosmology._src.core import _COSMOLOGY_CLASSES, Cosmology
139137
from astropy.cosmology._src.io.connect import convert_registry
138+
from astropy.cosmology._src.typing import _CosmoT
140139

141-
if TYPE_CHECKING:
142-
from astropy.cosmology._src.typing import _CosmoT
143-
144-
_MapT = TypeVar("_MapT", MutableMapping[str, Any])
140+
_MapT = TypeVar("_MapT", bound=MutableMapping[str, Any])
145141

146142

147143
def _rename_map(

‎astropy/cosmology/_src/io/builtin/model.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
Tcmb0=2.7255 K, Neff=3.046, m_nu=[0. 0. 0.06] eV, Ob0=0.04897)
2828
"""
2929

30-
from __future__ import annotations
31-
3230
import abc
3331
import copy
3432
import inspect

‎astropy/cosmology/_src/io/builtin/row.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,19 @@
3030
For more information on the argument options, see :ref:`cosmology_io_builtin-table`.
3131
"""
3232

33-
from __future__ import annotations
34-
3533
import copy
3634
from collections import defaultdict
3735
from collections.abc import Mapping
38-
from typing import TYPE_CHECKING
3936

40-
from astropy.table import QTable, Row
37+
from astropy.table import QTable, Row, Table
4138

4239
# isort: split
4340
from astropy.cosmology._src.core import Cosmology
4441
from astropy.cosmology._src.io.connect import convert_registry
42+
from astropy.cosmology._src.typing import _CosmoT
4543

4644
from .mapping import from_mapping
4745

48-
if TYPE_CHECKING:
49-
from astropy.cosmology._src.typing import _CosmoT
50-
from astropy.table import Table
51-
5246

5347
def from_row(
5448
row: Row,

‎astropy/cosmology/_src/io/builtin/table.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,21 @@
150150
True
151151
"""
152152

153-
from __future__ import annotations
154-
155153
from collections.abc import Mapping
156-
from typing import TYPE_CHECKING, TypeVar
154+
from typing import TypeVar
157155

158156
import numpy as np
159157

160158
from astropy.cosmology._src.core import Cosmology
161159
from astropy.cosmology._src.io.connect import convert_registry
160+
from astropy.cosmology._src.typing import _CosmoT
162161
from astropy.table import Column, QTable, Table
163162

164163
from .mapping import to_mapping
165164
from .row import from_row
166165
from .utils import convert_parameter_to_column
167166

168-
if TYPE_CHECKING:
169-
from astropy.cosmology._src.typing import _CosmoT
170-
171-
_TableT = TypeVar("_TableT", Table)
167+
_TableT = TypeVar("_TableT", bound=Table)
172168

173169

174170
def from_table(

0 commit comments

Comments
 (0)