Skip to content

Commit ec2b9ce

Browse files
Michael0x2agvanrossum
authored andcommitted
Add partial stubs for fractions (#544)
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 1d820d4 commit ec2b9ce

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

stdlib/2and3/fractions.pyi

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

0 commit comments

Comments
 (0)