From 44eff38b3b93518664ae4520507bae3b49ab256b Mon Sep 17 00:00:00 2001 From: Alessio Bogon Date: Fri, 26 Feb 2016 17:17:57 +0100 Subject: [PATCH 1/2] fractions stub --- stdlib/2and3/fractions.pyi | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 stdlib/2and3/fractions.pyi diff --git a/stdlib/2and3/fractions.pyi b/stdlib/2and3/fractions.pyi new file mode 100644 index 000000000000..426510bb0553 --- /dev/null +++ b/stdlib/2and3/fractions.pyi @@ -0,0 +1,31 @@ +from typing import overload, Union + +import numbers +import decimal + +AnyNumber = Union[numbers.Rational, float, decimal.Decimal, str] + + +class Fraction(numbers.Rational): + @overload + def __init__(self, numerator: numbers.Rational = ..., + denominator: numbers.Rational = ...) -> None: ... + + @overload + def __init__(self, number: AnyNumber) -> None: ... + + @classmethod + def from_float(flt: float) -> Fraction: ... + + @classmethod + def from_decimal(dec: decimal.Decimal) -> Fraction: ... + + def limit_denominator(max_denominator: int = ...) -> 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: ... From e69cfe149c57370e8c6dab4ae27e688e5b979cff Mon Sep 17 00:00:00 2001 From: Alessio Bogon Date: Sat, 27 Feb 2016 01:10:33 +0100 Subject: [PATCH 2/2] add many missing methods --- stdlib/2and3/fractions.pyi | 42 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/stdlib/2and3/fractions.pyi b/stdlib/2and3/fractions.pyi index 426510bb0553..29492b3c3d96 100644 --- a/stdlib/2and3/fractions.pyi +++ b/stdlib/2and3/fractions.pyi @@ -1,18 +1,22 @@ -from typing import overload, Union +from typing import overload, Union, TypeVar import numbers import decimal -AnyNumber = Union[numbers.Rational, float, decimal.Decimal, str] +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 = ...) -> None: ... + denominator: numbers.Rational = ..., + _normalize: bool = ...) -> None: ... @overload - def __init__(self, number: AnyNumber) -> None: ... + def __init__(self, number: Number_ish) -> None: ... @classmethod def from_float(flt: float) -> Fraction: ... @@ -22,9 +26,33 @@ class Fraction(numbers.Rational): def limit_denominator(max_denominator: int = ...) -> Fraction: ... - # TODO: floor, ceil and __round__ are Python 3 only - def floor(self) -> int: ... - def ceil(self) -> int: ... + 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: ...