Skip to content

Commit b8e56bf

Browse files
committed
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`.
1 parent 748428d commit b8e56bf

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

stdlib/2and3/fractions.pyi

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Stubs for fractions
2+
# See https://docs.python.org/3/library/fractions.html
3+
4+
from typing import Optional, TypeVar, Union, overload
5+
from numbers import Complex, Rational
6+
from decimal import Decimal
7+
import sys
8+
9+
_TComplex = TypeVar('_TComplex', bound=Complex)
10+
_TOtherNum = TypeVar('_TOtherNum', int, float, Decimal)
11+
_TNum = TypeVar('_TNum', int, float, Decimal, Complex)
12+
13+
@overload
14+
def gcd(a: _TComplex, b: _TComplex) -> _TComplex: ...
15+
@overload
16+
def gcd(a: _TOtherNum, b: _TOtherNum) -> _TOtherNum: ...
17+
18+
class Fraction(Rational):
19+
@overload
20+
def __init__(self,
21+
numerator: Union[int, Rational] = 0,
22+
denominator: Optional[Union[int, Rational]] = 0,
23+
*,
24+
_normalize: bool = True) -> None: ...
25+
@overload
26+
def __init__(self, value: float, *, _normalize=True) -> None: ...
27+
@overload
28+
def __init__(self, value: Decimal, *, _normalize=True) -> None: ...
29+
@overload
30+
def __init__(self, value: str, *, _normalize=True) -> None: ...
31+
32+
@classmethod
33+
def from_float(cls, f: float) -> 'Fraction': ...
34+
@classmethod
35+
def from_decimal(cls, dec: Decimal) -> 'Fraction': ...
36+
def limit_denominator(self, max_denominator: int = 1000000) -> 'Fraction': ...
37+
38+
@property
39+
def numerator(self) -> int: ...
40+
@property
41+
def denominator(self) -> int: ...
42+
43+
def __add__(self, other): ...
44+
def __radd__(self, other): ...
45+
def __sub__(self, other): ...
46+
def __rsub__(self, other): ...
47+
def __mul__(self, other): ...
48+
def __rmul__(self, other): ...
49+
def __truediv__(self, other): ...
50+
def __rtruediv__(self, other): ...
51+
if sys.version_info < (3, 0):
52+
def __div__(self, other): ...
53+
def __rdiv__(self, other): ...
54+
def __floordiv__(self, other) -> int: ...
55+
def __rfloordiv__(self, other) -> int: ...
56+
def __mod__(self, other): ...
57+
def __rmod__(self, other): ...
58+
def __pow__(self, other): ...
59+
def __rpow__(self, other): ...
60+
61+
def __pos__(self) -> 'Fraction': ...
62+
def __neg__(self) -> 'Fraction': ...
63+
def __abs__(self) -> 'Fraction': ...
64+
def __trunc__(self) -> int: ...
65+
if sys.version_info >= (3, 0):
66+
def __floor__(self) -> int: ...
67+
def __ceil__(self) -> int: ...
68+
def __round__(self, ndigits=None): ...
69+
70+
# TODO: Once https://github.com/python/typeshed/pull/543 is
71+
# accepted, add the following definition
72+
# def __hash__(self) -> int: ...
73+
def __eq__(self, other: object) -> bool: ...
74+
def __lt__(self, other: _TNum) -> bool: ...
75+
def __gt__(self, other: _TNum) -> bool: ...
76+
def __le__(self, other: _TNum) -> bool: ...
77+
def __ge__(self, other: _TNum) -> bool: ...
78+
if sys.version_info >= (3, 0):
79+
def __bool__(self) -> bool: ...
80+
else:
81+
def __nonzero__(self) -> bool: ...
82+
83+
# Not actually defined within fractions.py, but provides more useful
84+
# overrides
85+
@property
86+
def real(self) -> 'Fraction': ...
87+
@property
88+
def imag(self) -> 'Fraction': ...
89+
def conjugate(self) -> 'Fraction': ...

0 commit comments

Comments
 (0)