Skip to content

Commit 448b073

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 31fd209 commit 448b073

File tree

7 files changed

+22
-6
lines changed

7 files changed

+22
-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.utils.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.utils.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.utils.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.utils.typing import Real
2020

2121
T = TypeVar("T")
2222

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.utils.typing import Complex, Real
26+
2627
from .core import UnitBase
2728
from .quantity import Quantity
2829

astropy/utils/typing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
3+
"""
4+
Experimental typing support for ``astropy``, subject to change
5+
without notice.
6+
"""
7+
8+
from fractions import Fraction
9+
from typing import TypeAlias
10+
11+
import numpy as np
12+
13+
# The classes from the standard library `numbers` module are not suitable for
14+
# type checking (https://github.com/python/mypy/issues/3186).
15+
Real: TypeAlias = int | float | Fraction | np.integer | np.floating
16+
Complex: TypeAlias = Real | complex | np.complexfloating

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)