The convert_units function can be annotated more exactly. Since decimal.Decimal instances are returned if and only if the exact argument is True and float is returned otherwise, the following overloads can be added above the implementation:
from typing import Literal, Optional, overload
@overload
def convert_units(
n: float,
unit: int = ...,
to: Optional[int] = None,
si: bool = False,
exact: Literal[False] = False
) -> Tuplefloat, str]: ...
@overload
def convert_units(
n: float,
unit: int,
to: Optional[int],
si: bool,
exact: Literal[True]
) -> Tuple[Decimal, str]: ...
@overload
def convert_units(
n: float,
unit: int = ...,
to: Optional[int] = None,
si: bool = False,
*,
exact: Literal[True]
) -> Tuple[Decimal, str]: ...
Besides, I believe that the n parameter can be an instance of decimal.Decimal, and the logic of the function would still work correctly.
The
convert_unitsfunction can be annotated more exactly. Sincedecimal.Decimalinstances are returned if and only if theexactargument isTrueandfloatis returned otherwise, the following overloads can be added above the implementation:Besides, I believe that the
nparameter can be an instance ofdecimal.Decimal, and the logic of the function would still work correctly.