Thanks to visit codestin.com
Credit goes to github.com

Skip to content

random.Random: use Any and object for state #6064

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

Closed
wants to merge 4 commits into from

Conversation

Akuli
Copy link
Collaborator

@Akuli Akuli commented Sep 23, 2021

Fixes #6063.

I used object as the return value of getstate(), because you aren't supposed to rely on what it is, and you can still be more specific in a subclass if you want. But setstate() needs Any, because if it would accept an arbitrary object, then all subclasses would also need to handle arbitrary objects.

There is also module-level random.getstate() and random.setstate() which both use object. That's fine because they cannot be replaced in a subclass.

@srittau
Copy link
Collaborator

srittau commented Sep 23, 2021

As I outlined in #6063, I don't think this change is a good idea. While we prevent false positives when overriding the Random class (arguably a fringe use case), we lose all type checking for getting/setting the state.

@github-actions

This comment has been minimized.

@Akuli
Copy link
Collaborator Author

Akuli commented Sep 23, 2021

Makes sense.

@Akuli Akuli closed this Sep 23, 2021
@Akuli Akuli deleted the random-any branch September 23, 2021 13:37
@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.

random.Random.getvalue and random.Random.setvalue are unduly opinionated about their types and ill-suited for subclassing
2 participants