Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from

Conversation

hiwakaba
Copy link
Contributor

@hiwakaba hiwakaba commented Jun 27, 2022

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
Copy link
Collaborator

wbarnha commented Aug 3, 2023

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.

None yet

2 participants