diff --git a/stdlib/2and3/fractions.pyi b/stdlib/2and3/fractions.pyi new file mode 100644 index 000000000000..29492b3c3d96 --- /dev/null +++ b/stdlib/2and3/fractions.pyi @@ -0,0 +1,59 @@ +from typing import overload, Union, TypeVar + +import numbers +import decimal + +Number_ish = Union[int, float, str, numbers.Rational, decimal.Decimal] + +UpToComplexT = TypeVar('UpToComplexT', numbers.Rational, + numbers.Real, numbers.Complex) +UpToRealT = TypeVar('UpToRealT', numbers.Rational, numbers.Real) + +class Fraction(numbers.Rational): + @overload + def __init__(self, numerator: numbers.Rational = ..., + denominator: numbers.Rational = ..., + _normalize: bool = ...) -> None: ... + + @overload + def __init__(self, number: Number_ish) -> None: ... + + @classmethod + def from_float(flt: float) -> Fraction: ... + + @classmethod + def from_decimal(dec: decimal.Decimal) -> Fraction: ... + + def limit_denominator(max_denominator: int = ...) -> Fraction: ... + + def __add__(self, other: UpToComplexT) -> UpToComplexT: ... + def __radd__(self, other: UpToComplexT) -> UpToComplexT: ... + def __sub__(self, other: UpToComplexT) -> UpToComplexT: ... + def __rsub__(self, other: UpToComplexT) -> UpToComplexT: ... + def __mul__(self, other: UpToComplexT) -> UpToComplexT: ... + def __rmul__(self, other: UpToComplexT) -> UpToComplexT: ... + def __truediv__(self, other: UpToComplexT) -> UpToComplexT: ... + def __rtruediv__(self, other: UpToComplexT) -> UpToComplexT: ... + def __floordiv__(self, other: UpToComplexT) -> numbers.Integral: ... + def __rfloordiv__(self, other: UpToComplexT) -> numbers.Integral: ... + + def __mod__(self, other: UpToRealT) -> UpToRealT: ... + def __rmod__(self, other: UpToRealT) -> UpToRealT: ... + + @overload + def __pow__(self, other: numbers.Integral) -> Fraction: ... + @overload + def __pow__(self, other: numbers.Complex) -> numbers.Complex: ... + + def __rpow__(self, other: numbers.Complex) -> numbers.Complex: ... + def __pos__(self) -> Fraction: ... + def __neg__(self) -> Fraction: ... + def __abs__(self) -> Fraction: ... + + # TODO: __floor__, __ceil__ and __round__ are Python 3 only + def __floor__(self) -> int: ... + def __ceil__(self) -> int: ... + def __round__(self, ndigits: int = ...) -> Fraction: ... + + +def gcd(a: int, b: int) -> int: ...