From f32978ee0b2407f2d7f29e146f577a7e69a40ffe Mon Sep 17 00:00:00 2001 From: Alan Du Date: Tue, 27 Feb 2018 15:00:48 -0500 Subject: [PATCH 1/5] Ignore F8111 flake8 error It conflicts with `@overload`s in type stubs --- setup.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index b83aea4..043bb69 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,7 +4,8 @@ # E305 expected 2 blank lines after class or function definition, found 1 # E701 multiple statements on one line (colon) # E704 multiple statements on one line (def) +# F811 redefinition of unused (not compatible with @overload) [flake8] -ignore = E301, E302, E305, E701, E704 +ignore = E301, E302, E305, E701, E704, F811 exclude = .git,__pycache__,tests/reveal From 20ddd20fc932305c2aa8c4820dee666bb4e04cdb Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 6 Jul 2018 18:14:57 -0400 Subject: [PATCH 2/5] Add generate framework --- generate/random.py | 328 ++++++++++++++++++++++++++++++++++++++++++ generate/utils.py | 44 ++++++ test-requirements.txt | 1 + 3 files changed, 373 insertions(+) create mode 100644 generate/random.py create mode 100644 generate/utils.py diff --git a/generate/random.py b/generate/random.py new file mode 100644 index 0000000..32a04f0 --- /dev/null +++ b/generate/random.py @@ -0,0 +1,328 @@ +import os +from typing import Iterator, List +import itertools + +from generate.utils import Arg, Func, SELF, STUB_DIR + +SIZE = Arg(name="size", ty="_Size", optional=False) +NOSIZE = Arg(name="size", ty="None", optional=True) + + +def distribution(name: str, return_type: str, *args: Arg) -> Iterator[Func]: + def func(return_type: str, args: List[Arg], size: bool) -> Func: + args = [SELF] + list(args) + if size: + args.append(SIZE) + else: + args.append(NOSIZE) + return Func(name, return_type, args, overload=True) + + yield func(return_type, args, size=False) + yield func( + "ndarray", + [Arg(arg.name, f"_ScalarOrArray[{arg.ty}]") for arg in args], + size=True, + ) + + for i in range(len(args)): + for combo in itertools.combinations(args, i + 1): + yield func( + "ndarray", + [ + Arg( + arg.name, + f"_ArrayLike[{arg.ty}]" if arg in combo else arg.ty, + optional=False, + ) + for arg in args + ], + size=False, + ) + + +functions = list( + distribution("beta", "float", Arg("a", "float"), Arg("b", "float")) +) +functions.append(Func("bytes", "builtins.bytes", [SELF, Arg("length", "int")])) +functions += list( + distribution("binomial", "int", Arg("n", "int"), Arg("p", "float")) +) +functions += list(distribution("chisquared", "float", Arg("df", "int"))) + + +def choice(return_type: str, ty: str, size: bool) -> Func: + return Func( + "choice", + return_type, + [ + SELF, + Arg("a", ty), + SIZE if size else NOSIZE, + Arg("replace", "bool", True), + Arg("p", "Optional[_ArrayLike[float]]", True), + ], + overload=True, + ) + + +functions += [ + choice("int", "int", False), + choice("_T", "Sequence[_T]", False), + choice("Any", "ndarray", False), + choice("ndarray", "Union[int, ndarray]", True), +] +functions.append( + Func( + "dirichlet", + "ndarray", + [ + SELF, + Arg("alpha", "_ArrayLike[float]"), + Arg("size", "Optional[_Size]", True), + ], + ) +) +functions += list( + distribution("exponential", "float", Arg("scale", "float", True)) +) +functions += list( + distribution("f", "float", Arg("dfnum", "float"), Arg("dfden", "float")) +) +functions += list( + distribution( + "gamma", "float", Arg("shape", "float"), Arg("scale", "float", True) + ) +) +functions += list(distribution("geometric", "float", Arg("p", "float"))) +functions.append( + Func("get_state", "Tuple[str, ndarray, int, int, float]", [SELF]) +) +functions += list( + distribution( + "gumbel", + "float", + Arg("loc", "float", True), + Arg("scale", "float", True), + ) +) +functions += list( + distribution( + "hypergeometric", + "int", + Arg("ngood", "int"), + Arg("nbad", "int"), + Arg("nsample", "int"), + ) +) +functions += list( + distribution( + "laplace", + "float", + Arg("loc", "float", True), + Arg("scale", "float", True), + ) +) +functions += list( + distribution( + "logistic", + "float", + Arg("loc", "float", True), + Arg("scale", "float", True), + ) +) +functions += list( + distribution( + "lognormal", + "float", + Arg("mean", "float", True), + Arg("sigma", "float", True), + ) +) +functions += list(distribution("logseries", "int", Arg("p", "float"))) +functions.append( + Func( + "multinomial", + "ndarray", + [SELF, Arg("n", "int"), Arg("size", "Optional[_Size]", True)], + ) +) +functions.append( + Func( + "multivariate_normal", + "ndarray", + [ + SELF, + Arg("mean", "ndarray"), + Arg("cov", "ndarray"), + Arg("size", "Optional[_Size]", True), + Arg("check_valid", "str", True), + Arg("tol", "float", True), + ], + ) +) +functions += list( + distribution( + "negative_binomial", "int", Arg("n", "int"), Arg("p", "float") + ) +) +functions += list( + distribution( + "noncentral_chisquare", + "float", + Arg("df", "float"), + Arg("nonc", "float"), + ) +) +functions += list( + distribution( + "noncentral_f", + "float", + Arg("dfnum", "float"), + Arg("dfden", "float"), + Arg("nonc", "float"), + ) +) +functions += list( + distribution( + "normal", + "float", + Arg("loc", "float", True), + Arg("scale", "float", True), + ) +) +functions += list(distribution("pareto", "float", Arg("a", "float"))) +functions.append( + Func("permutation", "ndarray", [SELF, Arg("x", "Union[int, ndarray]")]) +) +functions += list(distribution("poisson", "float", Arg("lam", "float", True))) +functions += list(distribution("power", "float", Arg("a", "float"))) +functions += [ + Func("rand", "float", [SELF], overload=True), + Func( + "rand", + "ndarray", + [SELF, Arg("d0", "int"), Arg("*dn", "int")], + overload=True, + ), +] +# TODO(alan): dtype parameter +functions += list( + distribution("randint", "int", Arg("low", "int"), Arg("high", "int", True)) +) +functions += [ + Func("randn", "float", [SELF], overload=True), + Func( + "randn", + "ndarray", + [SELF, Arg("d0", "int"), Arg("*dn", "int")], + overload=True, + ), +] +functions += list( + distribution( + "random_integers", "int", Arg("low", "int"), Arg("high", "int", True) + ) +) +functions += list(distribution("random_sample", "int")) +functions += list(distribution("rayleigh", "float", Arg("scale", "float"))) +functions += [ + Func( + "seed", + "None", + [SELF, Arg("seed", "Union[None, int, Tuple[int], List[int]]", True)], + ), + Func( + "set_state", + "None", + [SELF, Arg("state", "Tuple[str, ndarray, int, int, float]")], + ), + Func("shuffle", "None", [SELF, Arg("x", "_ArrayLike[Any]")]), +] +functions += list(distribution("standard_cauchy", "float")) +functions += list(distribution("standard_exponential", "float")) +functions += list(distribution("standard_gamma", "float")) +functions += list(distribution("standard_normal", "float")) +functions += list(distribution("standard_t", "float", Arg("t", "int"))) +functions += list(distribution("tomaxint", "int", Arg("t", "int"))) +functions += list( + distribution( + "triangular", + "float", + Arg("left", "float"), + Arg("mode", "float"), + Arg("right", "float"), + ) +) +functions += list( + distribution( + "uniform", + "float", + Arg("low", "float", True), + Arg("high", "float", True), + ) +) +functions += list( + distribution( + "vonmises", "float", Arg("mu", "float"), Arg("kappa", "float") + ) +) +functions += list( + distribution("wald", "float", Arg("mean", "float"), Arg("scale", "float")) +) +functions += list(distribution("weibull", "float", Arg("a", "float"))) +functions += list(distribution("zipf", "int", Arg("a", "float"))) + + +imports = """\ +import builtins +from typing import ( + Any, List, overload, Optional, Sequence, Tuple, TypeVar, Union +) +from numpy import ndarray +""" + +typevars = """\ +_Size = Union[int, Sequence[int]] +_T = TypeVar("_T") +_ArrayLike = Union[Sequence[_T], ndarray] +_ScalarOrArray = Union[_T, Sequence[_T], ndarray] +""" + +with open(os.path.join(STUB_DIR, "random", "mtrand.pyi"), "w") as fout: + fout.write(imports) + fout.write(typevars) + fout.write( + """ +class RandomState: + def __init__( + self, state: Union[None, int, List[int], Tuple[int]] = ... + ) -> None: ... +""" + ) + prev = None + for func in functions: + if func.name != prev: + fout.write("\n") + prev = func.name + fout.write(func.render(indent=4)) + +with open(os.path.join(STUB_DIR, "random", "__init__.pyi"), "w") as fout: + fout.write(imports) + fout.write("from . import mtrand\n") + fout.write(typevars) + fout.write( + """\ +RandomState = mtrand.RandomState +""" + ) + + prev = None + for func in functions: + if func.name != prev: + fout.write("\n") + prev = func.name + + # Leave out first argument (SELF) + assert func.args[0] == SELF + func = Func(func.name, func.return_type, func.args[1:], func.overload) + fout.write(func.render()) diff --git a/generate/utils.py b/generate/utils.py new file mode 100644 index 0000000..9f7bd72 --- /dev/null +++ b/generate/utils.py @@ -0,0 +1,44 @@ +import os + +from dataclasses import dataclass, field +from typing import List, Optional + +import textwrap + +STUB_DIR = os.path.abspath(os.path.join(__file__, "..", "..", "numpy-stubs")) + + +@dataclass(frozen=True) +class Arg: + name: str + ty: Optional[str] = None + optional: bool = False + + def render(self) -> str: + s = self.name + if self.ty is not None: + s += f": {self.ty}" + if self.optional: + s += " = ..." + return s + + +@dataclass(frozen=True) +class Func: + name: str + return_type: str + + args: List[Arg] = field(default_factory=list) + overload: bool = False + + def render(self, indent=0) -> str: + s = f"def {self.name}(" + s += ", ".join(arg.render() for arg in self.args) + s += f") -> {self.return_type}: ...\n" + + if self.overload: + s = "@overload\n" + s + return textwrap.indent(s, " " * indent) + + +SELF = Arg("self") diff --git a/test-requirements.txt b/test-requirements.txt index 512c913..8aba38f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ +dataclasses==0.6 flake8==3.3.0 flake8-pyi==17.3.0 pytest==3.4.2 From 0bf5f84be5e1457149fa3fe5e3f3cc4c70288df5 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 6 Jul 2018 18:25:43 -0400 Subject: [PATCH 3/5] Generate random typestubs --- numpy-stubs/__init__.pyi | 4 +- numpy-stubs/random/__init__.pyi | 603 ++++++++++++++++++++++++++ numpy-stubs/random/mtrand.pyi | 739 ++++++++++++++++++++++++++++++++ 3 files changed, 1345 insertions(+), 1 deletion(-) create mode 100644 numpy-stubs/random/__init__.pyi create mode 100644 numpy-stubs/random/mtrand.pyi diff --git a/numpy-stubs/__init__.pyi b/numpy-stubs/__init__.pyi index eb9f21b..8ea7b5f 100644 --- a/numpy-stubs/__init__.pyi +++ b/numpy-stubs/__init__.pyi @@ -1,7 +1,6 @@ import builtins import sys -from numpy.core._internal import _ctypes from typing import ( Any, Container, @@ -23,6 +22,9 @@ from typing import ( Union, ) +from numpy.core._internal import _ctypes +from . import random # noqa + if sys.version_info[0] < 3: class SupportsBytes: ... else: diff --git a/numpy-stubs/random/__init__.pyi b/numpy-stubs/random/__init__.pyi new file mode 100644 index 0000000..986e4a8 --- /dev/null +++ b/numpy-stubs/random/__init__.pyi @@ -0,0 +1,603 @@ +import builtins +from typing import ( + Any, + List, + overload, + Optional, + Sequence, + Tuple, + TypeVar, + Union, +) +from numpy import ndarray +from . import mtrand + +_Size = Union[int, Sequence[int]] +_T = TypeVar("_T") +_ArrayLike = Union[Sequence[_T], ndarray] +_ScalarOrArray = Union[_T, Sequence[_T], ndarray] +RandomState = mtrand.RandomState +@overload +def beta(a: float, b: float, size: None = ...) -> float: ... +@overload +def beta( + a: _ScalarOrArray[float], b: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def beta(a: _ArrayLike[float], b: float, size: None = ...) -> ndarray: ... +@overload +def beta(a: float, b: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def beta( + a: _ArrayLike[float], b: _ArrayLike[float], size: None = ... +) -> ndarray: ... +def bytes(length: int) -> builtins.bytes: ... +@overload +def binomial(n: int, p: float, size: None = ...) -> int: ... +@overload +def binomial( + n: _ScalarOrArray[int], p: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def binomial(n: _ArrayLike[int], p: float, size: None = ...) -> ndarray: ... +@overload +def binomial(n: int, p: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def binomial( + n: _ArrayLike[int], p: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def chisquared(df: int, size: None = ...) -> float: ... +@overload +def chisquared(df: _ScalarOrArray[int], size: _Size) -> ndarray: ... +@overload +def chisquared(df: _ArrayLike[int], size: None = ...) -> ndarray: ... +@overload +def choice( + a: int, + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., +) -> int: ... +@overload +def choice( + a: Sequence[_T], + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., +) -> _T: ... +@overload +def choice( + a: ndarray, + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., +) -> Any: ... +@overload +def choice( + a: Union[int, ndarray], + size: _Size, + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., +) -> ndarray: ... +def dirichlet( + alpha: _ArrayLike[float], size: Optional[_Size] = ... +) -> ndarray: ... +@overload +def exponential(scale: float = ..., size: None = ...) -> float: ... +@overload +def exponential(scale: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def exponential(scale: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def f(dfnum: float, dfden: float, size: None = ...) -> float: ... +@overload +def f( + dfnum: _ScalarOrArray[float], dfden: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def f(dfnum: _ArrayLike[float], dfden: float, size: None = ...) -> ndarray: ... +@overload +def f(dfnum: float, dfden: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def f( + dfnum: _ArrayLike[float], dfden: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def gamma(shape: float, scale: float = ..., size: None = ...) -> float: ... +@overload +def gamma( + shape: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def gamma( + shape: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def gamma( + shape: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def gamma( + shape: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def geometric(p: float, size: None = ...) -> float: ... +@overload +def geometric(p: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def geometric(p: _ArrayLike[float], size: None = ...) -> ndarray: ... +def get_state() -> Tuple[str, ndarray, int, int, float]: ... +@overload +def gumbel( + loc: float = ..., scale: float = ..., size: None = ... +) -> float: ... +@overload +def gumbel( + loc: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def gumbel( + loc: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def gumbel( + loc: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def gumbel( + loc: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def hypergeometric( + ngood: int, nbad: int, nsample: int, size: None = ... +) -> int: ... +@overload +def hypergeometric( + ngood: _ScalarOrArray[int], + nbad: _ScalarOrArray[int], + nsample: _ScalarOrArray[int], + size: _Size, +) -> ndarray: ... +@overload +def hypergeometric( + ngood: _ArrayLike[int], nbad: int, nsample: int, size: None = ... +) -> ndarray: ... +@overload +def hypergeometric( + ngood: int, nbad: _ArrayLike[int], nsample: int, size: None = ... +) -> ndarray: ... +@overload +def hypergeometric( + ngood: int, nbad: int, nsample: _ArrayLike[int], size: None = ... +) -> ndarray: ... +@overload +def hypergeometric( + ngood: _ArrayLike[int], + nbad: _ArrayLike[int], + nsample: int, + size: None = ..., +) -> ndarray: ... +@overload +def hypergeometric( + ngood: _ArrayLike[int], + nbad: int, + nsample: _ArrayLike[int], + size: None = ..., +) -> ndarray: ... +@overload +def hypergeometric( + ngood: int, + nbad: _ArrayLike[int], + nsample: _ArrayLike[int], + size: None = ..., +) -> ndarray: ... +@overload +def hypergeometric( + ngood: _ArrayLike[int], + nbad: _ArrayLike[int], + nsample: _ArrayLike[int], + size: None = ..., +) -> ndarray: ... +@overload +def laplace( + loc: float = ..., scale: float = ..., size: None = ... +) -> float: ... +@overload +def laplace( + loc: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def laplace( + loc: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def laplace( + loc: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def laplace( + loc: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def logistic( + loc: float = ..., scale: float = ..., size: None = ... +) -> float: ... +@overload +def logistic( + loc: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def logistic( + loc: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def logistic( + loc: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def logistic( + loc: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def lognormal( + mean: float = ..., sigma: float = ..., size: None = ... +) -> float: ... +@overload +def lognormal( + mean: _ScalarOrArray[float], sigma: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def lognormal( + mean: _ArrayLike[float], sigma: float, size: None = ... +) -> ndarray: ... +@overload +def lognormal( + mean: float, sigma: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def lognormal( + mean: _ArrayLike[float], sigma: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def logseries(p: float, size: None = ...) -> int: ... +@overload +def logseries(p: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def logseries(p: _ArrayLike[float], size: None = ...) -> ndarray: ... +def multinomial(n: int, size: Optional[_Size] = ...) -> ndarray: ... +def multivariate_normal( + mean: ndarray, + cov: ndarray, + size: Optional[_Size] = ..., + check_valid: str = ..., + tol: float = ..., +) -> ndarray: ... +@overload +def negative_binomial(n: int, p: float, size: None = ...) -> int: ... +@overload +def negative_binomial( + n: _ScalarOrArray[int], p: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def negative_binomial( + n: _ArrayLike[int], p: float, size: None = ... +) -> ndarray: ... +@overload +def negative_binomial( + n: int, p: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def negative_binomial( + n: _ArrayLike[int], p: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def noncentral_chisquare( + df: float, nonc: float, size: None = ... +) -> float: ... +@overload +def noncentral_chisquare( + df: _ScalarOrArray[float], nonc: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def noncentral_chisquare( + df: _ArrayLike[float], nonc: float, size: None = ... +) -> ndarray: ... +@overload +def noncentral_chisquare( + df: float, nonc: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def noncentral_chisquare( + df: _ArrayLike[float], nonc: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: float, dfden: float, nonc: float, size: None = ... +) -> float: ... +@overload +def noncentral_f( + dfnum: _ScalarOrArray[float], + dfden: _ScalarOrArray[float], + nonc: _ScalarOrArray[float], + size: _Size, +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: _ArrayLike[float], dfden: float, nonc: float, size: None = ... +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: float, dfden: _ArrayLike[float], nonc: float, size: None = ... +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: float, dfden: float, nonc: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: _ArrayLike[float], + dfden: _ArrayLike[float], + nonc: float, + size: None = ..., +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: _ArrayLike[float], + dfden: float, + nonc: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: float, + dfden: _ArrayLike[float], + nonc: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def noncentral_f( + dfnum: _ArrayLike[float], + dfden: _ArrayLike[float], + nonc: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def normal( + loc: float = ..., scale: float = ..., size: None = ... +) -> float: ... +@overload +def normal( + loc: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def normal( + loc: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def normal( + loc: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def normal( + loc: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def pareto(a: float, size: None = ...) -> float: ... +@overload +def pareto(a: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def pareto(a: _ArrayLike[float], size: None = ...) -> ndarray: ... +def permutation(x: Union[int, ndarray]) -> ndarray: ... +@overload +def poisson(lam: float = ..., size: None = ...) -> float: ... +@overload +def poisson(lam: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def poisson(lam: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def power(a: float, size: None = ...) -> float: ... +@overload +def power(a: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def power(a: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def rand() -> float: ... +@overload +def rand(d0: int, *dn: int) -> ndarray: ... +@overload +def randint(low: int, high: int = ..., size: None = ...) -> int: ... +@overload +def randint( + low: _ScalarOrArray[int], high: _ScalarOrArray[int], size: _Size +) -> ndarray: ... +@overload +def randint(low: _ArrayLike[int], high: int, size: None = ...) -> ndarray: ... +@overload +def randint(low: int, high: _ArrayLike[int], size: None = ...) -> ndarray: ... +@overload +def randint( + low: _ArrayLike[int], high: _ArrayLike[int], size: None = ... +) -> ndarray: ... +@overload +def randn() -> float: ... +@overload +def randn(d0: int, *dn: int) -> ndarray: ... +@overload +def random_integers(low: int, high: int = ..., size: None = ...) -> int: ... +@overload +def random_integers( + low: _ScalarOrArray[int], high: _ScalarOrArray[int], size: _Size +) -> ndarray: ... +@overload +def random_integers( + low: _ArrayLike[int], high: int, size: None = ... +) -> ndarray: ... +@overload +def random_integers( + low: int, high: _ArrayLike[int], size: None = ... +) -> ndarray: ... +@overload +def random_integers( + low: _ArrayLike[int], high: _ArrayLike[int], size: None = ... +) -> ndarray: ... +@overload +def random_sample(size: None = ...) -> int: ... +@overload +def random_sample(size: _Size) -> ndarray: ... +@overload +def rayleigh(scale: float, size: None = ...) -> float: ... +@overload +def rayleigh(scale: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def rayleigh(scale: _ArrayLike[float], size: None = ...) -> ndarray: ... +def seed(seed: Union[None, int, Tuple[int], List[int]] = ...) -> None: ... +def set_state(state: Tuple[str, ndarray, int, int, float]) -> None: ... +def shuffle(x: _ArrayLike[Any]) -> None: ... +@overload +def standard_cauchy(size: None = ...) -> float: ... +@overload +def standard_cauchy(size: _Size) -> ndarray: ... +@overload +def standard_exponential(size: None = ...) -> float: ... +@overload +def standard_exponential(size: _Size) -> ndarray: ... +@overload +def standard_gamma(size: None = ...) -> float: ... +@overload +def standard_gamma(size: _Size) -> ndarray: ... +@overload +def standard_normal(size: None = ...) -> float: ... +@overload +def standard_normal(size: _Size) -> ndarray: ... +@overload +def standard_t(t: int, size: None = ...) -> float: ... +@overload +def standard_t(t: _ScalarOrArray[int], size: _Size) -> ndarray: ... +@overload +def standard_t(t: _ArrayLike[int], size: None = ...) -> ndarray: ... +@overload +def tomaxint(t: int, size: None = ...) -> int: ... +@overload +def tomaxint(t: _ScalarOrArray[int], size: _Size) -> ndarray: ... +@overload +def tomaxint(t: _ArrayLike[int], size: None = ...) -> ndarray: ... +@overload +def triangular( + left: float, mode: float, right: float, size: None = ... +) -> float: ... +@overload +def triangular( + left: _ScalarOrArray[float], + mode: _ScalarOrArray[float], + right: _ScalarOrArray[float], + size: _Size, +) -> ndarray: ... +@overload +def triangular( + left: _ArrayLike[float], mode: float, right: float, size: None = ... +) -> ndarray: ... +@overload +def triangular( + left: float, mode: _ArrayLike[float], right: float, size: None = ... +) -> ndarray: ... +@overload +def triangular( + left: float, mode: float, right: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def triangular( + left: _ArrayLike[float], + mode: _ArrayLike[float], + right: float, + size: None = ..., +) -> ndarray: ... +@overload +def triangular( + left: _ArrayLike[float], + mode: float, + right: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def triangular( + left: float, + mode: _ArrayLike[float], + right: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def triangular( + left: _ArrayLike[float], + mode: _ArrayLike[float], + right: _ArrayLike[float], + size: None = ..., +) -> ndarray: ... +@overload +def uniform( + low: float = ..., high: float = ..., size: None = ... +) -> float: ... +@overload +def uniform( + low: _ScalarOrArray[float], high: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def uniform( + low: _ArrayLike[float], high: float, size: None = ... +) -> ndarray: ... +@overload +def uniform( + low: float, high: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def uniform( + low: _ArrayLike[float], high: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def vonmises(mu: float, kappa: float, size: None = ...) -> float: ... +@overload +def vonmises( + mu: _ScalarOrArray[float], kappa: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def vonmises( + mu: _ArrayLike[float], kappa: float, size: None = ... +) -> ndarray: ... +@overload +def vonmises( + mu: float, kappa: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def vonmises( + mu: _ArrayLike[float], kappa: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def wald(mean: float, scale: float, size: None = ...) -> float: ... +@overload +def wald( + mean: _ScalarOrArray[float], scale: _ScalarOrArray[float], size: _Size +) -> ndarray: ... +@overload +def wald( + mean: _ArrayLike[float], scale: float, size: None = ... +) -> ndarray: ... +@overload +def wald( + mean: float, scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def wald( + mean: _ArrayLike[float], scale: _ArrayLike[float], size: None = ... +) -> ndarray: ... +@overload +def weibull(a: float, size: None = ...) -> float: ... +@overload +def weibull(a: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def weibull(a: _ArrayLike[float], size: None = ...) -> ndarray: ... +@overload +def zipf(a: float, size: None = ...) -> int: ... +@overload +def zipf(a: _ScalarOrArray[float], size: _Size) -> ndarray: ... +@overload +def zipf(a: _ArrayLike[float], size: None = ...) -> ndarray: ... diff --git a/numpy-stubs/random/mtrand.pyi b/numpy-stubs/random/mtrand.pyi new file mode 100644 index 0000000..10d78d7 --- /dev/null +++ b/numpy-stubs/random/mtrand.pyi @@ -0,0 +1,739 @@ +import builtins +from typing import ( + Any, + List, + overload, + Optional, + Sequence, + Tuple, + TypeVar, + Union, +) +from numpy import ndarray + +_Size = Union[int, Sequence[int]] +_T = TypeVar("_T") +_ArrayLike = Union[Sequence[_T], ndarray] +_ScalarOrArray = Union[_T, Sequence[_T], ndarray] + +class RandomState: + def __init__( + self, state: Union[None, int, List[int], Tuple[int]] = ... + ) -> None: ... + @overload + def beta(self, a: float, b: float, size: None = ...) -> float: ... + @overload + def beta( + self, a: _ScalarOrArray[float], b: _ScalarOrArray[float], size: _Size + ) -> ndarray: ... + @overload + def beta( + self, a: _ArrayLike[float], b: float, size: None = ... + ) -> ndarray: ... + @overload + def beta( + self, a: float, b: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def beta( + self, a: _ArrayLike[float], b: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + def bytes(self, length: int) -> builtins.bytes: ... + @overload + def binomial(self, n: int, p: float, size: None = ...) -> int: ... + @overload + def binomial( + self, n: _ScalarOrArray[int], p: _ScalarOrArray[float], size: _Size + ) -> ndarray: ... + @overload + def binomial( + self, n: _ArrayLike[int], p: float, size: None = ... + ) -> ndarray: ... + @overload + def binomial( + self, n: int, p: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def binomial( + self, n: _ArrayLike[int], p: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def chisquared(self, df: int, size: None = ...) -> float: ... + @overload + def chisquared(self, df: _ScalarOrArray[int], size: _Size) -> ndarray: ... + @overload + def chisquared(self, df: _ArrayLike[int], size: None = ...) -> ndarray: ... + @overload + def choice( + self, + a: int, + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., + ) -> int: ... + @overload + def choice( + self, + a: Sequence[_T], + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., + ) -> _T: ... + @overload + def choice( + self, + a: ndarray, + size: None = ..., + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., + ) -> Any: ... + @overload + def choice( + self, + a: Union[int, ndarray], + size: _Size, + replace: bool = ..., + p: Optional[_ArrayLike[float]] = ..., + ) -> ndarray: ... + def dirichlet( + self, alpha: _ArrayLike[float], size: Optional[_Size] = ... + ) -> ndarray: ... + @overload + def exponential(self, scale: float = ..., size: None = ...) -> float: ... + @overload + def exponential( + self, scale: _ScalarOrArray[float], size: _Size + ) -> ndarray: ... + @overload + def exponential( + self, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def f(self, dfnum: float, dfden: float, size: None = ...) -> float: ... + @overload + def f( + self, + dfnum: _ScalarOrArray[float], + dfden: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def f( + self, dfnum: _ArrayLike[float], dfden: float, size: None = ... + ) -> ndarray: ... + @overload + def f( + self, dfnum: float, dfden: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def f( + self, + dfnum: _ArrayLike[float], + dfden: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def gamma( + self, shape: float, scale: float = ..., size: None = ... + ) -> float: ... + @overload + def gamma( + self, + shape: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def gamma( + self, shape: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def gamma( + self, shape: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def gamma( + self, + shape: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def geometric(self, p: float, size: None = ...) -> float: ... + @overload + def geometric(self, p: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def geometric(self, p: _ArrayLike[float], size: None = ...) -> ndarray: ... + def get_state(self) -> Tuple[str, ndarray, int, int, float]: ... + @overload + def gumbel( + self, loc: float = ..., scale: float = ..., size: None = ... + ) -> float: ... + @overload + def gumbel( + self, + loc: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def gumbel( + self, loc: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def gumbel( + self, loc: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def gumbel( + self, + loc: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def hypergeometric( + self, ngood: int, nbad: int, nsample: int, size: None = ... + ) -> int: ... + @overload + def hypergeometric( + self, + ngood: _ScalarOrArray[int], + nbad: _ScalarOrArray[int], + nsample: _ScalarOrArray[int], + size: _Size, + ) -> ndarray: ... + @overload + def hypergeometric( + self, ngood: _ArrayLike[int], nbad: int, nsample: int, size: None = ... + ) -> ndarray: ... + @overload + def hypergeometric( + self, ngood: int, nbad: _ArrayLike[int], nsample: int, size: None = ... + ) -> ndarray: ... + @overload + def hypergeometric( + self, ngood: int, nbad: int, nsample: _ArrayLike[int], size: None = ... + ) -> ndarray: ... + @overload + def hypergeometric( + self, + ngood: _ArrayLike[int], + nbad: _ArrayLike[int], + nsample: int, + size: None = ..., + ) -> ndarray: ... + @overload + def hypergeometric( + self, + ngood: _ArrayLike[int], + nbad: int, + nsample: _ArrayLike[int], + size: None = ..., + ) -> ndarray: ... + @overload + def hypergeometric( + self, + ngood: int, + nbad: _ArrayLike[int], + nsample: _ArrayLike[int], + size: None = ..., + ) -> ndarray: ... + @overload + def hypergeometric( + self, + ngood: _ArrayLike[int], + nbad: _ArrayLike[int], + nsample: _ArrayLike[int], + size: None = ..., + ) -> ndarray: ... + @overload + def laplace( + self, loc: float = ..., scale: float = ..., size: None = ... + ) -> float: ... + @overload + def laplace( + self, + loc: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def laplace( + self, loc: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def laplace( + self, loc: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def laplace( + self, + loc: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def logistic( + self, loc: float = ..., scale: float = ..., size: None = ... + ) -> float: ... + @overload + def logistic( + self, + loc: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def logistic( + self, loc: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def logistic( + self, loc: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def logistic( + self, + loc: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def lognormal( + self, mean: float = ..., sigma: float = ..., size: None = ... + ) -> float: ... + @overload + def lognormal( + self, + mean: _ScalarOrArray[float], + sigma: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def lognormal( + self, mean: _ArrayLike[float], sigma: float, size: None = ... + ) -> ndarray: ... + @overload + def lognormal( + self, mean: float, sigma: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def lognormal( + self, + mean: _ArrayLike[float], + sigma: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def logseries(self, p: float, size: None = ...) -> int: ... + @overload + def logseries(self, p: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def logseries(self, p: _ArrayLike[float], size: None = ...) -> ndarray: ... + def multinomial(self, n: int, size: Optional[_Size] = ...) -> ndarray: ... + def multivariate_normal( + self, + mean: ndarray, + cov: ndarray, + size: Optional[_Size] = ..., + check_valid: str = ..., + tol: float = ..., + ) -> ndarray: ... + @overload + def negative_binomial(self, n: int, p: float, size: None = ...) -> int: ... + @overload + def negative_binomial( + self, n: _ScalarOrArray[int], p: _ScalarOrArray[float], size: _Size + ) -> ndarray: ... + @overload + def negative_binomial( + self, n: _ArrayLike[int], p: float, size: None = ... + ) -> ndarray: ... + @overload + def negative_binomial( + self, n: int, p: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def negative_binomial( + self, n: _ArrayLike[int], p: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def noncentral_chisquare( + self, df: float, nonc: float, size: None = ... + ) -> float: ... + @overload + def noncentral_chisquare( + self, + df: _ScalarOrArray[float], + nonc: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def noncentral_chisquare( + self, df: _ArrayLike[float], nonc: float, size: None = ... + ) -> ndarray: ... + @overload + def noncentral_chisquare( + self, df: float, nonc: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def noncentral_chisquare( + self, df: _ArrayLike[float], nonc: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def noncentral_f( + self, dfnum: float, dfden: float, nonc: float, size: None = ... + ) -> float: ... + @overload + def noncentral_f( + self, + dfnum: _ScalarOrArray[float], + dfden: _ScalarOrArray[float], + nonc: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: _ArrayLike[float], + dfden: float, + nonc: float, + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: float, + dfden: _ArrayLike[float], + nonc: float, + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: float, + dfden: float, + nonc: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: _ArrayLike[float], + dfden: _ArrayLike[float], + nonc: float, + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: _ArrayLike[float], + dfden: float, + nonc: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: float, + dfden: _ArrayLike[float], + nonc: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def noncentral_f( + self, + dfnum: _ArrayLike[float], + dfden: _ArrayLike[float], + nonc: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def normal( + self, loc: float = ..., scale: float = ..., size: None = ... + ) -> float: ... + @overload + def normal( + self, + loc: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def normal( + self, loc: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def normal( + self, loc: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def normal( + self, + loc: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def pareto(self, a: float, size: None = ...) -> float: ... + @overload + def pareto(self, a: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def pareto(self, a: _ArrayLike[float], size: None = ...) -> ndarray: ... + def permutation(self, x: Union[int, ndarray]) -> ndarray: ... + @overload + def poisson(self, lam: float = ..., size: None = ...) -> float: ... + @overload + def poisson(self, lam: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def poisson(self, lam: _ArrayLike[float], size: None = ...) -> ndarray: ... + @overload + def power(self, a: float, size: None = ...) -> float: ... + @overload + def power(self, a: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def power(self, a: _ArrayLike[float], size: None = ...) -> ndarray: ... + @overload + def rand(self) -> float: ... + @overload + def rand(self, d0: int, *dn: int) -> ndarray: ... + @overload + def randint(self, low: int, high: int = ..., size: None = ...) -> int: ... + @overload + def randint( + self, low: _ScalarOrArray[int], high: _ScalarOrArray[int], size: _Size + ) -> ndarray: ... + @overload + def randint( + self, low: _ArrayLike[int], high: int, size: None = ... + ) -> ndarray: ... + @overload + def randint( + self, low: int, high: _ArrayLike[int], size: None = ... + ) -> ndarray: ... + @overload + def randint( + self, low: _ArrayLike[int], high: _ArrayLike[int], size: None = ... + ) -> ndarray: ... + @overload + def randn(self) -> float: ... + @overload + def randn(self, d0: int, *dn: int) -> ndarray: ... + @overload + def random_integers( + self, low: int, high: int = ..., size: None = ... + ) -> int: ... + @overload + def random_integers( + self, low: _ScalarOrArray[int], high: _ScalarOrArray[int], size: _Size + ) -> ndarray: ... + @overload + def random_integers( + self, low: _ArrayLike[int], high: int, size: None = ... + ) -> ndarray: ... + @overload + def random_integers( + self, low: int, high: _ArrayLike[int], size: None = ... + ) -> ndarray: ... + @overload + def random_integers( + self, low: _ArrayLike[int], high: _ArrayLike[int], size: None = ... + ) -> ndarray: ... + @overload + def random_sample(self, size: None = ...) -> int: ... + @overload + def random_sample(self, size: _Size) -> ndarray: ... + @overload + def rayleigh(self, scale: float, size: None = ...) -> float: ... + @overload + def rayleigh( + self, scale: _ScalarOrArray[float], size: _Size + ) -> ndarray: ... + @overload + def rayleigh( + self, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + def seed( + self, seed: Union[None, int, Tuple[int], List[int]] = ... + ) -> None: ... + def set_state( + self, state: Tuple[str, ndarray, int, int, float] + ) -> None: ... + def shuffle(self, x: _ArrayLike[Any]) -> None: ... + @overload + def standard_cauchy(self, size: None = ...) -> float: ... + @overload + def standard_cauchy(self, size: _Size) -> ndarray: ... + @overload + def standard_exponential(self, size: None = ...) -> float: ... + @overload + def standard_exponential(self, size: _Size) -> ndarray: ... + @overload + def standard_gamma(self, size: None = ...) -> float: ... + @overload + def standard_gamma(self, size: _Size) -> ndarray: ... + @overload + def standard_normal(self, size: None = ...) -> float: ... + @overload + def standard_normal(self, size: _Size) -> ndarray: ... + @overload + def standard_t(self, t: int, size: None = ...) -> float: ... + @overload + def standard_t(self, t: _ScalarOrArray[int], size: _Size) -> ndarray: ... + @overload + def standard_t(self, t: _ArrayLike[int], size: None = ...) -> ndarray: ... + @overload + def tomaxint(self, t: int, size: None = ...) -> int: ... + @overload + def tomaxint(self, t: _ScalarOrArray[int], size: _Size) -> ndarray: ... + @overload + def tomaxint(self, t: _ArrayLike[int], size: None = ...) -> ndarray: ... + @overload + def triangular( + self, left: float, mode: float, right: float, size: None = ... + ) -> float: ... + @overload + def triangular( + self, + left: _ScalarOrArray[float], + mode: _ScalarOrArray[float], + right: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def triangular( + self, + left: _ArrayLike[float], + mode: float, + right: float, + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: float, + mode: _ArrayLike[float], + right: float, + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: float, + mode: float, + right: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: _ArrayLike[float], + mode: _ArrayLike[float], + right: float, + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: _ArrayLike[float], + mode: float, + right: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: float, + mode: _ArrayLike[float], + right: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def triangular( + self, + left: _ArrayLike[float], + mode: _ArrayLike[float], + right: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def uniform( + self, low: float = ..., high: float = ..., size: None = ... + ) -> float: ... + @overload + def uniform( + self, + low: _ScalarOrArray[float], + high: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def uniform( + self, low: _ArrayLike[float], high: float, size: None = ... + ) -> ndarray: ... + @overload + def uniform( + self, low: float, high: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def uniform( + self, low: _ArrayLike[float], high: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def vonmises(self, mu: float, kappa: float, size: None = ...) -> float: ... + @overload + def vonmises( + self, + mu: _ScalarOrArray[float], + kappa: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def vonmises( + self, mu: _ArrayLike[float], kappa: float, size: None = ... + ) -> ndarray: ... + @overload + def vonmises( + self, mu: float, kappa: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def vonmises( + self, mu: _ArrayLike[float], kappa: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def wald(self, mean: float, scale: float, size: None = ...) -> float: ... + @overload + def wald( + self, + mean: _ScalarOrArray[float], + scale: _ScalarOrArray[float], + size: _Size, + ) -> ndarray: ... + @overload + def wald( + self, mean: _ArrayLike[float], scale: float, size: None = ... + ) -> ndarray: ... + @overload + def wald( + self, mean: float, scale: _ArrayLike[float], size: None = ... + ) -> ndarray: ... + @overload + def wald( + self, + mean: _ArrayLike[float], + scale: _ArrayLike[float], + size: None = ..., + ) -> ndarray: ... + @overload + def weibull(self, a: float, size: None = ...) -> float: ... + @overload + def weibull(self, a: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def weibull(self, a: _ArrayLike[float], size: None = ...) -> ndarray: ... + @overload + def zipf(self, a: float, size: None = ...) -> int: ... + @overload + def zipf(self, a: _ScalarOrArray[float], size: _Size) -> ndarray: ... + @overload + def zipf(self, a: _ArrayLike[float], size: None = ...) -> ndarray: ... From 44c9946fce918312188606eacd269e636381e1d6 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 6 Jul 2018 19:00:47 -0400 Subject: [PATCH 4/5] Add a bit of a test suite --- tests/fail/random.py | 8 ++++++++ tests/pass/random.py | 5 +++++ tests/reveal/random.py | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 tests/fail/random.py create mode 100644 tests/pass/random.py create mode 100644 tests/reveal/random.py diff --git a/tests/fail/random.py b/tests/fail/random.py new file mode 100644 index 0000000..bb05957 --- /dev/null +++ b/tests/fail/random.py @@ -0,0 +1,8 @@ +import numpy as np + +np.random.RandomState(b"hello") # E: incompatible type +np.random.RandomState("") # E: incompatible type + +np.random.beta(3) # E: No overload variant +np.random.beta(3, "hello") # E: incompatible type +np.random.beta(3, 4, "hello") # E: incompatible type diff --git a/tests/pass/random.py b/tests/pass/random.py new file mode 100644 index 0000000..2986636 --- /dev/null +++ b/tests/pass/random.py @@ -0,0 +1,5 @@ +import numpy as np + +state = np.random.RandomState() +state = np.random.RandomState(3) +state = np.random.RandomState([3]) diff --git a/tests/reveal/random.py b/tests/reveal/random.py new file mode 100644 index 0000000..d9047f7 --- /dev/null +++ b/tests/reveal/random.py @@ -0,0 +1,4 @@ +import numpy as np + +np.random.beta(3, 4.0) # E: float +np.random.beta(3.0, 4.0, size=[3, 4]) # E: ndarray From 4bb8a467a12a3c3dd88901b67d9e968e4fa6dd48 Mon Sep 17 00:00:00 2001 From: Alan Du Date: Fri, 6 Jul 2018 19:16:27 -0400 Subject: [PATCH 5/5] Bump mypy version Better overloading rules --- test-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-requirements.txt b/test-requirements.txt index 8aba38f..4bcf5e7 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,4 +2,4 @@ dataclasses==0.6 flake8==3.3.0 flake8-pyi==17.3.0 pytest==3.4.2 -mypy==0.590 +mypy==0.610