|
1 | 1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
2 | 2 |
|
3 | | -from __future__ import annotations |
4 | 3 |
|
5 | 4 | import inspect |
6 | 5 | from abc import ABCMeta, abstractmethod |
7 | 6 | from collections.abc import Mapping |
8 | 7 | from dataclasses import KW_ONLY, dataclass, replace |
9 | 8 | from types import MappingProxyType |
10 | | -from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeVar |
| 9 | +from typing import Any, ClassVar, Self, TypeVar, Union |
11 | 10 |
|
12 | 11 | import numpy as np |
13 | 12 |
|
|
21 | 20 | ParametersAttribute, |
22 | 21 | all_parameters, |
23 | 22 | ) |
| 23 | +from astropy.cosmology._src.typing import _CosmoT |
24 | 24 | from astropy.cosmology.io import ( |
25 | 25 | CosmologyFromFormat, |
26 | 26 | CosmologyRead, |
27 | 27 | CosmologyToFormat, |
28 | 28 | CosmologyWrite, |
29 | 29 | ) |
30 | 30 |
|
31 | | -if TYPE_CHECKING: |
32 | | - from astropy.cosmology._src.funcs.comparison import _FormatType |
33 | | - from astropy.cosmology._src.typing import _CosmoT |
34 | | - |
35 | 31 | # Originally authored by Andrew Becker ([email protected]), |
36 | 32 | # and modified by Neil Crighton ([email protected]), Roban Kramer |
37 | 33 | |
|
45 | 41 | ############################################################################## |
46 | 42 | # Parameters |
47 | 43 |
|
48 | | -# registry of cosmology classes with {key=name : value=class} |
49 | | -_COSMOLOGY_CLASSES: dict[str, type[Cosmology]] = {} |
50 | | - |
51 | 44 | # typing |
52 | 45 | # NOTE: private b/c RTD error |
53 | 46 | _FlatCosmoT = TypeVar("_FlatCosmoT", bound="FlatCosmologyMixin") |
54 | 47 |
|
55 | 48 |
|
56 | 49 | # dataclass transformation |
57 | | -def _with_signature(cls: type[Cosmology]) -> type[Cosmology]: |
| 50 | +def _with_signature(cls: type["Cosmology"]) -> type["Cosmology"]: |
58 | 51 | """Decorator to precompute the class' signature. |
59 | 52 |
|
60 | 53 | This provides around a 20x speedup for future calls of ``inspect.signature(cls)``. |
@@ -98,14 +91,14 @@ class CosmologyError(Exception): |
98 | 91 | class _NameField: |
99 | 92 | default: str | None = None |
100 | 93 |
|
101 | | - def __get__(self, instance: Cosmology | None, owner: type) -> str: |
| 94 | + def __get__(self, instance: Union["Cosmology", None], owner: type) -> str: |
102 | 95 | # Called from the class. `dataclass` uses this to create ``__init__``. |
103 | 96 | if instance is None: |
104 | 97 | return self.default |
105 | 98 | # Called from the instance |
106 | 99 | return instance._name |
107 | 100 |
|
108 | | - def __set__(self, instance: Cosmology, value: str | None) -> None: |
| 101 | + def __set__(self, instance: "Cosmology", value: str | None) -> None: |
109 | 102 | object.__setattr__(instance, "_name", (None if value is None else str(value))) |
110 | 103 |
|
111 | 104 |
|
@@ -289,7 +282,9 @@ def _init_has_kwargs(cls): |
289 | 282 | # --------------------------------------------------------------- |
290 | 283 | # comparison methods |
291 | 284 |
|
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: |
293 | 288 | r"""Check equivalence between Cosmologies. |
294 | 289 |
|
295 | 290 | Two cosmologies may be equivalent even if not the same class. |
@@ -462,6 +457,9 @@ def __astropy_table__(self, cls, copy, **kwargs): |
462 | 457 | Cosmology.__dataclass_fields__["meta"].compare = False |
463 | 458 | Cosmology.__dataclass_fields__["meta"].repr = False |
464 | 459 |
|
| 460 | +# registry of cosmology classes with {key=name : value=class} |
| 461 | +_COSMOLOGY_CLASSES: dict[str, type[Cosmology]] = {} |
| 462 | + |
465 | 463 |
|
466 | 464 | @dataclass_decorator |
467 | 465 | class FlatCosmologyMixin(metaclass=ABCMeta): |
|
0 commit comments