Skip to content

Commit

Permalink
Merge pull request astropy#17605 from eerovaher/unit-type-aliases
Browse files Browse the repository at this point in the history
Rename `Complex` and `Real` type aliases
  • Loading branch information
pllim authored Jan 6, 2025
2 parents f612e15 + ed2c4e9 commit 654e241
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
12 changes: 6 additions & 6 deletions astropy/units/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from .format import Base
from .physical import PhysicalType
from .quantity import Quantity
from .typing import Complex, Real, UnitPower, UnitScale
from .typing import UnitPower, UnitPowerLike, UnitScale, UnitScaleLike

__all__ = [
"CompositeUnit",
Expand Down Expand Up @@ -774,7 +774,7 @@ def _normalize_equivalencies(equivalencies):

return normalized

def __pow__(self, p: Real) -> CompositeUnit:
def __pow__(self, p: UnitPowerLike) -> CompositeUnit:
try: # Handling scalars should be as quick as possible
return CompositeUnit(1, [self], [sanitize_power(p)], _error_check=False)
except Exception:
Expand Down Expand Up @@ -2081,9 +2081,9 @@ def __call__(
_error_check=False,
)

from .typing import Complex
from .typing import UnitScaleLike

if isinstance(s, Complex): # same as the annotation in sanitize_scale_type()
if isinstance(s, UnitScaleLike):
return CompositeUnit(s, [], [])

if isinstance(s, tuple):
Expand Down Expand Up @@ -2261,9 +2261,9 @@ class CompositeUnit(UnitBase):
@overload
def __init__(
self,
scale: Complex,
scale: UnitScaleLike,
bases: Sequence[UnitBase],
powers: Sequence[Real],
powers: Sequence[UnitPowerLike],
decompose: bool = False,
decompose_bases: Collection[UnitBase] = (),
_error_check: Literal[True] = True,
Expand Down
9 changes: 2 additions & 7 deletions astropy/units/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@
"""


# 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

UnitPower: TypeAlias = int | float | Fraction
UnitPowerLike: TypeAlias = UnitPower | np.integer | np.floating
UnitScale: TypeAlias = int | float | Fraction | complex
UnitScaleLike: TypeAlias = UnitScale | np.number
14 changes: 8 additions & 6 deletions astropy/units/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from .core import UnitBase
from .quantity import Quantity
from .typing import Complex, Real, UnitPower, UnitScale
from .typing import UnitPower, UnitPowerLike, UnitScale, UnitScaleLike

DType = TypeVar("DType", bound=np.generic)
FloatLike = TypeVar("FloatLike", bound=SupportsFloat)
Expand Down Expand Up @@ -181,7 +181,7 @@ def generate_prefixonly_unit_summary(namespace: dict[str, object]) -> str:
return docstring.getvalue()


def is_effectively_unity(value: Complex) -> bool:
def is_effectively_unity(value: UnitScaleLike) -> bool:
# value is *almost* always real, except, e.g., for u.mag**0.5, when
# it will be complex. Use try/except to ensure normal case is fast
try:
Expand All @@ -193,7 +193,7 @@ def is_effectively_unity(value: Complex) -> bool:
)


def sanitize_scale_type(scale: Complex) -> UnitScale:
def sanitize_scale_type(scale: UnitScaleLike) -> UnitScale:
if not scale:
raise UnitScaleError("cannot create a unit with a scale of 0.")

Expand Down Expand Up @@ -226,7 +226,7 @@ def sanitize_scale_value(scale: UnitScale) -> UnitScale:
return scale.real


def maybe_simple_fraction(p: Real, max_denominator: int = 100) -> UnitPower:
def maybe_simple_fraction(p: UnitPowerLike, max_denominator: int = 100) -> UnitPower:
"""Fraction very close to x with denominator at most max_denominator.
The fraction has to be such that fraction/x is unity to within 4 ulp.
Expand Down Expand Up @@ -257,7 +257,7 @@ def maybe_simple_fraction(p: Real, max_denominator: int = 100) -> UnitPower:
return float(p)


def sanitize_power(p: Real) -> UnitPower:
def sanitize_power(p: UnitPowerLike) -> UnitPower:
"""Convert the power to a float, an integer, or a Fraction.
If a fractional power can be represented exactly as a floating point
Expand Down Expand Up @@ -297,7 +297,9 @@ def sanitize_power(p: Real) -> UnitPower:
return p


def resolve_fractions(a: Real, b: Real) -> tuple[Real, Real]:
def resolve_fractions(
a: UnitPowerLike, b: UnitPowerLike
) -> tuple[UnitPowerLike, UnitPowerLike]:
"""
If either input is a Fraction, convert the other to a Fraction
(at least if it does not have a ridiculous denominator).
Expand Down
4 changes: 2 additions & 2 deletions docs/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ py:class np.ma.MaskedArray
# locally defined type variable for ndarray dtype
py:class DT
# type aliases
py:class Complex
py:class Real
py:class UnitPower
py:class UnitPowerLike
py:class UnitScale
py:class UnitScaleLike

# Classes from `astropy.extern` that are nonetheless exposed in documentation
py:class Lexer
Expand Down

0 comments on commit 654e241

Please sign in to comment.