Skip to content

Convert sets to sorted lists prior to sampling - #2889

Merged
hwwhww merged 3 commits into
ethereum:devfrom
jtraglia:remove-set-sampling
May 10, 2022
Merged

Convert sets to sorted lists prior to sampling#2889
hwwhww merged 3 commits into
ethereum:devfrom
jtraglia:remove-set-sampling

Conversation

@jtraglia

@jtraglia jtraglia commented May 9, 2022

Copy link
Copy Markdown
Member

PR Description

According the docs for random.sample:

Deprecated since version 3.9: In the future, the population must be a sequence. Instances of set are no longer supported. The set must first be converted to a list or tuple, preferably in a deterministic order so that the sample is reproducible.

I used sorted() instead of list() to make things deterministic.

Fixed Warnings

tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py: 282 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py:307: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    return rng.sample(indices, participant_count)

tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py: 282 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py:284: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    return rng.sample(indices, participant_count)

tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py::test_is_assigned_to_sync_committee
tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py::test_is_assigned_to_sync_committee
tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py::test_is_assigned_to_sync_committee
  /consensus-specs/tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py:62: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    some_pubkeys = rng.sample(disqualified_pubkeys, sample_size)

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:203: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, 1))

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:211: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, 1))

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:218: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) // 3))

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:226: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) // 3))

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:233: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) - 1))

tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py:241: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) - 1))

tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py: 64 warnings
  /consensus-specs/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py:41: DeprecationWarning: Sampling from a set deprecated
  since Python 3.9 and will be removed in a subsequent version.
    return rng.sample(indices, participant_count)

jtraglia added 3 commits May 9, 2022 14:09
This is because sampling of sets has been deprecated in Python 3.9. I used
sorted() instead of list() so that things are deterministic.
I felt that the the lambda was a little too complicated.

@ralexstokes ralexstokes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one, thanks!

@hwwhww hwwhww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! @jtraglia

@hwwhww
hwwhww merged commit 96de0fe into ethereum:dev May 10, 2022
@jtraglia
jtraglia deleted the remove-set-sampling branch April 11, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants