Skip to content

Commit

Permalink
More permissive type for random.choice and related functions (#6562)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Dec 10, 2021
1 parent 74ecc29 commit 943dc5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 9 additions & 4 deletions stdlib/random.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _random
import sys
from _typeshed import SupportsLenAndGetItem
from collections.abc import Callable, Iterable, MutableSequence, Sequence
from fractions import Fraction
from typing import Any, ClassVar, NoReturn, Tuple, TypeVar
Expand All @@ -17,10 +18,10 @@ class Random(_random.Random):
def randint(self, a: int, b: int) -> int: ...
if sys.version_info >= (3, 9):
def randbytes(self, n: int) -> bytes: ...
def choice(self, seq: Sequence[_T]) -> _T: ...
def choice(self, seq: SupportsLenAndGetItem[_T]) -> _T: ...
def choices(
self,
population: Sequence[_T],
population: SupportsLenAndGetItem[_T],
weights: Sequence[float | Fraction] | None = ...,
*,
cum_weights: Sequence[float | Fraction] | None = ...,
Expand Down Expand Up @@ -61,9 +62,13 @@ def randint(a: int, b: int) -> int: ...
if sys.version_info >= (3, 9):
def randbytes(n: int) -> bytes: ...

def choice(seq: Sequence[_T]) -> _T: ...
def choice(seq: SupportsLenAndGetItem[_T]) -> _T: ...
def choices(
population: Sequence[_T], weights: Sequence[float] | None = ..., *, cum_weights: Sequence[float] | None = ..., k: int = ...
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: ...

Expand Down
5 changes: 3 additions & 2 deletions stdlib/secrets.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from _typeshed import SupportsLenAndGetItem
from hmac import compare_digest as compare_digest
from random import SystemRandom as SystemRandom
from typing import Sequence, TypeVar
from typing import TypeVar

_T = TypeVar("_T")

def randbelow(exclusive_upper_bound: int) -> int: ...
def randbits(k: int) -> int: ...
def choice(seq: Sequence[_T]) -> _T: ...
def choice(seq: SupportsLenAndGetItem[_T]) -> _T: ...
def token_bytes(nbytes: int | None = ...) -> bytes: ...
def token_hex(nbytes: int | None = ...) -> str: ...
def token_urlsafe(nbytes: int | None = ...) -> str: ...

0 comments on commit 943dc5f

Please sign in to comment.