Skip to content

Commit

Permalink
Implement Real and Complex type aliases
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eerovaher committed Jul 25, 2024
1 parent bfa65b4 commit f8ed054
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion astropy/units/format/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

if TYPE_CHECKING:
from collections.abc import Iterable
from numbers import Real
from typing import ClassVar, Literal

import numpy as np

from astropy.units import NamedUnit, UnitBase
from astropy.units.typing import Real


class Base:
Expand Down
2 changes: 1 addition & 1 deletion astropy/units/format/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from . import console, utils

if TYPE_CHECKING:
from numbers import Real
from typing import ClassVar, Literal

from astropy.units import NamedUnit, UnitBase
from astropy.units.typing import Real


class Latex(console.Console):
Expand Down
2 changes: 1 addition & 1 deletion astropy/units/format/unicode_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from . import console, utils

if TYPE_CHECKING:
from numbers import Real
from typing import ClassVar

from astropy.units import NamedUnit
from astropy.units.typing import Real


class Unicode(console.Console):
Expand Down
2 changes: 1 addition & 1 deletion astropy/units/format/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

if TYPE_CHECKING:
from collections.abc import Callable, Generator, Iterable, Sequence
from numbers import Real
from typing import TypeVar

from astropy.units import UnitBase
from astropy.units.typing import Real

T = TypeVar("T")

Expand Down
10 changes: 10 additions & 0 deletions astropy/units/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
__all__ = ["QuantityLike"]


from fractions import Fraction
from typing import TYPE_CHECKING

import numpy as np
import numpy.typing as npt

from astropy.units import Quantity
Expand Down Expand Up @@ -64,3 +66,11 @@
For more examples see the :mod:`numpy.typing` definition of
:obj:`numpy.typing.ArrayLike`.
"""


# The classes from the standard library `numbers` module are not suitable for
# type checking (https://github.com/python/mypy/issues/3186). For now we define
# our own number types, but if a good definition becomes available upstream
# then we should switch to that.
Real: TypeAlias = int | float | Fraction | np.integer | np.floating
Complex: TypeAlias = Real | complex | np.complexfloating
3 changes: 2 additions & 1 deletion astropy/units/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

if TYPE_CHECKING:
from collections.abc import Generator, Sequence
from numbers import Complex, Real
from typing import Literal, SupportsFloat, TypeVar

from numpy.typing import ArrayLike, NDArray

from astropy.units.typing import Complex, Real

from .core import UnitBase
from .quantity import Quantity

Expand Down
1 change: 0 additions & 1 deletion docs/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ py:class a shallow copy of D
# types
py:class EllipsisType
py:class ModuleType
py:class Real
# numpy
py:class np.number
# numpy.typing
Expand Down

0 comments on commit f8ed054

Please sign in to comment.