Skip to content

Commit 002a444

Browse files
Added missing type annotations for fractions and numbers modules. (#4401)
Co-authored-by: Eric Traut <[email protected]>
1 parent 8059ea7 commit 002a444

File tree

2 files changed

+144
-64
lines changed

2 files changed

+144
-64
lines changed

stdlib/2and3/fractions.pyi

+101-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import sys
88
from decimal import Decimal
99
from numbers import Integral, Rational, Real
10-
from typing import Any, Optional, Tuple, Union, overload
10+
from typing import Optional, Tuple, Union, overload
11+
from typing_extensions import Literal
1112

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

@@ -39,33 +40,112 @@ class Fraction(Rational):
3940
def numerator(self) -> int: ...
4041
@property
4142
def denominator(self) -> int: ...
42-
def __add__(self, other): ...
43-
def __radd__(self, other): ...
44-
def __sub__(self, other): ...
45-
def __rsub__(self, other): ...
46-
def __mul__(self, other): ...
47-
def __rmul__(self, other): ...
48-
def __truediv__(self, other): ...
49-
def __rtruediv__(self, other): ...
43+
@overload
44+
def __add__(self, other: Union[int, Fraction]) -> Fraction: ...
45+
@overload
46+
def __add__(self, other: float) -> float: ...
47+
@overload
48+
def __add__(self, other: complex) -> complex: ...
49+
@overload
50+
def __radd__(self, other: Union[int, Fraction]) -> Fraction: ...
51+
@overload
52+
def __radd__(self, other: float) -> float: ...
53+
@overload
54+
def __radd__(self, other: complex) -> complex: ...
55+
@overload
56+
def __sub__(self, other: Union[int, Fraction]) -> Fraction: ...
57+
@overload
58+
def __sub__(self, other: float) -> float: ...
59+
@overload
60+
def __sub__(self, other: complex) -> complex: ...
61+
@overload
62+
def __rsub__(self, other: Union[int, Fraction]) -> Fraction: ...
63+
@overload
64+
def __rsub__(self, other: float) -> float: ...
65+
@overload
66+
def __rsub__(self, other: complex) -> complex: ...
67+
@overload
68+
def __mul__(self, other: Union[int, Fraction]) -> Fraction: ...
69+
@overload
70+
def __mul__(self, other: float) -> float: ...
71+
@overload
72+
def __mul__(self, other: complex) -> complex: ...
73+
@overload
74+
def __rmul__(self, other: Union[int, Fraction]) -> Fraction: ...
75+
@overload
76+
def __rmul__(self, other: float) -> float: ...
77+
@overload
78+
def __rmul__(self, other: complex) -> complex: ...
79+
@overload
80+
def __truediv__(self, other: Union[int, Fraction]) -> Fraction: ...
81+
@overload
82+
def __truediv__(self, other: float) -> float: ...
83+
@overload
84+
def __truediv__(self, other: complex) -> complex: ...
85+
@overload
86+
def __rtruediv__(self, other: Union[int, Fraction]) -> Fraction: ...
87+
@overload
88+
def __rtruediv__(self, other: float) -> float: ...
89+
@overload
90+
def __rtruediv__(self, other: complex) -> complex: ...
5091
if sys.version_info < (3, 0):
51-
def __div__(self, other): ...
52-
def __rdiv__(self, other): ...
53-
def __floordiv__(self, other) -> int: ...
54-
def __rfloordiv__(self, other) -> int: ...
55-
def __mod__(self, other): ...
56-
def __rmod__(self, other): ...
57-
def __divmod__(self, other): ...
58-
def __rdivmod__(self, other): ...
59-
def __pow__(self, other): ...
60-
def __rpow__(self, other): ...
92+
@overload
93+
def __div__(self, other: Union[int, Fraction]) -> Fraction: ...
94+
@overload
95+
def __div__(self, other: float) -> float: ...
96+
@overload
97+
def __div__(self, other: complex) -> complex: ...
98+
@overload
99+
def __rdiv__(self, other: Union[int, Fraction]) -> Fraction: ...
100+
@overload
101+
def __rdiv__(self, other: float) -> float: ...
102+
@overload
103+
def __rdiv__(self, other: complex) -> complex: ...
104+
@overload
105+
def __floordiv__(self, other: Union[int, Fraction]) -> int: ...
106+
@overload
107+
def __floordiv__(self, other: float) -> float: ...
108+
@overload
109+
def __rfloordiv__(self, other: Union[int, Fraction]) -> int: ...
110+
@overload
111+
def __rfloordiv__(self, other: float) -> float: ...
112+
@overload
113+
def __mod__(self, other: Union[int, Fraction]) -> Fraction: ...
114+
@overload
115+
def __mod__(self, other: float) -> float: ...
116+
@overload
117+
def __rmod__(self, other: Union[int, Fraction]) -> Fraction: ...
118+
@overload
119+
def __rmod__(self, other: float) -> float: ...
120+
@overload
121+
def __divmod__(self, other: Union[int, Fraction]) -> Tuple[int, Fraction]: ...
122+
@overload
123+
def __divmod__(self, other: float) -> Tuple[float, Fraction]: ...
124+
@overload
125+
def __rdivmod__(self, other: Union[int, Fraction]) -> Tuple[int, Fraction]: ...
126+
@overload
127+
def __rdivmod__(self, other: float) -> Tuple[float, Fraction]: ...
128+
@overload
129+
def __pow__(self, other: int) -> Fraction: ...
130+
@overload
131+
def __pow__(self, other: Union[float, Fraction]) -> float: ...
132+
@overload
133+
def __pow__(self, other: complex) -> complex: ...
134+
@overload
135+
def __rpow__(self, other: Union[int, float, Fraction]) -> float: ...
136+
@overload
137+
def __rpow__(self, other: complex) -> complex: ...
61138
def __pos__(self) -> Fraction: ...
62139
def __neg__(self) -> Fraction: ...
63140
def __abs__(self) -> Fraction: ...
64141
def __trunc__(self) -> int: ...
65142
if sys.version_info >= (3, 0):
66143
def __floor__(self) -> int: ...
67144
def __ceil__(self) -> int: ...
68-
def __round__(self, ndigits: Optional[Any] = ...): ...
145+
@overload
146+
def __round__(self, ndigits: int) -> Fraction: ...
147+
@overload
148+
def __round__(self, ndigits: None = ...) -> int: ...
69149
def __hash__(self) -> int: ...
70150
def __eq__(self, other: object) -> bool: ...
71151
def __lt__(self, other: _ComparableNum) -> bool: ...
@@ -81,5 +161,5 @@ class Fraction(Rational):
81161
@property
82162
def real(self) -> Fraction: ...
83163
@property
84-
def imag(self) -> Fraction: ...
164+
def imag(self) -> Literal[0]: ...
85165
def conjugate(self) -> Fraction: ...

stdlib/2and3/numbers.pyi

+43-43
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@ class Complex(Number):
2222
def __nonzero__(self) -> bool: ...
2323
@property
2424
@abstractmethod
25-
def real(self): ...
25+
def real(self) -> Any: ...
2626
@property
2727
@abstractmethod
28-
def imag(self): ...
28+
def imag(self) -> Any: ...
2929
@abstractmethod
30-
def __add__(self, other): ...
30+
def __add__(self, other: Any) -> Any: ...
3131
@abstractmethod
32-
def __radd__(self, other): ...
32+
def __radd__(self, other: Any) -> Any: ...
3333
@abstractmethod
34-
def __neg__(self): ...
34+
def __neg__(self) -> Any: ...
3535
@abstractmethod
36-
def __pos__(self): ...
37-
def __sub__(self, other): ...
38-
def __rsub__(self, other): ...
36+
def __pos__(self) -> Any: ...
37+
def __sub__(self, other: Any) -> Any: ...
38+
def __rsub__(self, other: Any) -> Any: ...
3939
@abstractmethod
40-
def __mul__(self, other): ...
40+
def __mul__(self, other: Any) -> Any: ...
4141
@abstractmethod
42-
def __rmul__(self, other): ...
42+
def __rmul__(self, other: Any) -> Any: ...
4343
if sys.version_info < (3, 0):
4444
@abstractmethod
4545
def __div__(self, other): ...
4646
@abstractmethod
4747
def __rdiv__(self, other): ...
4848
@abstractmethod
49-
def __truediv__(self, other): ...
49+
def __truediv__(self, other: Any) -> Any: ...
5050
@abstractmethod
51-
def __rtruediv__(self, other): ...
51+
def __rtruediv__(self, other: Any) -> Any: ...
5252
@abstractmethod
53-
def __pow__(self, exponent): ...
53+
def __pow__(self, exponent: Any) -> Any: ...
5454
@abstractmethod
55-
def __rpow__(self, base): ...
56-
def __abs__(self): ...
57-
def conjugate(self): ...
58-
def __eq__(self, other: object) -> bool: ...
55+
def __rpow__(self, base: Any) -> Any: ...
56+
def __abs__(self) -> Real: ...
57+
def conjugate(self) -> Any: ...
58+
def __eq__(self, other: Any) -> bool: ...
5959
if sys.version_info < (3, 0):
60-
def __ne__(self, other: object) -> bool: ...
60+
def __ne__(self, other: Any) -> bool: ...
6161

6262
class Real(Complex, SupportsFloat):
6363
@abstractmethod
@@ -71,30 +71,30 @@ class Real(Complex, SupportsFloat):
7171
def __ceil__(self) -> int: ...
7272
@abstractmethod
7373
@overload
74-
def __round__(self, ndigits: None = ...): ...
74+
def __round__(self, ndigits: int) -> Any: ...
7575
@abstractmethod
7676
@overload
77-
def __round__(self, ndigits: int): ...
78-
def __divmod__(self, other): ...
79-
def __rdivmod__(self, other): ...
77+
def __round__(self, ndigits: None = ...) -> int: ...
78+
def __divmod__(self, other: Any) -> Any: ...
79+
def __rdivmod__(self, other: Any) -> Any: ...
8080
@abstractmethod
81-
def __floordiv__(self, other): ...
81+
def __floordiv__(self, other: Any) -> int: ...
8282
@abstractmethod
83-
def __rfloordiv__(self, other): ...
83+
def __rfloordiv__(self, other: Any) -> int: ...
8484
@abstractmethod
85-
def __mod__(self, other): ...
85+
def __mod__(self, other: Any) -> Any: ...
8686
@abstractmethod
87-
def __rmod__(self, other): ...
87+
def __rmod__(self, other: Any) -> Any: ...
8888
@abstractmethod
89-
def __lt__(self, other) -> bool: ...
89+
def __lt__(self, other: Any) -> bool: ...
9090
@abstractmethod
91-
def __le__(self, other) -> bool: ...
91+
def __le__(self, other: Any) -> bool: ...
9292
def __complex__(self) -> complex: ...
9393
@property
94-
def real(self): ...
94+
def real(self) -> Any: ...
9595
@property
96-
def imag(self): ...
97-
def conjugate(self): ...
96+
def imag(self) -> Any: ...
97+
def conjugate(self) -> Any: ...
9898

9999
class Rational(Real):
100100
@property
@@ -114,29 +114,29 @@ class Integral(Rational):
114114
def __long__(self) -> long: ...
115115
def __index__(self) -> int: ...
116116
@abstractmethod
117-
def __pow__(self, exponent, modulus: Optional[Any] = ...): ...
117+
def __pow__(self, exponent: Any, modulus: Optional[Any] = ...) -> Any: ...
118118
@abstractmethod
119-
def __lshift__(self, other): ...
119+
def __lshift__(self, other: Any) -> Any: ...
120120
@abstractmethod
121-
def __rlshift__(self, other): ...
121+
def __rlshift__(self, other: Any) -> Any: ...
122122
@abstractmethod
123-
def __rshift__(self, other): ...
123+
def __rshift__(self, other: Any) -> Any: ...
124124
@abstractmethod
125-
def __rrshift__(self, other): ...
125+
def __rrshift__(self, other: Any) -> Any: ...
126126
@abstractmethod
127-
def __and__(self, other): ...
127+
def __and__(self, other: Any) -> Any: ...
128128
@abstractmethod
129-
def __rand__(self, other): ...
129+
def __rand__(self, other: Any) -> Any: ...
130130
@abstractmethod
131-
def __xor__(self, other): ...
131+
def __xor__(self, other: Any) -> Any: ...
132132
@abstractmethod
133-
def __rxor__(self, other): ...
133+
def __rxor__(self, other: Any) -> Any: ...
134134
@abstractmethod
135-
def __or__(self, other): ...
135+
def __or__(self, other: Any) -> Any: ...
136136
@abstractmethod
137-
def __ror__(self, other): ...
137+
def __ror__(self, other: Any) -> Any: ...
138138
@abstractmethod
139-
def __invert__(self): ...
139+
def __invert__(self) -> Any: ...
140140
def __float__(self) -> float: ...
141141
@property
142142
def numerator(self) -> int: ...

0 commit comments

Comments
 (0)