From ceae09c213f7a46f700ea6efe75ed9dfed33600d Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 28 Feb 2022 20:11:59 -0800 Subject: [PATCH 1/3] random: use bound methods Suggested by Akuli in #7400 Matches runtime better and minimises duplicated definitions. pytype only added support for bound methods about a year ago. We use them in a couple other places, e.g. multiprocessing and pkg_resources --- stdlib/random.pyi | 72 ++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/stdlib/random.pyi b/stdlib/random.pyi index b031ade0b608..cd82572ccb14 100644 --- a/stdlib/random.pyi +++ b/stdlib/random.pyi @@ -122,53 +122,29 @@ class SystemRandom(Random): def setstate(self, *args: Any, **kwds: Any) -> NoReturn: ... # ----- random function stubs ----- -if sys.version_info >= (3, 9): - def seed(a: int | float | str | bytes | bytearray | None = ..., version: int = ...) -> None: ... - -else: - def seed(a: Any = ..., version: int = ...) -> None: ... - -def getstate() -> object: ... -def setstate(state: object) -> None: ... -def getrandbits(__k: int) -> int: ... -def randrange(start: int, stop: None | int = ..., step: int = ...) -> int: ... -def randint(a: int, b: int) -> int: ... - -if sys.version_info >= (3, 9): - def randbytes(n: int) -> bytes: ... - -def choice(seq: SupportsLenAndGetItem[_T]) -> _T: ... -def choices( - population: SupportsLenAndGetItem[_T], - weights: Sequence[float] | None = ..., - *, - cum_weights: Sequence[float] | None = ..., - k: int = ..., -) -> list[_T]: ... -def shuffle(x: MutableSequence[Any], random: Callable[[], float] | None = ...) -> None: ... +_inst: Random +seed = _inst.seed +random = _inst.random +uniform = _inst.uniform +triangular = _inst.triangular +randint = _inst.randint +choice = _inst.choice +randrange = _inst.randrange +sample = _inst.sample +shuffle = _inst.shuffle +choices = _inst.choices +normalvariate = _inst.normalvariate +lognormvariate = _inst.lognormvariate +expovariate = _inst.expovariate +vonmisesvariate = _inst.vonmisesvariate +gammavariate = _inst.gammavariate +gauss = _inst.gauss +betavariate = _inst.betavariate +paretovariate = _inst.paretovariate +weibullvariate = _inst.weibullvariate +getstate = _inst.getstate +setstate = _inst.setstate +getrandbits = _inst.getrandbits if sys.version_info >= (3, 9): - def sample(population: Sequence[_T] | AbstractSet[_T], k: int, *, counts: Iterable[_T] | None = ...) -> list[_T]: ... - -else: - def sample(population: Sequence[_T] | AbstractSet[_T], k: int) -> list[_T]: ... - -def random() -> float: ... -def uniform(a: float, b: float) -> float: ... -def triangular(low: float = ..., high: float = ..., mode: float | None = ...) -> float: ... -def betavariate(alpha: float, beta: float) -> float: ... -def expovariate(lambd: float) -> float: ... -def gammavariate(alpha: float, beta: float) -> float: ... - -if sys.version_info >= (3, 11): - def gauss(mu: float = ..., sigma: float = ...) -> float: ... - def normalvariate(mu: float = ..., sigma: float = ...) -> float: ... - -else: - def gauss(mu: float, sigma: float) -> float: ... - def normalvariate(mu: float, sigma: float) -> float: ... - -def lognormvariate(mu: float, sigma: float) -> float: ... -def vonmisesvariate(mu: float, kappa: float) -> float: ... -def paretovariate(alpha: float) -> float: ... -def weibullvariate(alpha: float, beta: float) -> float: ... + randbytes = _inst.randbytes From 0f40425a15852b3eb82bbe42b6b7a76491e4338b Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 28 Feb 2022 21:40:53 -0800 Subject: [PATCH 2/3] Update stdlib/random.pyi Co-authored-by: Jelle Zijlstra --- stdlib/random.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/random.pyi b/stdlib/random.pyi index cd82572ccb14..16aa31a0f742 100644 --- a/stdlib/random.pyi +++ b/stdlib/random.pyi @@ -123,7 +123,7 @@ class SystemRandom(Random): # ----- random function stubs ----- -_inst: Random +_inst: Random = ... seed = _inst.seed random = _inst.random uniform = _inst.uniform From 85f2adfc2e7a9655a31189d4cebc6792805bc6fb Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 28 Feb 2022 21:42:08 -0800 Subject: [PATCH 3/3] allowlist --- tests/stubtest_allowlists/py36.txt | 1 - tests/stubtest_allowlists/py37.txt | 1 - tests/stubtest_allowlists/py38.txt | 1 - 3 files changed, 3 deletions(-) diff --git a/tests/stubtest_allowlists/py36.txt b/tests/stubtest_allowlists/py36.txt index 57777448a5d7..94025b954f6c 100644 --- a/tests/stubtest_allowlists/py36.txt +++ b/tests/stubtest_allowlists/py36.txt @@ -34,7 +34,6 @@ mmap.ACCESS_DEFAULT (os|posix).utime pyexpat.XMLParserType.ExternalEntityParserCreate # C signature is wrong - function gets only positional args random.Random.randrange # missing undocumented arg _int -random.randrange # missing undocumented arg _int sched.Event.__doc__ # __slots__ is overridden stringprep.unicodedata # re-exported from unicodedata sre_compile.dis diff --git a/tests/stubtest_allowlists/py37.txt b/tests/stubtest_allowlists/py37.txt index d6495f3af4a0..987623d79667 100644 --- a/tests/stubtest_allowlists/py37.txt +++ b/tests/stubtest_allowlists/py37.txt @@ -34,7 +34,6 @@ json.loads pyexpat.XMLParserType.ExternalEntityParserCreate # C signature is wrong - function gets only positional args queue.SimpleQueue.__init__ # Default C __init__ signature is wrong random.Random.randrange # missing undocumented arg _int -random.randrange # missing undocumented arg _int re.Pattern.scanner # Undocumented and not useful. #6405 sched.Event.__doc__ # __slots__ is overridden ssl.PROTOCOL_SSLv3 # Depends on ssl compilation diff --git a/tests/stubtest_allowlists/py38.txt b/tests/stubtest_allowlists/py38.txt index a9f9fd8aeee5..13a72397cef4 100644 --- a/tests/stubtest_allowlists/py38.txt +++ b/tests/stubtest_allowlists/py38.txt @@ -47,7 +47,6 @@ multiprocessing.spawn._main pickle.Pickler.reducer_override # implemented in C pickler queue.SimpleQueue.__init__ # Default C __init__ signature is wrong random.Random.randrange # missing undocumented arg _int -random.randrange # missing undocumented arg _int re.Pattern.scanner # Undocumented and not useful. #6405 sched.Event.__doc__ # __slots__ is overridden ssl.PROTOCOL_SSLv3 # Depends on ssl compilation