Skip to content

Add partial stubs for fractions #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions stdlib/2and3/fractions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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 Real, Integral, Rational
from decimal import Decimal
import sys

_ComparableNum = Union[int, float, Decimal, Real]


@overload
def gcd(a: int, b: int) -> int: ...
@overload
def gcd(a: Integral, b: int) -> Integral: ...
@overload
def gcd(a: int, b: Integral) -> Integral: ...
@overload
def gcd(a: Integral, b: Integral) -> Integral: ...


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): ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a # TODO comment here asking for better annotations in the future?

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): ...

def __hash__(self) -> int: ...
def __eq__(self, other: object) -> 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:
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': ...