Skip to content

bpo-32554: Deprecate hashing arbitrary types in random.seed() #15382

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

Merged
merged 3 commits into from
Aug 22, 2019

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Aug 22, 2019

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I am just interesting, why implement a warning in random.Random.seed() instead of _random.Random.seed()?

@@ -86,6 +86,11 @@ Bookkeeping functions
.. versionchanged:: 3.2
Moved to the version 2 scheme which uses all of the bits in a string seed.

.. deprecated:: 3.9
In the future, the *seed* must be one of the following types:
:class:`NoneType`, :class:`int`, :class:`float`, :class:`str`,
Copy link
Member

Choose a reason for hiding this comment

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

There is no an entry for NoneType, so the link will not work.

Copy link
Member

Choose a reason for hiding this comment

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

Is float because of seed(time.time())?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

Lib/random.py Outdated
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
_warn('Seeding based on hashing is deprecated\n'
'since Python 3.9 and will be removed in a subsequent '
'version. The only \nsupported seed types are: None, '
Copy link
Member

Choose a reason for hiding this comment

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

I would break a string literal and a line after \n for readability

possible seed value. Unfortunately, some of those types are not
guaranteed to have a deterministic hash value. After Python 3.9,
the module will restrict its seeds to *None*, :class:`int`,
:class:`float`, :class:`str`, :class:`bytes`, and :class:`bytearray'.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
:class:`float`, :class:`str`, :class:`bytes`, and :class:`bytearray'.
:class:`float`, :class:`str`, :class:`bytes`, and :class:`bytearray`.

@rhettinger rhettinger merged commit d0cdeaa into python:master Aug 22, 2019
@rhettinger rhettinger deleted the deterministic_seed branch August 22, 2019 16:19
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
@Kodiologist
Copy link
Contributor

Excuse my density and lateness, but why was this feature deprecated and removed instead of being fixed? Couldn't you use a non-random hash? The Python standard library already provides a wide variety of hashes in hashlib.

@serhiy-storchaka
Copy link
Member

It is unrelated. hashlib does not allow you to make a hash from arbitrary object.

ngnpope added a commit to ngnpope/mimesis that referenced this pull request Nov 26, 2021
Makes the following changes:

- Added `float` to the `Union` for `Seed`.
- Added `None` to the `Union` for `Seed`.
- Replaced `Optional[Seed]` with `Seed` everywhere.

See the following links for details:

- https://bugs.python.org/issue32554
- python/cpython#15382
- https://docs.python.org/3.9/library/random.html#random.seed

Currently `typeshed` is still using `Any`.
ngnpope added a commit to ngnpope/mimesis that referenced this pull request Nov 26, 2021
Makes the following changes:

- Added `float` to the `Union` for `Seed`.
- Added `None` to the `Union` for `Seed`.
- Replaced `Optional[Seed]` with `Seed` everywhere.

See the following links for details:

- https://bugs.python.org/issue32554
- python/cpython#15382
- https://docs.python.org/3.9/library/random.html#random.seed

Currently `typeshed` is still using `Any`.
ngnpope added a commit to ngnpope/mimesis that referenced this pull request Nov 27, 2021
Makes the following changes:

- Added `float` to the `Union` for `Seed`.
- Added `None` to the `Union` for `Seed`.
- Replaced `Optional[Seed]` with `Seed` everywhere.

See the following links for details:

- https://bugs.python.org/issue32554
- python/cpython#15382
- https://docs.python.org/3.9/library/random.html#random.seed

Currently `typeshed` is still using `Any`.
ngnpope added a commit to ngnpope/mimesis that referenced this pull request Nov 30, 2021
Makes the following changes:

- Added `float` to the `Union` for `Seed`.
- Added `None` to the `Union` for `Seed`.
- Replaced `Optional[Seed]` with `Seed` everywhere.

See the following links for details:

- https://bugs.python.org/issue32554
- python/cpython#15382
- https://docs.python.org/3.9/library/random.html#random.seed

Currently `typeshed` is still using `Any`.
lk-geimfari pushed a commit to lk-geimfari/mimesis that referenced this pull request Nov 30, 2021
Makes the following changes:

- Added `float` to the `Union` for `Seed`.
- Added `None` to the `Union` for `Seed`.
- Replaced `Optional[Seed]` with `Seed` everywhere.

See the following links for details:

- https://bugs.python.org/issue32554
- python/cpython#15382
- https://docs.python.org/3.9/library/random.html#random.seed

Currently `typeshed` is still using `Any`.
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.

5 participants