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 22, 2024
1 parent 31fd209 commit 448b073
Show file tree
Hide file tree
Showing 7 changed files with 22 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.utils.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.utils.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.utils.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.utils.typing import Real

T = TypeVar("T")

Expand Down
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.utils.typing import Complex, Real

from .core import UnitBase
from .quantity import Quantity

Expand Down
16 changes: 16 additions & 0 deletions astropy/utils/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
Experimental typing support for ``astropy``, subject to change
without notice.
"""

from fractions import Fraction
from typing import TypeAlias

import numpy as np

# The classes from the standard library `numbers` module are not suitable for
# type checking (https://github.com/python/mypy/issues/3186).
Real: TypeAlias = int | float | Fraction | np.integer | np.floating
Complex: TypeAlias = Real | complex | np.complexfloating
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 448b073

Please sign in to comment.