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

Deprecate the optional *random* argument to random.shuffle() #84645

Closed
rhettinger opened this issue May 1, 2020 · 11 comments
Closed

Deprecate the optional *random* argument to random.shuffle() #84645

rhettinger opened this issue May 1, 2020 · 11 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rhettinger
Copy link
Contributor

BPO 40465
Nosy @tim-one, @rhettinger, @vstinner, @serhiy-storchaka, @hugovk
PRs
  • bpo-40465: Deprecate the optional argument to random.shuffle(). #19867
  • bpo-40465: Remove random module features deprecated in 3.9 #25874
  • bpo-40465: Document random module changes in 3.11 What's new #31818
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-02.23:49:03.587>
    created_at = <Date 2020-05-01.08:31:41.792>
    labels = ['type-bug', 'library', '3.9']
    title = 'Deprecate the optional *random* argument to random.shuffle()'
    updated_at = <Date 2022-03-24.13:29:22.965>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2022-03-24.13:29:22.965>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-02.23:49:03.587>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-05-01.08:31:41.792>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40465
    keywords = ['patch']
    message_count = 8.0
    messages = ['367826', '367828', '367948', '370417', '370418', '415418', '415940', '415941']
    nosy_count = 5.0
    nosy_names = ['tim.peters', 'rhettinger', 'vstinner', 'serhiy.storchaka', 'hugovk']
    pr_nums = ['19867', '25874', '31818']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40465'
    versions = ['Python 3.9']

    @rhettinger
    Copy link
    Contributor Author

    shuffle(x, random=None) ⟼ shuffle(x)

    AFAICT, no one ever uses the optional parameter, nor would they have a valid reason to do so. It is an ancient API oddity and is inconsistent with the other methods in the module. I've long been annoyed by it and would like to see it cleaned-up before I retire ;-)

    https://docs.python.org/3/library/random.html#random.shuffle

    @rhettinger rhettinger added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 1, 2020
    @serhiy-storchaka
    Copy link
    Member

    +1

    @rhettinger
    Copy link
    Contributor Author

    New changeset 190fac9 by Raymond Hettinger in branch 'master':
    bpo-40465: Deprecate the optional argument to random.shuffle(). (bpo-19867)
    190fac9

    @asottile
    Copy link
    Mannequin

    asottile mannequin commented May 31, 2020

    pre-commit uses this to do deterministic shuffling, please don't remove this

    pre-commit/pre-commit#1479

    @serhiy-storchaka
    Copy link
    Member

    Raymond, could you please add an entry in What's New?

    @hugovk
    Copy link
    Member

    hugovk commented Mar 17, 2022

    #75999 adds an entry to What's New in 3.11.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 12c0012 by Tomáš Hrnčiar in branch 'main':
    bpo-40465: Document random module changes in 3.11 What's new (bpo-31818)
    12c0012

    @vstinner
    Copy link
    Member

    commit 70a071d
    Author: Raymond Hettinger <rhettinger@users.noreply.github.com>
    Date: Tue May 4 01:55:40 2021 -0700

    bpo-40465: Remove random module features deprecated in 3.9 (GH-25874)
    

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    exarkun added a commit to tahoe-lafs/collections-extended that referenced this issue Feb 13, 2023
    Instead, accept any arguments ``random.shuffle`` accepts.  Prior to Python
    3.11 this was `random` but Python 3.9 deprecated this feature and Python 3.11
    removed it.
    
    python/cpython#84645
    
    Accepting `*args, **kwargs` makes the signature compatible with whatever
    Python runtime happens to be in use.
    @shaperilio
    Copy link

    AFAICT, no one ever uses the optional parameter, nor would they have a valid reason to do so. It is an ancient API oddity and is inconsistent with the other methods in the module. I've long been annoyed by it and would like to see it cleaned-up before I retire ;-)

    Wow, what a bold statement 🤣. We were using it. Hope you're enjoying retirement.

    Can someone help me understand the motivation for this? I keep clicking on links trying to figure it out if there was a bug associated with this, but I'm not familiar with the issue tracking here.

    @serhiy-storchaka
    Copy link
    Member

    1. It complicated the code.
    2. When that argument was specified, the result was less random.
    3. The design just looked wrong.

    @tim-one
    Copy link
    Member

    tim-one commented Aug 23, 2023

    The default implementation of shuffle() stopped using random() long ago - it switched to a method avoiding floats entirely, and also avoiding the slight statistical biases the come with using floats for this.

    So if you passed random= after that change, it went back to using the older - different & inherently biased - algorithm.

    Keeping a worse algorithm in the code base was a Bad Idea on the face of it, especially given that nobody looking at this knew of anyone actually using the optional argument.

    In the very early days, Python used the feeble (by current standards) Wichmann–Hill generator. Then there was far more user interest in replacing it with something better, even if slower. For the far fewer users who still care about that, there's a documented way to subclass Random now to use whatever other underlying generator you like better, and in a way that affects all the methods (not just shuffle()) building on the underlying generator.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants