From 2f697fa8f6d415f5a75553adfc7626e0de4a4a00 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 13 Sep 2016 20:34:21 -0700 Subject: [PATCH 1/2] Add stubs for the statistics module --- stdlib/3.4/statistics.pyi | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 stdlib/3.4/statistics.pyi diff --git a/stdlib/3.4/statistics.pyi b/stdlib/3.4/statistics.pyi new file mode 100644 index 000000000000..e2830fb8b2bd --- /dev/null +++ b/stdlib/3.4/statistics.pyi @@ -0,0 +1,40 @@ +# Stubs for statistics +# See: http://docs.python.org/3/library/statistics.html + +import sys +from typing import Iterable, Iterator, Sequence, TypeVar, Union, overload +from decimal import Decimal +from fractions import Fraction + +# Note: according to the docs, statistics only explicitly supports +# int, float, Decimal, and Fraction. Other types within the numeric +# tower are not supported. It also states mixing together different +# types results in undefined behavior, so the type signatures below +# deliberately enforce that numeric types may not be mixed. +_TNum = TypeVar('_TNum', int, float, Decimal, Fraction) +_T = TypeVar('_T') + +def mean(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... +if sys.version_info >= (3, 6): + def geometric_mean(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... + def harmonic_mean(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... + +# Note: in CPython, the output of median_grouped may sometimes be coerced to +# float as an implementation detail. In the interests of not breaking code +# that relies on this implementation detail, the return type is set to the +# Union of float and the contained numeric type. +def median(data: Iterable[_TNum]) -> _TNum: ... +def median_low(data: Iterable[_TNum]) -> _TNum: ... +def median_high(data: Iterable[_TNum]) -> _TNum: ... +def median_grouped(data: Iterable[_TNum], + interval: Union[int, float, Fraction] = 1 + ) -> Union[_TNum, float]: ... + +def mode(data: Iterable[_T]) -> _T: ... + +def pstdev(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> Union[float, Decimal]: ... +def stdev(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> Union[float, Decimal]: ... +def pvariance(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... +def variance(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... + +class StatisticsError(ValueError): pass From 98161406c69d8f31f4b848dffb889c04a5e49098 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 13 Sep 2016 23:03:18 -0700 Subject: [PATCH 2/2] Temporarily remove references to Fraction Since typeshed currently does not have working stubs for the fractions module, this commit removes all references to Fraction within the statistics module so that this pull request can be evaluated and accepted independently of the other pull requests I've made recently to improve various numbers related modules. --- stdlib/3.4/statistics.pyi | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stdlib/3.4/statistics.pyi b/stdlib/3.4/statistics.pyi index e2830fb8b2bd..c4fab62364e4 100644 --- a/stdlib/3.4/statistics.pyi +++ b/stdlib/3.4/statistics.pyi @@ -4,14 +4,17 @@ import sys from typing import Iterable, Iterator, Sequence, TypeVar, Union, overload from decimal import Decimal -from fractions import Fraction # Note: according to the docs, statistics only explicitly supports # int, float, Decimal, and Fraction. Other types within the numeric # tower are not supported. It also states mixing together different # types results in undefined behavior, so the type signatures below # deliberately enforce that numeric types may not be mixed. -_TNum = TypeVar('_TNum', int, float, Decimal, Fraction) +# +# TODO: Once either https://github.com/python/typeshed/pull/545 or +# https://github.com/python/typeshed/pull/94 is accepted, add support +# for fractions. +_TNum = TypeVar('_TNum', int, float, Decimal) _T = TypeVar('_T') def mean(data: Union[Iterator[_TNum], Sequence[_TNum]]) -> _TNum: ... @@ -26,8 +29,9 @@ if sys.version_info >= (3, 6): def median(data: Iterable[_TNum]) -> _TNum: ... def median_low(data: Iterable[_TNum]) -> _TNum: ... def median_high(data: Iterable[_TNum]) -> _TNum: ... +# TODO: interval should also accept a Fraction. def median_grouped(data: Iterable[_TNum], - interval: Union[int, float, Fraction] = 1 + interval: Union[int, float] = 1 ) -> Union[_TNum, float]: ... def mode(data: Iterable[_T]) -> _T: ...