r/pythonhelp • u/Satiivas1 • May 25 '22
random.sample throws a warning with the counts parameter
Hi,
I am using random.sample with the counts parameter in my code and PyCharm throws a warning : Expected type 'Iterable[str] | None' (matched generic type 'Iterable[_T] | None'), got 'list[int]' instead
The code itself works fine, but I am losing my mind trying to get rid of the warning. Why does it do that? Here is an example of the code:
from random import sample
a, b, c = sample(["A", "B"], counts=[1, 2], k=3)
print(a, b, c)Results = B B A , A B B , B A B
2
Upvotes
2
u/Goobyalus May 25 '22
It's because
random.pyi(the python interface file) declares sample like this:Which I think erroneously implies that
populationandcountscontain the same type of data.If you change your
populationto[1, 2], the warning goes away, right?