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

More permissive type for random.choice and related functions #6562

Merged
merged 5 commits into from
Dec 10, 2021

Conversation

JelleZijlstra
Copy link
Member

It only does len() and [i]:

(3.6)

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        try:
            i = self._randbelow(len(seq))
        except ValueError:
            raise IndexError('Cannot choose from an empty sequence') from None
        return seq[i]

(3.9)

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        # raises IndexError if seq is empty
        return seq[self._randbelow(len(seq))]

It only does `len()` and `[i]`:

(3.6)
```
    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        try:
            i = self._randbelow(len(seq))
        except ValueError:
            raise IndexError('Cannot choose from an empty sequence') from None
        return seq[i]
```
(3.9)
```
    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        # raises IndexError if seq is empty
        return seq[self._randbelow(len(seq))]
```
stdlib/secrets.pyi Outdated Show resolved Hide resolved
@JelleZijlstra JelleZijlstra changed the title More permissive type for secrets.choice More permissive type for random.choice and related functions Dec 10, 2021
@github-actions

This comment has been minimized.

@srittau srittau merged commit 943dc5f into master Dec 10, 2021
@srittau srittau deleted the JelleZijlstra-patch-2 branch December 10, 2021 18:50
@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

2 participants