Skip to content

Fixes "TypeError: Population must be a sequence." using random.sample()#2318

Closed
hiwakaba wants to merge 2 commits into
dpkp:masterfrom
hiwakaba:master
Closed

Fixes "TypeError: Population must be a sequence." using random.sample()#2318
hiwakaba wants to merge 2 commits into
dpkp:masterfrom
hiwakaba:master

Conversation

@hiwakaba

@hiwakaba hiwakaba commented Jun 27, 2022

Copy link
Copy Markdown
Contributor

Hello, I am a kafka-python rpm packager.

I would like to fix "TypeError" in test/test_assignors.py using Python-3.11. The population of random.sample() must be a sequence and automatic conversion of sets to lists is no longer supported in Python-3.11.

<mock-chroot> sh-5.1# python3
Python 3.11.0b3 (main, Jun 24 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import Random
>>> r = Random()
>>> r.seed('test')
>>> r.sample({'t1', 't10', 't2', 't3', 't4', 't5'}, k=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.11/random.py", line 436, in sample
    raise TypeError("Population must be a sequence.  "
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).

I think we should pass the data as an instance of list or sorted set.

>>> r.sample(sorted({'t1', 't10', 't2', 't3', 't4', 't5'}), k=4)
['t1', 't4', 't3', 't5']

References:

Thanks in advance,
Hirotaka Wakabayashi


This change is Reviewable

@wbarnha
wbarnha self-requested a review August 3, 2023 03:21
@wbarnha wbarnha added the bug label Aug 3, 2023
@wbarnha

wbarnha commented Aug 3, 2023

Copy link
Copy Markdown
Contributor

This patch is included in #2378, it will be merged in soon.

@wbarnha wbarnha closed this Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants