From b8e56bfa1d6b36e8a6f899c018689b282d158ec4 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 13 Sep 2016 20:57:47 -0700 Subject: [PATCH 1/2] Add partial stubs for fractions This commit adds some incomplete stubs for the fractions module. In particular, this commit does not add type signatures for the more complex functions (such as `__add__`), and just leaves their types as effectively `Any`. --- stdlib/2and3/fractions.pyi | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 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..3de8d82ad87a --- /dev/null +++ b/stdlib/2and3/fractions.pyi @@ -0,0 +1,89 @@ +# Stubs for fractions +# See https://docs.python.org/3/library/fractions.html + +from typing import Optional, TypeVar, Union, overload +from numbers import Complex, Rational +from decimal import Decimal +import sys + +_TComplex = TypeVar('_TComplex', bound=Complex) +_TOtherNum = TypeVar('_TOtherNum', int, float, Decimal) +_TNum = TypeVar('_TNum', int, float, Decimal, Complex) + +@overload +def gcd(a: _TComplex, b: _TComplex) -> _TComplex: ... +@overload +def gcd(a: _TOtherNum, b: _TOtherNum) -> _TOtherNum: ... + +class Fraction(Rational): + @overload + def __init__(self, + numerator: Union[int, Rational] = 0, + denominator: Optional[Union[int, Rational]] = 0, + *, + _normalize: bool = True) -> None: ... + @overload + def __init__(self, value: float, *, _normalize=True) -> None: ... + @overload + def __init__(self, value: Decimal, *, _normalize=True) -> None: ... + @overload + def __init__(self, value: str, *, _normalize=True) -> None: ... + + @classmethod + def from_float(cls, f: float) -> 'Fraction': ... + @classmethod + def from_decimal(cls, dec: Decimal) -> 'Fraction': ... + def limit_denominator(self, max_denominator: int = 1000000) -> 'Fraction': ... + + @property + def numerator(self) -> int: ... + @property + def denominator(self) -> int: ... + + def __add__(self, other): ... + def __radd__(self, other): ... + def __sub__(self, other): ... + def __rsub__(self, other): ... + def __mul__(self, other): ... + def __rmul__(self, other): ... + def __truediv__(self, other): ... + def __rtruediv__(self, other): ... + if sys.version_info < (3, 0): + def __div__(self, other): ... + def __rdiv__(self, other): ... + def __floordiv__(self, other) -> int: ... + def __rfloordiv__(self, other) -> int: ... + def __mod__(self, other): ... + def __rmod__(self, other): ... + def __pow__(self, other): ... + def __rpow__(self, other): ... + + def __pos__(self) -> 'Fraction': ... + def __neg__(self) -> 'Fraction': ... + def __abs__(self) -> 'Fraction': ... + def __trunc__(self) -> int: ... + if sys.version_info >= (3, 0): + def __floor__(self) -> int: ... + def __ceil__(self) -> int: ... + def __round__(self, ndigits=None): ... + + # TODO: Once https://github.com/python/typeshed/pull/543 is + # accepted, add the following definition + # def __hash__(self) -> int: ... + def __eq__(self, other: object) -> bool: ... + def __lt__(self, other: _TNum) -> bool: ... + def __gt__(self, other: _TNum) -> bool: ... + def __le__(self, other: _TNum) -> bool: ... + def __ge__(self, other: _TNum) -> bool: ... + if sys.version_info >= (3, 0): + def __bool__(self) -> bool: ... + else: + def __nonzero__(self) -> bool: ... + + # Not actually defined within fractions.py, but provides more useful + # overrides + @property + def real(self) -> 'Fraction': ... + @property + def imag(self) -> 'Fraction': ... + def conjugate(self) -> 'Fraction': ... From fd54238d7ab388d5cff0957b5c9358eabc4a30ba Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 14 Sep 2016 16:36:00 -0700 Subject: [PATCH 2/2] Refine functions.py stubs This commit... 1. Adds back in the __hash__ method 2. Adds a TODO comment 3. Defines a more sane interface for functions.gcd 4. Replaces `_TNum` with a more useful `_ComparableNum` union. --- stdlib/2and3/fractions.pyi | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/stdlib/2and3/fractions.pyi b/stdlib/2and3/fractions.pyi index 3de8d82ad87a..66408fbffedc 100644 --- a/stdlib/2and3/fractions.pyi +++ b/stdlib/2and3/fractions.pyi @@ -1,19 +1,26 @@ # Stubs for fractions # See https://docs.python.org/3/library/fractions.html +# +# Note: these stubs are incomplete. The more complex type +# signatures are currently omitted. Also see numbers.pyi. from typing import Optional, TypeVar, Union, overload -from numbers import Complex, Rational +from numbers import Real, Integral, Rational from decimal import Decimal import sys -_TComplex = TypeVar('_TComplex', bound=Complex) -_TOtherNum = TypeVar('_TOtherNum', int, float, Decimal) -_TNum = TypeVar('_TNum', int, float, Decimal, Complex) +_ComparableNum = Union[int, float, Decimal, Real] + +@overload +def gcd(a: int, b: int) -> int: ... @overload -def gcd(a: _TComplex, b: _TComplex) -> _TComplex: ... +def gcd(a: Integral, b: int) -> Integral: ... @overload -def gcd(a: _TOtherNum, b: _TOtherNum) -> _TOtherNum: ... +def gcd(a: int, b: Integral) -> Integral: ... +@overload +def gcd(a: Integral, b: Integral) -> Integral: ... + class Fraction(Rational): @overload @@ -67,14 +74,12 @@ class Fraction(Rational): def __ceil__(self) -> int: ... def __round__(self, ndigits=None): ... - # TODO: Once https://github.com/python/typeshed/pull/543 is - # accepted, add the following definition - # def __hash__(self) -> int: ... + def __hash__(self) -> int: ... def __eq__(self, other: object) -> bool: ... - def __lt__(self, other: _TNum) -> bool: ... - def __gt__(self, other: _TNum) -> bool: ... - def __le__(self, other: _TNum) -> bool: ... - def __ge__(self, other: _TNum) -> bool: ... + def __lt__(self, other: _ComparableNum) -> bool: ... + def __gt__(self, other: _ComparableNum) -> bool: ... + def __le__(self, other: _ComparableNum) -> bool: ... + def __ge__(self, other: _ComparableNum) -> bool: ... if sys.version_info >= (3, 0): def __bool__(self) -> bool: ... else: