Skip to content

Commit f8ed054

Browse files
committed
Implement Real and Complex type aliases
The Python standard library `numbers` module defines abstract base classes for different types of numbers, but those classes are not suitable for type checking (python/mypy#3186), so `astropy` should define these types itself.
1 parent bfa65b4 commit f8ed054

File tree

7 files changed

+16
-6
lines changed

7 files changed

+16
-6
lines changed

astropy/units/format/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
if TYPE_CHECKING:
1010
from collections.abc import Iterable
11-
from numbers import Real
1211
from typing import ClassVar, Literal
1312

1413
import numpy as np
1514

1615
from astropy.units import NamedUnit, UnitBase
16+
from astropy.units.typing import Real
1717

1818

1919
class Base:

astropy/units/format/latex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from . import console, utils
1313

1414
if TYPE_CHECKING:
15-
from numbers import Real
1615
from typing import ClassVar, Literal
1716

1817
from astropy.units import NamedUnit, UnitBase
18+
from astropy.units.typing import Real
1919

2020

2121
class Latex(console.Console):

astropy/units/format/unicode_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from . import console, utils
1212

1313
if TYPE_CHECKING:
14-
from numbers import Real
1514
from typing import ClassVar
1615

1716
from astropy.units import NamedUnit
17+
from astropy.units.typing import Real
1818

1919

2020
class Unicode(console.Console):

astropy/units/format/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
if TYPE_CHECKING:
1515
from collections.abc import Callable, Generator, Iterable, Sequence
16-
from numbers import Real
1716
from typing import TypeVar
1817

1918
from astropy.units import UnitBase
19+
from astropy.units.typing import Real
2020

2121
T = TypeVar("T")
2222

astropy/units/typing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
__all__ = ["QuantityLike"]
66

77

8+
from fractions import Fraction
89
from typing import TYPE_CHECKING
910

11+
import numpy as np
1012
import numpy.typing as npt
1113

1214
from astropy.units import Quantity
@@ -64,3 +66,11 @@
6466
For more examples see the :mod:`numpy.typing` definition of
6567
:obj:`numpy.typing.ArrayLike`.
6668
"""
69+
70+
71+
# The classes from the standard library `numbers` module are not suitable for
72+
# type checking (https://github.com/python/mypy/issues/3186). For now we define
73+
# our own number types, but if a good definition becomes available upstream
74+
# then we should switch to that.
75+
Real: TypeAlias = int | float | Fraction | np.integer | np.floating
76+
Complex: TypeAlias = Real | complex | np.complexfloating

astropy/units/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
if TYPE_CHECKING:
2020
from collections.abc import Generator, Sequence
21-
from numbers import Complex, Real
2221
from typing import Literal, SupportsFloat, TypeVar
2322

2423
from numpy.typing import ArrayLike, NDArray
2524

25+
from astropy.units.typing import Complex, Real
26+
2627
from .core import UnitBase
2728
from .quantity import Quantity
2829

docs/nitpick-exceptions

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ py:class a shallow copy of D
8383
# types
8484
py:class EllipsisType
8585
py:class ModuleType
86-
py:class Real
8786
# numpy
8887
py:class np.number
8988
# numpy.typing

0 commit comments

Comments
 (0)