Skip to content

Type convert_units more exactly #14

@jonathandung

Description

@jonathandung

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions