From a38dbc6c1edf71012e0a16526e9ad5f10f963ed9 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 10 Jul 2018 04:03:01 +0200 Subject: [PATCH] Merge decimal (#2306) --- stdlib/{2 => 2and3}/decimal.pyi | 156 +++++++++++++------ stdlib/3/decimal.pyi | 255 -------------------------------- 2 files changed, 107 insertions(+), 304 deletions(-) rename stdlib/{2 => 2and3}/decimal.pyi (66%) delete mode 100644 stdlib/3/decimal.pyi diff --git a/stdlib/2/decimal.pyi b/stdlib/2and3/decimal.pyi similarity index 66% rename from stdlib/2/decimal.pyi rename to stdlib/2and3/decimal.pyi index 98d754113..0913f0e4a 100644 --- a/stdlib/2/decimal.pyi +++ b/stdlib/2and3/decimal.pyi @@ -1,8 +1,7 @@ -# Stubs for decimal (Python 2) - +import sys from typing import ( - Any, Dict, NamedTuple, Optional, Sequence, Tuple, Union, - SupportsAbs, SupportsFloat, SupportsInt, + Any, Dict, NamedTuple, Optional, Sequence, Tuple, Union, Text, + SupportsAbs, SupportsFloat, SupportsInt, SupportsRound, ) _Decimal = Union[Decimal, int] @@ -13,14 +12,21 @@ DecimalTuple = NamedTuple('DecimalTuple', ('digits', Sequence[int]), # TODO: Use Tuple[int, ...] ('exponent', int)]) -ROUND_DOWN = ... # type: str -ROUND_HALF_UP = ... # type: str -ROUND_HALF_EVEN = ... # type: str -ROUND_CEILING = ... # type: str -ROUND_FLOOR = ... # type: str -ROUND_UP = ... # type: str -ROUND_HALF_DOWN = ... # type: str -ROUND_05UP = ... # type: str +ROUND_DOWN: str +ROUND_HALF_UP: str +ROUND_HALF_EVEN: str +ROUND_CEILING: str +ROUND_FLOOR: str +ROUND_UP: str +ROUND_HALF_DOWN: str +ROUND_05UP: str + +if sys.version_info >= (3,): + HAVE_THREADS: bool + MAX_EMAX: int + MAX_PREC: int + MIN_EMIN: int + MIN_ETINY: int class DecimalException(ArithmeticError): def handle(self, context, *args): ... @@ -49,24 +55,37 @@ class Overflow(Inexact, Rounded): ... class Underflow(Inexact, Rounded, Subnormal): ... -def setcontext(context: Context): ... +if sys.version_info >= (3,): + class FloatOperation(DecimalException, TypeError): ... + +def setcontext(context: Context) -> None: ... def getcontext() -> Context: ... def localcontext(ctx: Optional[Context] = ...) -> _ContextManager: ... -class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt): - def __init__(cls, value: Union[_Decimal, float, str, unicode, +if sys.version_info >= (3,): + _SupportsRound = SupportsRound[int] +else: + _SupportsRound = object + +class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt, _SupportsRound): + # TODO: SupportsCeil, SupportsFloor, SupportsTrunc? + + def __init__(cls, value: Union[_Decimal, float, Text, Tuple[int, Sequence[int], int]] = ..., context: Context = ...) -> None: ... @classmethod def from_float(cls, f: float) -> Decimal: ... - def __nonzero__(self) -> bool: ... + if sys.version_info >= (3,): + def __bool__(self) -> bool: ... + else: + def __nonzero__(self) -> bool: ... def __eq__(self, other: object) -> bool: ... def __ne__(self, other: object) -> bool: ... def __lt__(self, other: _ComparableNum) -> bool: ... def __le__(self, other: _ComparableNum) -> bool: ... def __gt__(self, other: _ComparableNum) -> bool: ... def __ge__(self, other: _ComparableNum) -> bool: ... - def compare(self, other: _Decimal) -> Decimal: ... + def compare(self, other: _Decimal, context: Optional[Context] = ...) -> Decimal: ... def __hash__(self) -> int: ... def as_tuple(self) -> DecimalTuple: ... def to_eng_string(self, context: Context = ...) -> str: ... @@ -81,8 +100,9 @@ class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt): def __rmul__(self, other: int) -> Decimal: ... def __truediv__(self, other: _Decimal) -> Decimal: ... def __rtruediv__(self, other: int) -> Decimal: ... - def __div__(self, other: _Decimal) -> Decimal: ... - def __rdiv__(self, other: int) -> Decimal: ... + if sys.version_info < (3,): + def __div__(self, other: _Decimal) -> Decimal: ... + def __rdiv__(self, other: int) -> Decimal: ... def __divmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ... def __rdivmod__(self, other: int) -> Tuple[Decimal, Decimal]: ... def __mod__(self, other: _Decimal) -> Decimal: ... @@ -99,14 +119,22 @@ class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt): def real(self) -> Decimal: ... def conjugate(self) -> Decimal: ... def __complex__(self) -> complex: ... - def __long__(self) -> long: ... + if sys.version_info >= (3,): + def __round__(self, n=...) -> int: ... + def __floor__(self) -> int: ... + def __ceil__(self) -> int: ... + else: + def __long__(self) -> long: ... def fma(self, other: _Decimal, third: _Decimal, context: Context = ...) -> Decimal: ... def __pow__(self, other: _Decimal) -> Decimal: ... def __rpow__(self, other: int) -> Decimal: ... def normalize(self, context: Context = ...) -> Decimal: ... def quantize(self, exp: _Decimal, rounding: str = ..., context: Context = ...) -> Decimal: ... - def same_quantum(self, other: Decimal) -> bool: ... + if sys.version_info >= (3,): + def same_quantum(self, other: _Decimal, context: Context = ...) -> bool: ... + else: + def same_quantum(self, other: _Decimal) -> bool: ... def to_integral(self, rounding: str = ..., context: Context = ...) -> Decimal: ... def to_integral_exact(self, rounding: str = ..., context: Context = ...) -> Decimal: ... def to_integral_value(self, rounding: str = ..., context: Context = ...) -> Decimal: ... @@ -114,13 +142,23 @@ class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt): def max(self, other: _Decimal, context: Context = ...) -> Decimal: ... def min(self, other: _Decimal, context: Context = ...) -> Decimal: ... def adjusted(self) -> int: ... - def canonical(self, context: Context = ...) -> Decimal: ... + if sys.version_info >= (3,): + def canonical(self) -> Decimal: ... + else: + def canonical(self, context: Context = ...) -> Decimal: ... def compare_signal(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def compare_total(self, other: _Decimal) -> Decimal: ... - def compare_total_mag(self, other: _Decimal) -> Decimal: ... + if sys.version_info >= (3,): + def compare_total(self, other: _Decimal, context: Context = ...) -> Decimal: ... + def compare_total_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ... + else: + def compare_total(self, other: _Decimal) -> Decimal: ... + def compare_total_mag(self, other: _Decimal) -> Decimal: ... def copy_abs(self) -> Decimal: ... def copy_negate(self) -> Decimal: ... - def copy_sign(self, other: _Decimal) -> Decimal: ... + if sys.version_info >= (3,): + def copy_sign(self, other: _Decimal, context: Context = ...) -> Decimal: ... + else: + def copy_sign(self, other: _Decimal) -> Decimal: ... def exp(self, context: Context = ...) -> Decimal: ... def is_canonical(self) -> bool: ... def is_finite(self) -> bool: ... @@ -150,36 +188,56 @@ class Decimal(SupportsAbs[Decimal], SupportsFloat, SupportsInt): def scaleb(self, other: _Decimal, context: Context = ...) -> Decimal: ... def shift(self, other: _Decimal, context: Context = ...) -> Decimal: ... def __reduce__(self): ... - def __copy__(self): ... - def __deepcopy__(self, memo): ... - def __format__(self, specifier, context=None, _localeconv=None) -> str: ... + def __copy__(self) -> Decimal: ... + def __deepcopy__(self, memo) -> Decimal: ... + def __format__(self, specifier, context=..., _localeconv=...) -> str: ... class _ContextManager: - new_context = ... # type: Context - saved_context = ... # type: Context + new_context: Context + saved_context: Context def __init__(self, new_context: Context) -> None: ... - def __enter__(self): ... - def __exit__(self, t, v, tb): ... + def __enter__(self) -> Context: ... + def __exit__(self, t, v, tb) -> None: ... class Context: - prec = ... # type: int - rounding = ... # type: str - Emin = ... # type: int - Emax = ... # type: int - capitals = ... # type: int - traps = ... # type: Dict[type, bool] - flags = ... # type: Any - def __init__(self, prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=None, capitals=None, _clamp=0, _ignored_flags=None): ... + prec: int + rounding: str + Emin: int + Emax: int + capitals: int + if sys.version_info >= (3,): + clamp: int + else: + _clamp: int + traps: Dict[type, bool] + flags: Any + if sys.version_info >= (3,): + def __init__(self, prec: Optional[int] = ..., rounding: Optional[str] = ..., + Emin: Optional[int] = ..., Emax: Optional[int] = ..., + capitals: Optional[int] = ..., clamp: Optional[int] = ..., + flags=..., traps=..., + _ignored_flags=...) -> None: ... + else: + def __init__(self, prec: Optional[int] = ..., rounding: Optional[str] = ..., + traps=..., flags=..., + Emin: Optional[int] = ..., Emax: Optional[int] = ..., + capitals: Optional[int] = ..., _clamp: Optional[int] = ..., + _ignored_flags=...) -> None: ... + if sys.version_info >= (3,): + def __delattr__(self, name): ... + def __reduce__(self): ... def clear_flags(self): ... - def copy(self): ... - __copy__ = ... # type: Any - __hash__ = ... # type: Any + if sys.version_info >= (3,): + def clear_traps(self): ... + def copy(self) -> Context: ... + def __copy__(self) -> Context: ... + __hash__: Any = ... def Etiny(self): ... def Etop(self): ... def create_decimal(self, num=...): ... def create_decimal_from_float(self, f): ... - def abs(self, a): ... - def add(self, a, b): ... + def abs(self, a: _Decimal) -> Decimal: ... + def add(self, a: _Decimal, b: _Decimal) -> Decimal: ... def canonical(self, a): ... def compare(self, a, b): ... def compare_signal(self, a, b): ... @@ -223,7 +281,7 @@ class Context: def normalize(self, a): ... def number_class(self, a): ... def plus(self, a): ... - def power(self, a, b, modulo=None): ... + def power(self, a: _Decimal, b: _Decimal, modulo: Optional[_Decimal] = ...) -> Decimal: ... def quantize(self, a, b): ... def radix(self): ... def remainder(self, a, b): ... @@ -240,6 +298,6 @@ class Context: def to_integral_value(self, a): ... def to_integral(self, a): ... -DefaultContext = ... # type: Context -BasicContext = ... # type: Context -ExtendedContext = ... # type: Context +DefaultContext: Context +BasicContext: Context +ExtendedContext: Context diff --git a/stdlib/3/decimal.pyi b/stdlib/3/decimal.pyi deleted file mode 100644 index e77328854..000000000 --- a/stdlib/3/decimal.pyi +++ /dev/null @@ -1,255 +0,0 @@ -# Stubs for decimal (Python 3.4) - -from typing import ( - Any, Union, SupportsInt, SupportsFloat, SupportsAbs, SupportsRound, Sequence, - Tuple, NamedTuple, Dict -) - -_Decimal = Union[Decimal, int] -_ComparableNum = Union[Decimal, int, float] - -BasicContext = ... # type: Context -DefaultContext = ... # type: Context -ExtendedContext = ... # type: Context -HAVE_THREADS = ... # type: bool -MAX_EMAX = ... # type: int -MAX_PREC = ... # type: int -MIN_EMIN = ... # type: int -MIN_ETINY = ... # type: int -ROUND_05UP = ... # type: str -ROUND_CEILING = ... # type: str -ROUND_DOWN = ... # type: str -ROUND_FLOOR = ... # type: str -ROUND_HALF_DOWN = ... # type: str -ROUND_HALF_EVEN = ... # type: str -ROUND_HALF_UP = ... # type: str -ROUND_UP = ... # type: str - -def getcontext() -> Context: ... -def localcontext(ctx: Context = ...) -> _ContextManager: ... -def setcontext(c: Context) -> None: ... - -DecimalTuple = NamedTuple('DecimalTuple', - [('sign', int), - ('digits', Sequence[int]), # TODO: Use Tuple[int, ...] - ('exponent', int)]) - -class _ContextManager: - def __enter__(self) -> Context: ... - def __exit__(self, t, v, tb) -> None: ... - -class Context: - Emax = ... # type: int - Emin = ... # type: int - capitals = ... # type: int - clamp = ... # type: int - prec = ... # type: int - rounding = ... # type: str - traps = ... # type: Dict[type, bool] - def __init__(self, prec: int = ..., rounding: str = ..., Emin: int = ..., Emax: int = ..., - capitals: int = ..., clamp: int = ..., flags=..., traps=..., - _ignored_flags=...) -> None: ... - def Etiny(self): ... - def Etop(self): ... - def abs(self, x: _Decimal) -> Decimal: ... - def add(self, x: _Decimal, y: _Decimal) -> Decimal: ... - def canonical(self, x): ... - def clear_flags(self): ... - def clear_traps(self): ... - def compare(self, x, y): ... - def compare_signal(self, x, y): ... - def compare_total(self, x, y): ... - def compare_total_mag(self, x, y): ... - def copy(self): ... - def copy_abs(self, x): ... - def copy_decimal(self, x): ... - def copy_negate(self, x): ... - def copy_sign(self, x, y): ... - def create_decimal(self, x): ... - def create_decimal_from_float(self, f): ... - def divide(self, x, y): ... - def divide_int(self, x, y): ... - def divmod(self, x, y): ... - def exp(self, x): ... - def fma(self, x, y, z): ... - def is_canonical(self, x): ... - def is_finite(self, x): ... - def is_infinite(self, x): ... - def is_nan(self, x): ... - def is_normal(self, x): ... - def is_qnan(self, x): ... - def is_signed(self, x): ... - def is_snan(self): ... - def is_subnormal(self, x): ... - def is_zero(self, x): ... - def ln(self, x): ... - def log10(self, x): ... - def logb(self, x): ... - def logical_and(self, x, y): ... - def logical_invert(self, x): ... - def logical_or(self, x, y): ... - def logical_xor(self, x, y): ... - def max(self, x, y): ... - def max_mag(self, x, y): ... - def min(self, x, y): ... - def min_mag(self, x, y): ... - def minus(self, x): ... - def multiply(self, x, y): ... - def next_minus(self, x): ... - def next_plus(self, x): ... - def next_toward(self, x): ... - def normalize(self, x): ... - def number_class(self, x): ... - def plus(self, x): ... - def power(self, x, y): ... - def quantize(self, x, y): ... - def radix(self): ... - def remainder(self, x, y): ... - def remainder_near(self, x, y): ... - def rotate(self, x, y): ... - def same_quantum(self, x, y): ... - def scaleb(self, x, y): ... - def shift(self, x, y): ... - def sqrt(self, x): ... - def subtract(self, x, y): ... - def to_eng_string(self, x): ... - def to_integral(self, x): ... - def to_integral_exact(self, x): ... - def to_integral_value(self, x): ... - def to_sci_string(self, x): ... - def __copy__(self) -> Context: ... - def __delattr__(self, name): ... - def __reduce__(self): ... - -class ConversionSyntax(InvalidOperation): ... - -class Decimal(SupportsInt, SupportsFloat, SupportsAbs[Decimal], SupportsRound[int]): - # TODO: SupportsCeil, SupportsFloor, SupportsTrunc? - - def __init__(cls, value: Union[_Decimal, float, str, - Tuple[int, Sequence[int], int]] = ..., - context: Context = ...) -> None: ... - - @property - def imag(self) -> Decimal: ... - @property - def real(self) -> Decimal: ... - - def adjusted(self) -> int: ... - def as_tuple(self) -> DecimalTuple: ... - def canonical(self) -> Decimal: ... - def compare(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def compare_signal(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def compare_total(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def compare_total_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def conjugate(self) -> Decimal: ... - def copy_abs(self) -> Decimal: ... - def copy_negate(self) -> Decimal: ... - def copy_sign(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def exp(self, context: Context = ...) -> Decimal: ... - def fma(self, other: _Decimal, third: _Decimal, context: Context = ...) -> Decimal: ... - @classmethod - def from_float(cls, f: float) -> Decimal: ... - def is_canonical(self) -> bool: ... - def is_finite(self) -> bool: ... - def is_infinite(self) -> bool: ... - def is_nan(self) -> bool: ... - def is_normal(self, context: Context = ...) -> bool: ... - def is_qnan(self) -> bool: ... - def is_signed(self) -> bool: ... - def is_snan(self) -> bool: ... - def is_subnormal(self, context: Context = ...) -> bool: ... - def is_zero(self) -> bool: ... - def ln(self, context: Context = ...) -> Decimal: ... - def log10(self, context: Context = ...) -> Decimal: ... - def logb(self, context: Context = ...) -> Decimal: ... - def logical_and(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def logical_invert(self, context: Context = ...) -> Decimal: ... - def logical_or(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def logical_xor(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def max(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def max_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def min(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def min_mag(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def next_minus(self, context: Context = ...) -> Decimal: ... - def next_plus(self, context: Context = ...) -> Decimal: ... - def next_toward(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def normalize(self, context: Context = ...) -> Decimal: ... - def number_class(self, context: Context = ...) -> str: ... - def quantize(self, exp: _Decimal, rounding: str = ..., - context: Context = ...) -> Decimal: ... - def radix(self) -> Decimal: ... - def remainder_near(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def rotate(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def same_quantum(self, other: _Decimal, context: Context = ...) -> bool: ... - def scaleb(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def shift(self, other: _Decimal, context: Context = ...) -> Decimal: ... - def sqrt(self, context: Context = ...) -> Decimal: ... - def to_eng_string(self, context: Context = ...) -> str: ... - def to_integral(self, rounding: str = ..., context: Context = ...) -> Decimal: ... - def to_integral_exact(self, rounding: str = ..., context: Context = ...) -> Decimal: ... - def to_integral_value(self, rounding: str = ..., context: Context = ...) -> Decimal: ... - def __abs__(self) -> Decimal: ... - def __add__(self, other: _Decimal) -> Decimal: ... - def __bool__(self) -> bool: ... - def __ceil__(self) -> int: ... - def __complex__(self) -> complex: ... - def __copy__(self) -> Decimal: ... - def __deepcopy__(self) -> Decimal: ... - def __divmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ... - def __eq__(self, other: object) -> bool: ... - def __float__(self) -> float: ... - def __floor__(self) -> int: ... - def __floordiv__(self, other: _Decimal) -> Decimal: ... - def __format__(self, specifier, context=..., _localeconv=...) -> str: ... - def __ge__(self, other: _ComparableNum) -> bool: ... - def __gt__(self, other: _ComparableNum) -> bool: ... - def __hash__(self) -> int: ... - def __int__(self) -> int: ... - def __le__(self, other: _ComparableNum) -> bool: ... - def __lt__(self, other: _ComparableNum) -> bool: ... - def __mod__(self, other: _Decimal) -> Decimal: ... - def __mul__(self, other: _Decimal) -> Decimal: ... - def __ne__(self, other: object) -> bool: ... - def __neg__(self) -> Decimal: ... - def __pos__(self) -> Decimal: ... - def __pow__(self, other: _Decimal) -> Decimal: ... - def __radd__(self, other: int) -> Decimal: ... - def __rdivmod__(self, other: int) -> Tuple[Decimal, Decimal]: ... - def __reduce__(self): ... - def __rfloordiv__(self, other: int) -> Decimal: ... - def __rmod__(self, other: int) -> Decimal: ... - def __rmul__(self, other: int) -> Decimal: ... - def __round__(self, n=...) -> int: ... - def __rpow__(self, other: int) -> Decimal: ... - def __rsub__(self, other: int) -> Decimal: ... - def __rtruediv__(self, other: int) -> Decimal: ... - def __sub__(self, other: _Decimal) -> Decimal: ... - def __truediv__(self, other: _Decimal) -> Decimal: ... - def __trunc__(self) -> int: ... - -class DecimalException(ArithmeticError): ... - -class Clamped(DecimalException): ... - -class DivisionByZero(DecimalException, ZeroDivisionError): ... - -class DivisionImpossible(InvalidOperation): ... - -class DivisionUndefined(InvalidOperation, ZeroDivisionError): ... - -class FloatOperation(DecimalException, TypeError): ... - -class Inexact(DecimalException): ... - -class InvalidContext(InvalidOperation): ... - -class InvalidOperation(DecimalException): ... - -class Overflow(Inexact, Rounded): ... - -class Rounded(DecimalException): ... - -class Subnormal(DecimalException): ... - -class Underflow(Inexact, Rounded, Subnormal): ...