Skip to content

Commit 283dfed

Browse files
committed
Refine stubs for Python 2's decimal module
The decimal module for Python 2 was relatively incomplete, unlike the decimal module for Python 3. This commit copies the relevant type signatures from Python 3's decimal module to Python 2's. There was a lot of code in both stubs and it wasn't clear to me if it was safe to merge the two modules together, so I refrained from doing so.
1 parent 312725e commit 283dfed

File tree

1 file changed

+129
-116
lines changed

1 file changed

+129
-116
lines changed

stdlib/2.7/decimal.pyi

Lines changed: 129 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
# Stubs for decimal (Python 2)
2-
#
3-
# NOTE: This dynamically typed stub was automatically generated by stubgen.
42

5-
from typing import Any, SupportsAbs, SupportsFloat, SupportsInt
3+
from typing import (
4+
Any, Dict, NamedTuple, Optional, Sequence, Tuple, Union,
5+
SupportsAbs, SupportsFloat, SupportsInt,
6+
)
67

7-
ROUND_DOWN = ... # type: Any
8-
ROUND_HALF_UP = ... # type: Any
9-
ROUND_HALF_EVEN = ... # type: Any
10-
ROUND_CEILING = ... # type: Any
11-
ROUND_FLOOR = ... # type: Any
12-
ROUND_UP = ... # type: Any
13-
ROUND_HALF_DOWN = ... # type: Any
14-
ROUND_05UP = ... # type: Any
8+
_Decimal = Union[Decimal, int]
9+
10+
DecimalTuple = NamedTuple('DecimalTuple',
11+
[('sign', int),
12+
('digits', Sequence[int]), # TODO: Use Tuple[int, ...]
13+
('exponent', int)])
14+
15+
ROUND_DOWN = ... # type: str
16+
ROUND_HALF_UP = ... # type: str
17+
ROUND_HALF_EVEN = ... # type: str
18+
ROUND_CEILING = ... # type: str
19+
ROUND_FLOOR = ... # type: str
20+
ROUND_UP = ... # type: str
21+
ROUND_HALF_DOWN = ... # type: str
22+
ROUND_05UP = ... # type: str
1523

1624
class DecimalException(ArithmeticError):
1725
def handle(self, context, *args): ...
@@ -40,120 +48,125 @@ class Overflow(Inexact, Rounded): ...
4048

4149
class Underflow(Inexact, Rounded, Subnormal): ...
4250

43-
def setcontext(context): ...
44-
def getcontext(): ...
45-
def localcontext(ctx=None): ...
51+
def setcontext(context: Context): ...
52+
def getcontext() -> Context: ...
53+
def localcontext(ctx: Optional[Context] = None) -> _ContextManager: ...
4654

4755
class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt):
48-
def __new__(cls, value=..., context=None): ...
56+
def __init__(cls, value: Union[_Decimal, float, str,
57+
Tuple[int, Sequence[int], int]] = ...,
58+
context: Context = ...) -> None: ...
4959
@classmethod
50-
def from_float(cls, f): ...
51-
def __nonzero__(self): ...
52-
def __eq__(self, other, context=None): ...
53-
def __ne__(self, other, context=None): ...
54-
def __lt__(self, other, context=None): ...
55-
def __le__(self, other, context=None): ...
56-
def __gt__(self, other, context=None): ...
57-
def __ge__(self, other, context=None): ...
58-
def compare(self, other, context=None): ...
59-
def __hash__(self): ...
60-
def as_tuple(self): ...
61-
def to_eng_string(self, context=None): ...
62-
def __neg__(self, context=None): ...
63-
def __pos__(self, context=None): ...
64-
def __abs__(self, round=True, context=None): ...
65-
def __add__(self, other, context=None): ...
66-
def __radd__(self, other, context=None): ...
67-
def __sub__(self, other, context=None): ...
68-
def __rsub__(self, other, context=None): ...
69-
def __mul__(self, other, context=None): ...
70-
def __rmul__(self, other, context=None): ...
71-
def __truediv__(self, other, context=None): ...
72-
def __rtruediv__(self, other, context=None): ...
73-
def __div__(self, other, context=None): ...
74-
def __rdiv__(self, other, context=None): ...
75-
def __divmod__(self, other, context=None): ...
76-
def __rdivmod__(self, other, context=None): ...
77-
def __mod__(self, other, context=None): ...
78-
def __rmod__(self, other, context=None): ...
79-
def remainder_near(self, other, context=None): ...
80-
def __floordiv__(self, other, context=None): ...
81-
def __rfloordiv__(self, other, context=None): ...
82-
def __float__(self): ...
83-
def __int__(self): ...
84-
def __trunc__(self): ...
85-
real = ... # type: property
86-
imag = ... # type: property
87-
def conjugate(self): ...
88-
def __complex__(self): ...
89-
def __long__(self): ...
90-
def fma(self, other, third, context=None): ...
91-
def __pow__(self, other, modulo=None, context=None): ...
92-
def __rpow__(self, other, context=None): ...
93-
def normalize(self, context=None): ...
94-
def quantize(self, exp, rounding=None, context=None, watchexp=True): ...
95-
def same_quantum(self, other): ...
96-
def to_integral_exact(self, rounding=None, context=None): ...
97-
def to_integral_value(self, rounding=None, context=None): ...
98-
def to_integral(self, rounding=None, context=None): ...
99-
def sqrt(self, context=None): ...
100-
def max(self, other, context=None): ...
101-
def min(self, other, context=None): ...
102-
def adjusted(self): ...
103-
def canonical(self, context=None): ...
104-
def compare_signal(self, other, context=None): ...
105-
def compare_total(self, other): ...
106-
def compare_total_mag(self, other): ...
107-
def copy_abs(self): ...
108-
def copy_negate(self): ...
109-
def copy_sign(self, other): ...
110-
def exp(self, context=None): ...
111-
def is_canonical(self): ...
112-
def is_finite(self): ...
113-
def is_infinite(self): ...
114-
def is_nan(self): ...
115-
def is_normal(self, context=None): ...
116-
def is_qnan(self): ...
117-
def is_signed(self): ...
118-
def is_snan(self): ...
119-
def is_subnormal(self, context=None): ...
120-
def is_zero(self): ...
121-
def ln(self, context=None): ...
122-
def log10(self, context=None): ...
123-
def logb(self, context=None): ...
124-
def logical_and(self, other, context=None): ...
125-
def logical_invert(self, context=None): ...
126-
def logical_or(self, other, context=None): ...
127-
def logical_xor(self, other, context=None): ...
128-
def max_mag(self, other, context=None): ...
129-
def min_mag(self, other, context=None): ...
130-
def next_minus(self, context=None): ...
131-
def next_plus(self, context=None): ...
132-
def next_toward(self, other, context=None): ...
133-
def number_class(self, context=None): ...
134-
def radix(self): ...
135-
def rotate(self, other, context=None): ...
136-
def scaleb(self, other, context=None): ...
137-
def shift(self, other, context=None): ...
60+
def from_float(cls, f: float) -> Decimal: ...
61+
def __nonzero__(self) -> bool: ...
62+
def __eq__(self, other: object) -> bool: ...
63+
def __ne__(self, other: object) -> bool: ...
64+
def __lt__(self, other: _Decimal) -> bool: ...
65+
def __le__(self, other: _Decimal) -> bool: ...
66+
def __gt__(self, other: _Decimal) -> bool: ...
67+
def __ge__(self, other: _Decimal) -> bool: ...
68+
def compare(self, other: _Decimal) -> int: ...
69+
def __hash__(self) -> int: ...
70+
def as_tuple(self) -> DecimalTuple: ...
71+
def to_eng_string(self, context: Context = ...) -> str: ...
72+
def __neg__(self) -> Decimal: ...
73+
def __pos__(self) -> Decimal: ...
74+
def __abs__(self, round: bool = True) -> Decimal: ...
75+
def __add__(self, other: _Decimal) -> Decimal: ...
76+
def __radd__(self, other: _Decimal) -> Decimal: ...
77+
def __sub__(self, other: _Decimal) -> Decimal: ...
78+
def __rsub__(self, other: _Decimal) -> Decimal: ...
79+
def __mul__(self, other: _Decimal) -> Decimal: ...
80+
def __rmul__(self, other: _Decimal) -> Decimal: ...
81+
def __truediv__(self, other: _Decimal) -> Decimal: ...
82+
def __rtruediv__(self, other: _Decimal) -> Decimal: ...
83+
def __div__(self, other: _Decimal) -> Decimal: ...
84+
def __rdiv__(self, other: _Decimal) -> Decimal: ...
85+
def __divmod__(self, other: _Decimal) -> Decimal: ...
86+
def __rdivmod__(self, other: _Decimal) -> Decimal: ...
87+
def __mod__(self, other: _Decimal) -> Decimal: ...
88+
def __rmod__(self, other: _Decimal) -> Decimal: ...
89+
def remainder_near(self, other: _Decimal, context: Context = ...) -> Decimal: ...
90+
def __floordiv__(self, other: _Decimal) -> Decimal: ...
91+
def __rfloordiv__(self, other: _Decimal) -> Decimal: ...
92+
def __float__(self) -> float: ...
93+
def __int__(self) -> int: ...
94+
def __trunc__(self) -> int: ...
95+
@property
96+
def imag(self) -> Decimal: ...
97+
@property
98+
def real(self) -> Decimal: ...
99+
def conjugate(self) -> Decimal: ...
100+
def __complex__(self) -> complex: ...
101+
def __long__(self) -> long: ...
102+
def fma(self, other: _Decimal, third: _Decimal, context: Context = ...) -> Decimal: ...
103+
def __pow__(self, other: _Decimal) -> Decimal: ...
104+
def __rpow__(self, other: int) -> Decimal: ...
105+
def normalize(self, context: Context = ...) -> Decimal: ...
106+
def quantize(self, exp: _Decimal, rounding: str = ...,
107+
context: Context = ...) -> Decimal: ...
108+
def same_quantum(self, other: Decimal) -> bool: ...
109+
def to_integral(self, rounding: str = ..., context: Context = ...) -> Decimal: ...
110+
def to_integral_exact(self, rounding: str = ..., context: Context = ...) -> Decimal: ...
111+
def to_integral_value(self, rounding: str = ..., context: Context = ...) -> Decimal: ...
112+
def sqrt(self, context: Context = ...) -> Decimal: ...
113+
def max(self, other: _Decimal, context: Context = ...) -> Decimal: ...
114+
def min(self, other: _Decimal, context: Context = ...) -> Decimal: ...
115+
def adjusted(self) -> int: ...
116+
def canonical(self, context: Context = ...) -> Decimal: ...
117+
def compare_signal(self, other: _Decimal, context: Context = ...) -> Decimal: ...
118+
def compare_total(self, other: _Decimal) -> Decimal: ...
119+
def compare_total_mag(self, other: _Decimal) -> Decimal: ...
120+
def copy_abs(self) -> Decimal: ...
121+
def copy_negate(self) -> Decimal: ...
122+
def copy_sign(self, other: _Decimal) -> Decimal: ...
123+
def exp(self, context: Context = ...) -> Decimal: ...
124+
def is_canonical(self) -> bool: ...
125+
def is_finite(self) -> bool: ...
126+
def is_infinite(self) -> bool: ...
127+
def is_nan(self) -> bool: ...
128+
def is_normal(self, context: Context = ...) -> bool: ...
129+
def is_qnan(self) -> bool: ...
130+
def is_signed(self) -> bool: ...
131+
def is_snan(self) -> bool: ...
132+
def is_subnormal(self, context: Context = ...) -> bool: ...
133+
def is_zero(self) -> bool: ...
134+
def ln(self, context: Context = ...) -> Decimal: ...
135+
def log10(self, context: Context = ...) -> Decimal: ...
136+
def logb(self, context: Context = ...) -> Decimal: ...
137+
def logical_and(self, other: _Decimal, context: Context = ...) -> Decimal: ...
138+
def logical_invert(self, context: Context = ...) -> Decimal: ...
139+
def logical_or(self, other: _Decimal, context: Context = ...) -> Decimal: ...
140+
def logical_xor(self, other: _Decimal, context: Context = ...) -> Decimal: ...
141+
def max_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ...
142+
def min_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ...
143+
def next_minus(self, context: Context = ...) -> Decimal: ...
144+
def next_plus(self, context: Context = ...) -> Decimal: ...
145+
def next_toward(self, other: _Decimal, context: Context = ...) -> Decimal: ...
146+
def number_class(self, context: Context = ...) -> str: ...
147+
def radix(self) -> Decimal: ...
148+
def rotate(self, other: _Decimal, context: Context = ...) -> Decimal: ...
149+
def scaleb(self, other: _Decimal, context: Context = ...) -> Decimal: ...
150+
def shift(self, other: _Decimal, context: Context = ...) -> Decimal: ...
138151
def __reduce__(self): ...
139152
def __copy__(self): ...
140153
def __deepcopy__(self, memo): ...
141-
def __format__(self, specifier, context=None, _localeconv=None): ...
154+
def __format__(self, specifier, context=None, _localeconv=None) -> str: ...
142155

143156
class _ContextManager:
144-
new_context = ... # type: Any
145-
def __init__(self, new_context): ...
146-
saved_context = ... # type: Any
157+
new_context = ... # type: Context
158+
saved_context = ... # type: Context
159+
def __init__(self, new_context: Context) -> None: ...
147160
def __enter__(self): ...
148161
def __exit__(self, t, v, tb): ...
149162

150163
class Context:
151-
prec = ... # type: Any
152-
rounding = ... # type: Any
153-
Emin = ... # type: Any
154-
Emax = ... # type: Any
155-
capitals = ... # type: Any
156-
traps = ... # type: Any
164+
prec = ... # type: int
165+
rounding = ... # type: str
166+
Emin = ... # type: int
167+
Emax = ... # type: int
168+
capitals = ... # type: int
169+
traps = ... # type: Dict[type, bool]
157170
flags = ... # type: Any
158171
def __init__(self, prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=None, capitals=None, _clamp=0, _ignored_flags=None): ...
159172
def clear_flags(self): ...
@@ -226,6 +239,6 @@ class Context:
226239
def to_integral_value(self, a): ...
227240
def to_integral(self, a): ...
228241

229-
DefaultContext = ... # type: Any
230-
BasicContext = ... # type: Any
231-
ExtendedContext = ... # type: Any
242+
DefaultContext = ... # type: Context
243+
BasicContext = ... # type: Context
244+
ExtendedContext = ... # type: Context

0 commit comments

Comments
 (0)